翻翻過(guò)去那場(chǎng)雪
2021-09-28 17:12:55
我想了解如何實(shí)現(xiàn)簡(jiǎn)潔的 python,以便在每次預(yù)測(cè)后重新訓(xùn)練,因此每次預(yù)測(cè)后訓(xùn)練集的大小都會(huì)增加。我正在嘗試通過(guò)配置文件設(shè)置整潔的 python,以便在每次預(yù)測(cè)測(cè)試/未見集后重新訓(xùn)練。例如,如果 XOR“進(jìn)化最小”示例,根據(jù)我的理解,它可以進(jìn)行調(diào)整,以便它對(duì)部分?jǐn)?shù)據(jù)進(jìn)行訓(xùn)練(達(dá)到特定的適應(yīng)度水平,獲得最佳基因組),然后它會(huì)根據(jù)設(shè)置的其他數(shù)據(jù)進(jìn)行預(yù)測(cè)一邊作為測(cè)試集。請(qǐng)參閱下面的代碼以了解我的意思:from __future__ import print_functionimport neatimport visualize# 2-input XOR inputs and expected outputs. Training setxor_inputs = [(0.0, 0.0, 0.0), (0.0, 1.0, 0.0), (1.0, 1.0, 1.0), (0.0, 0.0, 1.0), (1.0, 1.0, 0.0)]xor_outputs = [(1.0,), (1.0,), (1.0,), (0.0,), (0.0,)]# Test setxor_inputs2 = [(1.0, 0.0, 1.0), (1.0, 1.0, 0.0), (1.0, 0.0, 0.0)]xor_outputs2 = [(1.0,), (0.0,), (0.0,)]def eval_genomes(genomes, config): for genome_id, genome in genomes: genome.fitness = 5 net = neat.nn.FeedForwardNetwork.create(genome, config) for xi, xo in zip(xor_inputs, xor_outputs): output = net.activate(xi) genome.fitness -= (output[0] - xo[0]) ** 2# Load configuration.config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, 'config-feedforward')# Create the population, which is the top-level object for a NEAT run.p = neat.Population(config)# Add a stdout reporter to show progress in the terminal.p.add_reporter(neat.StdOutReporter(True))stats = neat.StatisticsReporter()p.add_reporter(stats)# Run until a solution is found.winner = p.run(eval_genomes) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner))# Show output of the most fit genome against training data.print('\nOutput:')winner_net = neat.nn.FeedForwardNetwork.create(winner, config)count = 0#To make predictions using the best genomefor xi, xo in zip(xor_inputs2, xor_outputs2): prediction = winner_net.activate(xi) print(" input {!r}, expected output {!r}, got {!r}".format( xi, xo[0], round(prediction[0]))) #to get prediction accuracy if int(xo[0]) == int(round(prediction[0])): count = count + 1accuracy = count / len(xor_outputs2)print('\nAccuracy: ', accuracy)
1 回答

慕妹3146593
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果我理解你的要求是正確的,這不能簡(jiǎn)單地在 config_file 中完成。
config_file 中定義的參數(shù)只是改變模型直接運(yùn)行數(shù)據(jù)時(shí)發(fā)生的情況,無(wú)需任何重新訓(xùn)練即可進(jìn)行預(yù)測(cè)。
如果您希望模型在每次預(yù)測(cè)后重新訓(xùn)練,您必須在eval_genomes
和/或run
函數(shù)中實(shí)現(xiàn)此功能。您可以在迭代每個(gè)基因組的循環(huán)中添加另一個(gè) for 循環(huán)以獲取每個(gè)輸出并重新訓(xùn)練模型。但是,這可能會(huì)顯著增加計(jì)算時(shí)間,因?yàn)槟皇呛?jiǎn)單地獲取輸出,而是為每個(gè)輸出運(yùn)行另一組訓(xùn)練生成。
添加回答
舉報(bào)
0/150
提交
取消