请编写一个免疫遗传算法优化BP神经网络的代码框架
时间: 2024-05-11 08:20:25 浏览: 169
以下是一个免疫遗传算法优化BP神经网络的代码框架,供参考:
```python
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 定义BP神经网络类
class BPNN:
def __init__(self, n_input, n_hidden, n_output):
self.n_input = n_input
self.n_hidden = n_hidden
self.n_output = n_output
self.W1 = np.random.randn(n_input, n_hidden)
self.b1 = np.random.randn(n_hidden)
self.W2 = np.random.randn(n_hidden, n_output)
self.b2 = np.random.randn(n_output)
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def forward(self, X):
self.z1 = np.dot(X, self.W1) + self.b1
self.a1 = self.sigmoid(self.z1)
self.z2 = np.dot(self.a1, self.W2) + self.b2
self.a2 = self.sigmoid(self.z2)
return self.a2
def sigmoid_derivative(self, x):
return x * (1 - x)
def backpropagation(self, X, y, learning_rate):
delta_a2 = (self.a2 - y) * self.sigmoid_derivative(self.a2)
delta_z2 = np.dot(delta_a2, self.W2.T)
delta_a1 = delta_z2 * self.sigmoid_derivative(self.a1)
dW2 = np.dot(self.a1.T, delta_a2)
db2 = np.sum(delta_a2, axis=0)
dW1 = np.dot(X.T, delta_a1)
db1 = np.sum(delta_a1, axis=0)
self.W2 -= learning_rate * dW2
self.b2 -= learning_rate * db2
self.W1 -= learning_rate * dW1
self.b1 -= learning_rate * db1
# 定义免疫遗传算法类
class IGA:
def __init__(self, pop_size, max_gen, mutation_rate, eli_rate, num_immunes, num_clones, clone_rate):
self.pop_size = pop_size
self.max_gen = max_gen
self.mutation_rate = mutation_rate
self.eli_rate = eli_rate
self.num_immunes = num_immunes
self.num_clones = num_clones
self.clone_rate = clone_rate
self.pop = []
def init_pop(self, n_genes):
for i in range(self.pop_size):
individual = np.random.randn(n_genes)
self.pop.append(individual)
def fitness_func(self, X_train, y_train, X_test, y_test):
def calc_accuracy(individual):
nn = BPNN(n_input=X_train.shape[1], n_hidden=10, n_output=1)
nn.W1 = individual[:nn.n_input*nn.n_hidden].reshape(nn.n_input, nn.n_hidden)
nn.b1 = individual[nn.n_input*nn.n_hidden:nn.n_input*nn.n_hidden+nn.n_hidden]
nn.W2 = individual[nn.n_input*nn.n_hidden+nn.n_hidden:nn.n_input*nn.n_hidden+nn.n_hidden+nn.n_hidden*nn.n_output].reshape(nn.n_hidden, nn.n_output)
nn.b2 = individual[nn.n_input*nn.n_hidden+nn.n_hidden+nn.n_hidden*nn.n_output:]
y_pred_train = nn.forward(X_train)
y_pred_train = np.round(y_pred_train)
train_acc = accuracy_score(y_train, y_pred_train)
y_pred_test = nn.forward(X_test)
y_pred_test = np.round(y_pred_test)
test_acc = accuracy_score(y_test, y_pred_test)
return 0.5 * (train_acc + test_acc)
return calc_accuracy
def select_parents(self, fitness, n_parents):
fitness_values = np.array([fitness(individual) for individual in self.pop])
sorted_indices = np.argsort(fitness_values)[::-1]
selected_indices = sorted_indices[:n_parents]
return [self.pop[i] for i in selected_indices]
def mutate(self, individual):
for i in range(len(individual)):
if np.random.rand() < self.mutation_rate:
individual[i] += np.random.randn()
return individual
def crossover(self, parent1, parent2):
child1 = np.copy(parent1)
child2 = np.copy(parent2)
for i in range(len(parent1)):
if np.random.rand() < 0.5:
child1[i] = parent2[i]
child2[i] = parent1[i]
return child1, child2
def elitism(self, fitness, n_elites):
fitness_values = np.array([fitness(individual) for individual in self.pop])
sorted_indices = np.argsort(fitness_values)[::-1]
elite_indices = sorted_indices[:n_elites]
elite_individuals = [self.pop[i] for i in elite_indices]
return elite_individuals
def generate_clones(self, individual):
clones = []
for i in range(self.num_clones):
clone = np.copy(individual)
for j in range(len(clone)):
clone[j] += np.random.randn()
clones.append(clone)
return clones
def select_immunes(self, fitness, n_immunes):
fitness_values = np.array([fitness(individual) for individual in self.pop])
sorted_indices = np.argsort(fitness_values)
selected_indices = sorted_indices[:n_immunes]
return [self.pop[i] for i in selected_indices]
def clone_selection(self, fitness, clones):
fitness_values = np.array([fitness(individual) for individual in clones])
sorted_indices = np.argsort(fitness_values)[::-1]
selected_indices = sorted_indices[:int(self.clone_rate*len(clones))]
return [clones[i] for i in selected_indices]
def run(self, X_train, y_train, X_test, y_test):
n_genes = X_train.shape[1]*10 + 10 + 10*1 + 1
self.init_pop(n_genes)
fitness = self.fitness_func(X_train, y_train, X_test, y_test)
for gen in range(self.max_gen):
parents = self.select_parents(fitness, n_parents=int(0.5*self.pop_size))
offsprings = []
for i in range(int(0.5*self.pop_size)):
parent1 = parents[np.random.randint(len(parents))]
parent2 = parents[np.random.randint(len(parents))]
child1, child2 = self.crossover(parent1, parent2)
child1 = self.mutate(child1)
child2 = self.mutate(child2)
offsprings.append(child1)
offsprings.append(child2)
elites = self.elitism(fitness, n_elites=int(self.eli_rate*self.pop_size))
self.pop = offsprings + elites
immunes = self.select_immunes(fitness, n_immunes=self.num_immunes)
clones = []
for immune in immunes:
clones += self.generate_clones(immune)
clones = self.clone_selection(fitness, clones)
self.pop += clones
if len(self.pop) > self.pop_size:
self.pop = self.pop[:self.pop_size]
print("Generation:", gen, "Best Fitness:", fitness(self.elitism(fitness, n_elites=1)[0]))
return self.elitism(fitness, n_elites=1)[0]
# 测试代码
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
iga = IGA(pop_size=100, max_gen=50, mutation_rate=0.1, eli_rate=0.1, num_immunes=10, num_clones=5, clone_rate=0.5)
best_individual = iga.run(X_train, y_train, X_test, y_test)
nn = BPNN(n_input=X_train.shape[1], n_hidden=10, n_output=1)
nn.W1 = best_individual[:nn.n_input*nn.n_hidden].reshape(nn.n_input, nn.n_hidden)
nn.b1 = best_individual[nn.n_input*nn.n_hidden:nn.n_input*nn.n_hidden+nn.n_hidden]
nn.W2 = best_individual[nn.n_input*nn.n_hidden+nn.n_hidden:nn.n_input*nn.n_hidden+nn.n_hidden*nn.n_output+nn.n_output].reshape(nn.n_hidden, nn.n_output)
nn.b2 = best_individual[nn.n_input*nn.n_hidden+nn.n_hidden*nn.n_output+nn.n_output:]
y_pred_train = nn.forward(X_train)
y_pred_train = np.round(y_pred_train)
train_acc = accuracy_score(y_train, y_pred_train)
y_pred_test = nn.forward(X_test)
y_pred_test = np.round(y_pred_test)
test_acc = accuracy_score(y_test, y_pred_test)
print("Training Accuracy:", train_acc)
print("Testing Accuracy:", test_acc)
```
其中,BP神经网络的代码已经实现,可以直接调用。免疫遗传算法的代码框架包括以下几个步骤:
1. 初始化种群
2. 定义适应度函数(这里使用BP神经网络的精度作为适应度)
3. 选择父代个体
4. 交叉和变异产生后代
5. 精英保留
6. 选择免疫个体
7. 克隆免疫个体
8. 选择克隆个体
9. 更新种群
10. 重复步骤3-9,直到达到最大迭代次数
最终输出训练集和测试集的准确率。
阅读全文