Python NEAT 在某一点后不再进一步学习
Python NEAT not learning further after a certain point
看来我的程序一直在努力学习直到达到某个点,然后它就满足了,根本就停止了改进和改变。根据我的测试,它通常最多达到 -5 的值,然后无论我保持多久它都保持不变 运行。结果集也没有改变。
只是为了跟踪它,我做了自己的日志记录,看看哪个做得最好。 1 和 0 的数组指的是 AI 做出正确选择的频率 (1),以及 AI 做出错误选择的频率 (0)。
我的目标是让 AI 重复高于 0.5 然后低于 0.5 的模式,而不一定要找到奇数。这只是一个小测试,看看我能否让 AI 正确处理一些基本数据,然后再做一些更高级的事情。
但不幸的是它不起作用,我不确定为什么。
代码:
import os
import neat
def main(genomes, config):
networks = []
ge = []
choices = []
for _, genome in genomes:
network = neat.nn.FeedForwardNetwork.create(genome, config)
networks.append(network)
genome.fitness = 0
ge.append(genome)
choices.append([])
for x in range(25):
for i, genome in enumerate(ge):
output = networks[i].activate([x])
# print(str(x) + " - " + str(i) + " chose " + str(output[0]))
if output[0] > 0.5:
if x % 2 == 0:
ge[i].fitness += 1
choices[i].append(1)
else:
ge[i].fitness -= 5
choices[i].append(0)
else:
if not x % 2 == 0:
ge[i].fitness += 1
choices[i].append(1)
else:
ge[i].fitness -= 5
choices[i].append(0)
pass
# Optional death function, if I use this there are no winners at any point.
# if ge[i].fitness <= 20:
# ge[i].fitness -= 100
# ge.pop(i)
# choices.pop(i)
# networks.pop(i)
if len(ge) > 0:
fittest = -1
fitness = -999999
for i, genome in enumerate(ge):
if ge[i].fitness > fitness:
fittest = i
fitness = ge[i].fitness
print("Best: " + str(fittest) + " with fitness " + str(fitness))
print(str(choices[fittest]))
else:
print("Done with no best.")
def run(config_path):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet,
neat.DefaultStagnation, config_path)
pop = neat.Population(config)
#pop.add_reporter(neat.StdOutReporter(True))
#stats = neat.StatisticsReporter()
#pop.add_reporter(stats)
winner = pop.run(main, 100)
if __name__ == "__main__":
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "config-feedforward.txt")
run(config_path)
NEAT 配置:
[NEAT]
fitness_criterion = max
fitness_threshold = 100000
pop_size = 5000
reset_on_extinction = False
[DefaultGenome]
# node activation options
activation_default = tanh
activation_mutate_rate = 0.0
activation_options = tanh
# 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.1
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 = 1
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
很遗憾地告诉您,这种方法行不通。请记住,神经网络通常是通过进行矩阵乘法然后将最大值与 0 相乘而构建的(这称为 RELU),因此基本上每一层都是线性的并带有截止值(不,选择不同的激活函数如 sigmoid 不会有帮助) .您希望网络产生 >.5、<.5、>.5、<.5、... 25 次。想象一下用 RELU 片段构建它需要什么。你至少需要一个约 25 层深的网络,而 NEAT 不会在进化过程中没有持续的增量进展的情况下产生这么大的网络。虽然你是好伙伴,你所做的相当于学习模运算符,已经研究了很多年。这是一个成功的 post,尽管没有使用 NEAT。
您可以使用 NEAT 取得的唯一真正进步是为网络提供更多功能作为输入,例如给它 x%2
作为输入,它会学得很快,尽管这显然是 'cheating'.
看来我的程序一直在努力学习直到达到某个点,然后它就满足了,根本就停止了改进和改变。根据我的测试,它通常最多达到 -5 的值,然后无论我保持多久它都保持不变 运行。结果集也没有改变。
只是为了跟踪它,我做了自己的日志记录,看看哪个做得最好。 1 和 0 的数组指的是 AI 做出正确选择的频率 (1),以及 AI 做出错误选择的频率 (0)。
我的目标是让 AI 重复高于 0.5 然后低于 0.5 的模式,而不一定要找到奇数。这只是一个小测试,看看我能否让 AI 正确处理一些基本数据,然后再做一些更高级的事情。
但不幸的是它不起作用,我不确定为什么。
代码:
import os
import neat
def main(genomes, config):
networks = []
ge = []
choices = []
for _, genome in genomes:
network = neat.nn.FeedForwardNetwork.create(genome, config)
networks.append(network)
genome.fitness = 0
ge.append(genome)
choices.append([])
for x in range(25):
for i, genome in enumerate(ge):
output = networks[i].activate([x])
# print(str(x) + " - " + str(i) + " chose " + str(output[0]))
if output[0] > 0.5:
if x % 2 == 0:
ge[i].fitness += 1
choices[i].append(1)
else:
ge[i].fitness -= 5
choices[i].append(0)
else:
if not x % 2 == 0:
ge[i].fitness += 1
choices[i].append(1)
else:
ge[i].fitness -= 5
choices[i].append(0)
pass
# Optional death function, if I use this there are no winners at any point.
# if ge[i].fitness <= 20:
# ge[i].fitness -= 100
# ge.pop(i)
# choices.pop(i)
# networks.pop(i)
if len(ge) > 0:
fittest = -1
fitness = -999999
for i, genome in enumerate(ge):
if ge[i].fitness > fitness:
fittest = i
fitness = ge[i].fitness
print("Best: " + str(fittest) + " with fitness " + str(fitness))
print(str(choices[fittest]))
else:
print("Done with no best.")
def run(config_path):
config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet,
neat.DefaultStagnation, config_path)
pop = neat.Population(config)
#pop.add_reporter(neat.StdOutReporter(True))
#stats = neat.StatisticsReporter()
#pop.add_reporter(stats)
winner = pop.run(main, 100)
if __name__ == "__main__":
local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, "config-feedforward.txt")
run(config_path)
NEAT 配置:
[NEAT]
fitness_criterion = max
fitness_threshold = 100000
pop_size = 5000
reset_on_extinction = False
[DefaultGenome]
# node activation options
activation_default = tanh
activation_mutate_rate = 0.0
activation_options = tanh
# 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.1
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 = 1
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
很遗憾地告诉您,这种方法行不通。请记住,神经网络通常是通过进行矩阵乘法然后将最大值与 0 相乘而构建的(这称为 RELU),因此基本上每一层都是线性的并带有截止值(不,选择不同的激活函数如 sigmoid 不会有帮助) .您希望网络产生 >.5、<.5、>.5、<.5、... 25 次。想象一下用 RELU 片段构建它需要什么。你至少需要一个约 25 层深的网络,而 NEAT 不会在进化过程中没有持续的增量进展的情况下产生这么大的网络。虽然你是好伙伴,你所做的相当于学习模运算符,已经研究了很多年。这是一个成功的 post,尽管没有使用 NEAT。
您可以使用 NEAT 取得的唯一真正进步是为网络提供更多功能作为输入,例如给它 x%2
作为输入,它会学得很快,尽管这显然是 'cheating'.