【基于neat-python库实现hyperneat】neat-python实现自定义激活函数,以及代码修改部分

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...#这是在进化过程中可能突变的激活函数种类,空格分隔

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值