文章目录
- 1.引入库
- 2.加载数据
- 3.数据处理
- 4.构建网络
- 5.模型编译
- 6.模型训练
- 7.显示训练结果
- 8.模型测试
- 9.模型保存
1.引入库
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,Activation,BatchNormalization
from tensorflow.keras import utils
from tensorflow.keras import regularizers
from tensorflow.keras.datasets import mnist
2.加载数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train)
# 显示数据
plt.figure()
# 显示x_train中索引为1的图片
plt.imshow(x_train[1], cmap='gray')
print(y_train[1])
3.数据处理
# 数据维度的调整
x_train = x_train.reshape(60000,784)
x_test = x_test.reshape(10000,784)
# 数据类型调整
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# 归一化
x_train = x_train/255
x_test = x_test/255
# 将目标值转换成热编码的形式 分成10类 0-9
y_train = utils.to_categorical(y_train, 10)
print(y_train)
y_test = utils.to_categorical(y_test, 10)
print(y_test)
4.构建网络
# 搭建神经网络模型
model = Sequential()
# 全连接层:2个隐层,一个输出层
# 第一个隐层: 512个神经元
model.add(Dense(512,activation='relu',input_shape=(784,)))
model.add(BatchNormalization())
model.add(Dropout(0.2))
# 第二个隐层 同时加入批标准化和DropOut防止过拟合
model.add(Dense(512,activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model.add(BatchNormalization())
model.add(Dropout(0.2))
# 输出层
model.add(Dense(10,activation='softmax'))
# 显示网络框架
model.summary()
## 或者以下方式显示网络框架
# utils.plot_model(model)
5.模型编译
# 损失函数,优化器,评价指标
model.compile(loss=tf.keras.losses.categorical_crossentropy, optimizer='Adam',
metrics = tf.keras.metrics.Accuracy())
6.模型训练
# 使用fit,指定训练集,epochs,batch_size,val,verbose
history = model.fit(x_train, y_train,epochs=4,batch_size=128,validation_data=(x_test,y_test),verbose=1)
7.显示训练结果
print(history.history)
# 绘制图像
# 损失函数
plt.figure()
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'],label='val')
plt.legend()
plt.grid()
# 准确率
plt.figure()
plt.plot(history.history['accuracy'], label='train')
plt.plot(history.history['val_accuracy'],label='val')
plt.legend()
plt.grid()
8.模型测试
score = model.evaluate(x_test, y_test, verbose=1)
9.模型保存
model.save('model_mnist.h5')
# 模型加载
# 加载
loadmodel = tf.keras.models.load_model('model_mnist.h5')
loadmodel.evaluate(x_test, y_test, verbose=1)