我想了解如何实现简洁的 python,以便在每次预测后重新训练,因此每次预测后训练集的大小都会增加。
我正在尝试通过配置文件设置整洁的 python,以便在每次预测测试/未见集后重新训练。例如,如果 XOR“进化最小”示例,根据我的理解,它可以进行调整,以便它对部分数据进行训练(达到特定的适应度水平,获得最佳基因组),然后它会根据设置的其他数据进行预测一边作为测试集。请参阅下面的代码以了解我的意思:
from __future__ import print_function
import neat
import visualize
# 2-input XOR inputs and expected outputs. Training set
xor_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 set
xor_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 genome
for 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 + 1
accuracy = count / len(xor_outputs2)
print('\nAccuracy: ', accuracy)
慕妹3146593
相关分类