python利用遗传算法优化神经网络模型的代码
时间: 2023-09-06 14:09:19 浏览: 109
遗传算法(Genetic Algorithm)是模拟自然进化过程的一种优化算法,适用于优化复杂的非线性问题。在神经网络中,遗传算法可以用于优化神经网络的结构和参数,以达到更好的学习效果。
下面是一个使用遗传算法优化神经网络模型的 Python 代码示例:
```
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# 加载数据集
iris = load_iris()
X = iris.data
y = iris.target
# 将类别变量转换为二进制形式
y_binary = to_categorical(y)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y_binary, test_size=0.2, random_state=42)
# 定义神经网络模型结构
def create_model(input_dim, output_dim, nodes, n_layers):
model = Sequential()
model.add(Dense(nodes, input_dim=input_dim, activation='relu'))
for i in range(n_layers-1):
model.add(Dense(nodes, activation='relu'))
model.add(Dense(output_dim, activation='softmax'))
return model
# 定义适应度函数
def fitness(model, X_train, y_train, X_test, y_test):
model.fit(X_train, y_train, epochs=100, batch_size=10, verbose=0)
_, accuracy = model.evaluate(X_test, y_test, verbose=0)
return accuracy
# 定义遗传算法参数
pop_size = 20 # 种群大小
n_generations = 50 # 迭代次数
mutation_rate = 0.1 # 变异率
n_layers_range = [1, 3] # 神经网络层数范围
nodes_range = [5, 20] # 神经元数量范围
# 初始化种群
pop = []
for i in range(pop_size):
n_layers = np.random.randint(n_layers_range[0], n_layers_range[1]+1)
nodes = np.random.randint(nodes_range[0], nodes_range[1]+1)
model = create_model(X_train.shape[1], y_binary.shape[1], nodes, n_layers)
pop.append(model)
# 迭代优化
for i in range(n_generations):
# 计算适应度
fitness_scores = []
for j in range(pop_size):
fitness_scores.append(fitness(pop[j], X_train, y_train, X_test, y_test))
# 选择
parents = np.random.choice(pop, size=pop_size//2, replace=False, p=np.array(fitness_scores)/sum(fitness_scores))
# 交叉
children = []
for j in range(pop_size//2):
parent1 = parents[j]
parent2 = parents[pop_size//2-1-j]
child1 = create_model(X_train.shape[1], y_binary.shape[1], parent1.layers[0].output_shape[1], len(parent1.layers)-1)
child2 = create_model(X_train.shape[1], y_binary.shape[1], parent2.layers[0].output_shape[1], len(parent2.layers)-1)
for k in range(len(parent1.layers)-1):
if np.random.rand() > 0.5:
child1.layers[k].set_weights(parent1.layers[k].get_weights())
child2.layers[k].set_weights(parent2.layers[k].get_weights())
else:
child1.layers[k].set_weights(parent2.layers[k].get_weights())
child2.layers[k].set_weights(parent1.layers[k].get_weights())
children += [child1, child2]
# 变异
for j in range(len(children)):
if np.random.rand() < mutation_rate:
n_layers = np.random.randint(n_layers_range[0], n_layers_range[1]+1)
nodes = np.random.randint(nodes_range[0], nodes_range[1]+1)
model = create_model(X_train.shape[1], y_binary.shape[1], nodes, n_layers)
children[j] = model
# 更新种群
pop = parents + children
# 输出最优解
fitness_scores = []
for j in range(pop_size):
fitness_scores.append(fitness(pop[j], X_train, y_train, X_test, y_test))
best_model = pop[np.argmax(fitness_scores)]
print('Best accuracy:', max(fitness_scores))
print('Best model:')
best_model.summary()
```
以上代码中,首先加载 Iris 数据集,并将类别变量转换为二进制形式。接着定义神经网络模型结构和适应度函数。然后定义遗传算法参数,包括种群大小、迭代次数、变异率、神经网络层数范围和神经元数量范围。然后初始化种群,并进行迭代优化过程。在每次迭代中,先计算种群中每个个体的适应度,然后进行选择、交叉和变异操作,最后更新种群。最终输出最优解,即最高的测试集准确率和对应的神经网络模型结构。
需要注意的是,该代码使用了 Keras 框架来构建神经网络模型,因此需要先安装 Keras 和相关依赖库。如果没有安装,可以通过以下命令进行安装:
```
pip install keras tensorflow scikit-learn numpy
```
阅读全文