python整洁示例已损坏
我正在尝试运行下面的代码,从中可以将遗传算法应用于神经网络。这是一个非常有趣的概念,只是我无法运行示例代码python整洁示例已损坏,python,machine-learning,Python,Machine Learning,我正在尝试运行下面的代码,从中可以将遗传算法应用于神经网络。这是一个非常有趣的概念,只是我无法运行示例代码 """ 2-input XOR example -- this is most likely the simplest possible example. """ from __future__ import print_function import os import neat import visualize # 2-i
"""
2-input XOR example -- this is most likely the simplest possible example.
"""
from __future__ import print_function
import os
import neat
import visualize
# 2-input XOR inputs and expected outputs.
xor_inputs = [(0.0, 0.0), (0.0, 1.0), (1.0, 0.0), (1.0, 1.0)]
xor_outputs = [ (0.0,), (1.0,), (1.0,), (0.0,)]
def eval_genomes(genomes, config):
for genome_id, genome in genomes:
genome.fitness = 4.0
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
def run(config_file):
# Load configuration.
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_file)
# 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)
p.add_reporter(neat.Checkpointer(5))
# Run for up to 300 generations.
winner = p.run(eval_genomes, 300)
# 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)
for xi, xo in zip(xor_inputs, xor_outputs):
output = winner_net.activate(xi)
print("input {!r}, expected output {!r}, got {!r}".format(xi, xo, output))
node_names = {-1:'A', -2: 'B', 0:'A XOR B'}
visualize.draw_net(config, winner, True, node_names=node_names)
visualize.plot_stats(stats, ylog=False, view=True)
visualize.plot_species(stats, view=True)
p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-4')
p.run(eval_genomes, 10)
if __name__ == '__main__':
# Determine path to configuration file. This path manipulation is
# here so that the script will run successfully regardless of the
# current working directory.
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'config-feedforward')
run(config_path)
这是配置文件
#--- parameters for the XOR-2 experiment ---#
[NEAT]
fitness_criterion = max
fitness_threshold = 3.9
pop_size = 150
reset_on_extinction = False
[DefaultGenome]
# node activation options
activation_default = sigmoid
activation_mutate_rate = 0.0
activation_options = sigmoid
# node aggregation options
aggregation_default = sum
aggregation_mutate_rate = 0.0
aggregation_options = sum
# node bias options
bias_init_mean = 0.0
bias_init_stdev = 1.0
bias_max_value = 30.0
bias_min_value = -30.0
bias_mutate_power = 0.5
bias_mutate_rate = 0.7
bias_replace_rate = 0.1
# genome compatibility options
compatibility_disjoint_coefficient = 1.0
compatibility_weight_coefficient = 0.5
# connection add/remove rates
conn_add_prob = 0.5
conn_delete_prob = 0.5
# connection enable options
enabled_default = True
enabled_mutate_rate = 0.01
feed_forward = True
initial_connection = full
# node add/remove rates
node_add_prob = 0.2
node_delete_prob = 0.2
# network parameters
num_hidden = 0
num_inputs = 2
num_outputs = 1
# node response options
response_init_mean = 1.0
response_init_stdev = 0.0
response_max_value = 30.0
response_min_value = -30.0
response_mutate_power = 0.0
response_mutate_rate = 0.0
response_replace_rate = 0.0
# connection weight options
weight_init_mean = 0.0
weight_init_stdev = 1.0
weight_max_value = 30
weight_min_value = -30
weight_mutate_power = 0.5
weight_mutate_rate = 0.8
weight_replace_rate = 0.1
[DefaultSpeciesSet]
compatibility_threshold = 3.0
[DefaultStagnation]
species_fitness_func = max
max_stagnation = 20
species_elitism = 2
[DefaultReproduction]
elitism = 2
survival_threshold = 0.2
最后,这里是我安装的python neat版本
pip show neat-python
Name: neat-python
Version: 0.92
Summary: A NEAT (NeuroEvolution of Augmenting Topologies) implementation
Home-page: https://github.com/CodeReclaimers/neat-python
有人能帮我解决以下错误吗?因为到目前为止,我尝试的是从github安装和从pip安装,这两种方法在示例代码中都给出了相同的错误。我也尝试了变体代码,但我得到了相同的错误
****** Running generation 0 ******
Traceback (most recent call last):
File "driver.py", line 34, in <module>
winner = p.run(eval_genomes)
File "/home/j/anaconda3/lib/python3.8/site-packages/neat_python-0.92-py3.8.egg/neat/population.py", line 88, in run
File "driver.py", line 18, in eval_genomes
output = net.activate(xi)
File "/home/j/anaconda3/lib/python3.8/site-packages/neat_python-0.92-py3.8.egg/neat/nn/feed_forward.py", line 13, in activate
RuntimeError: Expected 3 inputs, got 2
****正在运行第0代*******
回溯(最近一次呼叫最后一次):
文件“driver.py”,第34行,在
winner=p.run(评估基因组)
文件“/home/j/anaconda3/lib/python3.8/site packages/neat_python-0.92-py3.8.egg/neat/population.py”,第88行,运行中
文件“driver.py”,第18行,在eval_基因组中
输出=净激活(xi)
文件“/home/j/anaconda3/lib/python3.8/site packages/neat_python-0.92-py3.8.egg/neat/nn/feed_forward.py”,第13行,激活
运行时错误:预期3个输入,得到2个
看起来该项目已经5年没有活动了。我不想让你气馁,但你能为此投入多少精力?@alexander.Hayes我有很多空闲时间。