遗传算法优化bp神经网络代码
时间: 2023-08-23 10:05:49 浏览: 98
基于遗传算法的Bp神经网络优化算法代码.rar
遗传算法和BP神经网络都是优化算法,结合可以提高模型的准确性和鲁棒性。以下是一个简单的遗传算法优化BP神经网络的示例代码:
```python
import numpy as np
# 定义BP神经网络
class BPNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
# 初始化权重和偏置
self.weights1 = np.random.randn(input_size, hidden_size)
self.bias1 = np.random.randn(hidden_size)
self.weights2 = np.random.randn(hidden_size, output_size)
self.bias2 = np.random.randn(output_size)
# 前向传播
def forward(self, X):
self.z1 = np.dot(X, self.weights1) + self.bias1
self.a1 = np.tanh(self.z1)
self.z2 = np.dot(self.a1, self.weights2) + self.bias2
self.a2 = np.tanh(self.z2)
return self.a2
# 反向传播
def backward(self, X, y, learning_rate):
# 计算输出误差
delta2 = (self.a2 - y) * (1 - np.power(self.a2, 2))
# 计算隐藏层误差
delta1 = np.dot(delta2, self.weights2.T) * (1 - np.power(self.a1, 2))
# 更新权重和偏置
self.weights2 -= learning_rate * np.dot(self.a1.T, delta2)
self.bias2 -= learning_rate * np.sum(delta2, axis=0)
self.weights1 -= learning_rate * np.dot(X.T, delta1)
self.bias1 -= learning_rate * np.sum(delta1, axis=0)
# 定义适应度函数
def fitness_function(weights):
neural_network.weights1 = np.reshape(weights[:input_size*hidden_size], (input_size, hidden_size))
neural_network.bias1 = np.reshape(weights[input_size*hidden_size:hidden_size*(input_size+1)], (hidden_size,))
neural_network.weights2 = np.reshape(weights[hidden_size*(input_size+1):hidden_size*(input_size+1)+hidden_size*output_size], (hidden_size, output_size))
neural_network.bias2 = np.reshape(weights[hidden_size*(input_size+1)+hidden_size*output_size:], (output_size,))
# 计算预测值和误差
y_pred = neural_network.forward(X_train)
loss = np.mean(np.power(y_pred - y_train, 2))
# 返回逆误差作为适应度值
return 1 / loss
# 遗传算法
class GeneticAlgorithm:
def __init__(self, population_size, weights_size, fitness_function):
self.population_size = population_size
self.weights_size = weights_size
self.fitness_function = fitness_function
# 初始化种群
self.population = np.random.randn(population_size, weights_size)
# 选择
def selection(self, fitness):
return np.random.choice(range(self.population_size), size=self.population_size, replace=True, p=fitness/fitness.sum())
# 交叉
def crossover(self, parent1, parent2):
crossover_point = np.random.randint(0, self.weights_size)
child1 = np.concatenate((parent1[:crossover_point], parent2[crossover_point:]))
child2 = np.concatenate((parent2[:crossover_point], parent1[crossover_point:]))
return child1, child2
# 变异
def mutation(self, offspring):
mutation_point = np.random.randint(0, self.weights_size)
offspring[mutation_point] += np.random.randn()
return offspring
# 进化
def evolve(self, generations):
for i in range(generations):
# 计算适应度
fitness = np.array([self.fitness_function(weights) for weights in self.population])
# 选择
parents = self.selection(fitness)
# 交叉
offspring = np.array([self.crossover(self.population[parent1], self.population[parent2]) for parent1, parent2 in zip(parents[::2], parents[1::2])]).reshape(-1, self.weights_size)
# 变异
offspring = np.array([self.mutation(child) for child in offspring])
# 更新种群
self.population = offspring
# 输出进化过程
print('Generation: {}\tBest Fitness: {}'.format(i+1, fitness.max()))
# 返回最佳个体
return self.population[fitness.argmax()]
# 训练数据
X_train = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_train = np.array([[0], [1], [1], [0]])
# 神经网络参数
input_size = 2
hidden_size = 3
output_size = 1
# 初始化神经网络
neural_network = BPNeuralNetwork(input_size, hidden_size, output_size)
# 训练神经网络
genetic_algorithm = GeneticAlgorithm(population_size=20, weights_size=(input_size*hidden_size+hidden_size*(input_size+1)+hidden_size*output_size+output_size), fitness_function=fitness_function)
best_individual = genetic_algorithm.evolve(generations=100)
# 使用最佳个体预测
neural_network.weights1 = np.reshape(best_individual[:input_size*hidden_size], (input_size, hidden_size))
neural_network.bias1 = np.reshape(best_individual[input_size*hidden_size:hidden_size*(input_size+1)], (hidden_size,))
neural_network.weights2 = np.reshape(best_individual[hidden_size*(input_size+1):hidden_size*(input_size+1)+hidden_size*output_size], (hidden_size, output_size))
neural_network.bias2 = np.reshape(best_individual[hidden_size*(input_size+1)+hidden_size*output_size:], (output_size,))
y_pred = neural_network.forward(X_train)
print('Predictions:', y_pred)
```
在这个示例中,我们使用遗传算法来优化BP神经网络的权重和偏置。适应度函数计算预测值和真实值之间的误差,并返回逆误差作为适应度值。遗传算法的三个操作(选择、交叉和变异)在`GeneticAlgorithm`类中实现。在训练过程中,我们使用遗传算法的进化过程来优化神经网络的权重和偏置。最后,使用最佳个体进行预测。
阅读全文