ui设计做兼职的网站,自助贸易免费建站,app官方网站,多语言网站 自助文章目录任务查看当前的准确率情况使用遗传算法进行优化完整代码任务
使用启发式优化算法遗传算法对多层感知机中中间层神经个数进行优化#xff0c;以提高模型的准确率。
待优化的模型#xff1a; 基于TensorFlow2实现的Mnist手写数字识别多层感知机MLP
# MLP手写数字识别…
文章目录任务查看当前的准确率情况使用遗传算法进行优化完整代码任务
使用启发式优化算法遗传算法对多层感知机中中间层神经个数进行优化以提高模型的准确率。
待优化的模型 基于TensorFlow2实现的Mnist手写数字识别多层感知机MLP
# MLP手写数字识别模型待优化的参数为layer1、layer2
model tf.keras.Sequential([tf.keras.layers.Flatten(input_shape(28, 28, 1)), tf.keras.layers.Dense(layer1, activationrelu),tf.keras.layers.Dense(layer2, activationrelu),tf.keras.layers.Dense(10,activationsoftmax) # 对应0-9这10个数字
])查看当前的准确率情况
设置随机树种子避免相同结构的神经网络其结果不同的影响。
random_seed 10
np.random.seed(random_seed)
tf.random.set_seed(random_seed)
random.seed(random_seed)
model tf.keras.Sequential([tf.keras.layers.Flatten(input_shape(28, 28, 1)), tf.keras.layers.Dense(128, activationrelu),tf.keras.layers.Dense(32, activationrelu),tf.keras.layers.Dense(10,activationsoftmax) # 对应0-9这10个数字
])
optimizer tf.keras.optimizers.Adam()
loss_func tf.keras.losses.SparseCategoricalCrossentropy()
model.compile(optimizeroptimizer,lossloss_func,metrics[acc])
history model.fit(dataset, validation_datatest_dataset, epochs5, verbose1)
score model.evaluate(test_dataset)
print(测试集准确率:,score) # 输出 [损失率准确率]Epoch 1/5
235/235 [] - 1s 5ms/step - loss: 0.4663 - acc: 0.8703 - val_loss: 0.2173 - val_acc: 0.9361
Epoch 2/5
235/235 [] - 1s 4ms/step - loss: 0.1882 - acc: 0.9465 - val_loss: 0.1604 - val_acc: 0.9521
Epoch 3/5
235/235 [] - 1s 4ms/step - loss: 0.1362 - acc: 0.9608 - val_loss: 0.1278 - val_acc: 0.9629
Epoch 4/5
235/235 [] - 1s 4ms/step - loss: 0.1084 - acc: 0.9689 - val_loss: 0.1086 - val_acc: 0.9681
Epoch 5/5
235/235 [] - 1s 4ms/step - loss: 0.0878 - acc: 0.9740 - val_loss: 0.1077 - val_acc: 0.9675
40/40 [] - 0s 2ms/step - loss: 0.1077 - acc: 0.9675
测试集准确率: [0.10773944109678268, 0.9674999713897705]准确率为96.7%
使用遗传算法进行优化
使用scikit-opt提供的遗传算法库进行优化。pip install scikit-opt 官方文档https://scikit-opt.github.io/scikit-opt/#/zh/README
# 遗传算法调用
ga GA(funcloss_func, n_dim2, size_pop4, max_iter3, prob_mut0.15, lb[10, 10], ub[256, 256], precision1)优化目标函数loss_func1 - 模型准确率求优化目标函数的最小值 待优化的参数数量n_dim2 种群数量size_pop4 迭代次数max_iter3 变异概率prob_mut0.15 待优化两个参数的取值范围均为10-256 精确度precision1
# 定义多层感知机模型函数
def mlp_model(layer1, layer2):model tf.keras.Sequential([tf.keras.layers.Flatten(input_shape(28, 28, 1)), tf.keras.layers.Dense(layer1, activationrelu),tf.keras.layers.Dense(layer2, activationrelu),tf.keras.layers.Dense(10,activationsoftmax) # 对应0-9这10个数字])optimizer tf.keras.optimizers.Adam()loss_func tf.keras.losses.SparseCategoricalCrossentropy()model.compile(optimizeroptimizer,lossloss_func,metrics[acc])return model# 定义损失函数
def loss_func(params):random_seed 10np.random.seed(random_seed)tf.random.set_seed(random_seed)random.seed(random_seed)layer1 int(params[0])layer2 int(params[1])model mlp_model(layer1, layer2)history model.fit(dataset, validation_datatest_dataset, epochs5, verbose0)return 1 - history.history[val_acc][-1]ga GA(funcloss_func, n_dim2, size_pop4, max_iter3, prob_mut0.15, lb[10, 10], ub[256, 256], precision1)
bestx, besty ga.run()
print(bestx:, bestx, \n, besty:, besty)结果
bestx: [165. 155.]
besty: [0.02310002]通过迭代找到layer1、layer2的最好值为165、155此时准确率为1-0.02310.9769。
查看每轮迭代的损失函数值图1-准确率
Y_history pd.DataFrame(ga.all_history_Y)
fig, ax plt.subplots(2, 1)
ax[0].plot(Y_history.index, Y_history.values, ., colorred)
Y_history.min(axis1).cummin().plot(kindline)
plt.show()上图为三次迭代种群中种群每个个体的损失函数值每个种群4个个体。 下图为三次迭代种群中种群个体中的最佳损失函数值。
可以看出通过遗传算法其准确率有一定的提升。
增加种群数量及迭代次数效果可更加明显。
完整代码
# python3.6.9
import tensorflow as tf # 2.3
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random
from sko.GA import GA# 数据导入获取训练集和测试集
(train_image, train_labels), (test_image, test_labels) tf.keras.datasets.mnist.load_data()# 增加通道维度
train_image tf.expand_dims(train_image, -1)
test_image tf.expand_dims(test_image, -1)# 归一化 类型转换
train_image tf.cast(train_image/255, tf.float32)
test_image tf.cast(test_image/255, tf.float32)
train_labels tf.cast(train_labels, tf.int64)
test_labels tf.cast(test_labels, tf.int64)# 创建Dataset
dataset tf.data.Dataset.from_tensor_slices((train_image, train_labels)).shuffle(60000).batch(256)
test_dataset tf.data.Dataset.from_tensor_slices((test_image, test_labels)).batch(256)# 定义多层感知机模型函数
def mlp_model(layer1, layer2):model tf.keras.Sequential([tf.keras.layers.Flatten(input_shape(28, 28, 1)), tf.keras.layers.Dense(layer1, activationrelu),tf.keras.layers.Dense(layer2, activationrelu),tf.keras.layers.Dense(10,activationsoftmax) # 对应0-9这10个数字])optimizer tf.keras.optimizers.Adam()loss_func tf.keras.losses.SparseCategoricalCrossentropy()model.compile(optimizeroptimizer,lossloss_func,metrics[acc])return model# 定义损失函数
def loss_func(params):random_seed 10np.random.seed(random_seed)tf.random.set_seed(random_seed)random.seed(random_seed)layer1 int(params[0])layer2 int(params[1])model mlp_model(layer1, layer2)history model.fit(dataset, validation_datatest_dataset, epochs5, verbose0)return 1 - history.history[val_acc][-1]ga GA(funcloss_func, n_dim2, size_pop4, max_iter3, prob_mut0.15, lb[10, 10], ub[256, 256], precision1)
bestx, besty ga.run()
print(bestx:, bestx, \n, besty:, besty)