我想了解如何实现简洁的 python,以便在每次预测后重新训练,因此每次预测后训练集的大小都会增加。我正在尝试通过配置文件设置整洁的 python,以便在每次预测测试/未见集后重新训练。例如,如果 XOR“进化最小”示例,根据我的理解,它可以进行调整,以便它对部分数据进行训练(达到特定的适应度水平,获得最佳基因组),然后它会根据设置的其他数据进行预测一边作为测试集。请参阅下面的代码以了解我的意思: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贡献1820条经验 获得超9个赞
如果我理解你的要求是正确的,这不能简单地在 config_file 中完成。
config_file 中定义的参数只是改变模型直接运行数据时发生的情况,无需任何重新训练即可进行预测。
如果您希望模型在每次预测后重新训练,您必须在eval_genomes
和/或run
函数中实现此功能。您可以在迭代每个基因组的循环中添加另一个 for 循环以获取每个输出并重新训练模型。但是,这可能会显着增加计算时间,因为您不是简单地获取输出,而是为每个输出运行另一组训练生成。
添加回答
举报
0/150
提交
取消