1、需要实现最简单的neat实验,用于验证自定义激活函数是否成功定义。
import neat
import os
# 定义自定义激活函数
def 自定义的激活函数(x):
return x ** 2
# 定义 XOR 输入和输出
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:
net = neat.nn.FeedForwardNetwork.create(genome, config)
error = 0.0
for xi, xo in zip(xor_inputs, xor_outputs):
output = net.activate(xi)
error += (output[0] - xo[0]) ** 2
genome.fitness = 4.0 - error # 错误越小适应度越高
# 创建 neat 配置
config_path = "config-feedforward" # 替换为你的配置文件路径
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
# 注册自定义激活函数到 genome_config
config.genome_config.add_activation('自定义激活函数在配置文件中的名字', 自定义的激活函数)
# 创建 NEAT 种群
p = neat.Population(config)
# 添加报告者
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
# 运行 NEAT 算法
winner = p.run(eval_genomes, 10)
# 显示最佳基因组
print('\nBest genome:\n{!s}'.format(winner))
# 检查激活函数集中是否包括自定义的 'square' 激活函数
activation_functions = neat.activations.ActivationFunctionSet()
activation_functions.add('aaaa', test)
print("Registered Activation Functions:", activation_functions.functions)
1.1、关键部分
# 定义自定义激活函数
def test(x):
return x ** 2
# 创建 neat 配置
config_path = "config-feedforward" # 替换为你的配置文件路径
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
# 注册自定义激活函数到 genome_config
config.genome_config.add_activation('a', test)
2、自定义函数迁移到其他文件
首先,新建py文件。
import 必要的库
#自定义激活函数
def test(x):
return x ** 2
# 注册自定义函数
def regist(config):
config.genome_config.add_activation('a', test)
2.1主程序修改的部分
import neat
import os
from multiFx import *
# 创建 neat 配置
config_path = "config-feedforward" # 替换为你的配置文件路径
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
# 注册自定义激活函数到 genome_config
#config.genome_config.add_activation('a', test)
regist(config) # 注册自定义激活函数
全部代码
import neat
import os
from multiFx import *
# 定义 XOR 输入和输出
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:
net = neat.nn.FeedForwardNetwork.create(genome, config)
error = 0.0
for xi, xo in zip(xor_inputs, xor_outputs):
output = net.activate(xi)
error += (output[0] - xo[0]) ** 2
genome.fitness = 4.0 - error # 错误越小适应度越高
# 创建 neat 配置
config_path = "config-feedforward" # 替换为你的配置文件路径
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
config_path)
# 注册自定义激活函数到 genome_config
#config.genome_config.add_activation('a', test)
regist(config) # 注册自定义激活函数
# 创建 NEAT 种群
p = neat.Population(config)
# 添加报告者
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
# 运行 NEAT 算法
winner = p.run(eval_genomes, 10)
# 显示最佳基因组
print('\nBest genome:\n{!s}'.format(winner))
# 检查激活函数集中是否包括自定义的 'square' 激活函数
activation_functions = neat.activations.ActivationFunctionSet()
activation_functions.add('a', test)
#print("Registered Activation Functions:", activation_functions.functions)
3、配置文件修改:按需求修改这俩参数
activation_default = a #这是默认的激活函数
activation_mutate_rate = 0.0
activation_options = a sigmoid xxx xxx...#这是在进化过程中可能突变的激活函数种类,空格分隔