基于对卷积神经网络有一定的认识
参考教材《TensorFlow+Keras深度学习与人工智能实践应用》
编码环境:jupyter notebook
(1)导入所需模块
import numpy as np
from keras.utils import np_utils # 后续将label转为一位热编码
from keras.datasets import mnist # 导入mnist数据集
np.random.seed(10) # 第10批随机种子
(2)数据预处理
# 数据预处理
# 接收mnist数据
(x_train_image,y_train_label),(x_test_image,y_test_label) = mnist.load_data()
# 将image数据reshape 成为28*28*1的图像 类型为float32 x_train_image.shape[0]为image的项数
x_train4D_image = x_train_image.reshape(x_train_image.shape[0],28,28,1).astype('float32')
x_test4D_image = x_test_image.reshape(x_test_image.shape[0],28,28,1).astype('float32')
# 标准化 [0,1]之间
x_train4D_normalize = x_train4D_image / 255
x_test4D_normalize = x_test4D_image / 255
# 将标签转为一位热编码
y_train_label_OneHot = np_utils.to_categorical(y_train_label)
y_test_label_OneHot = np_utils.to_categorical(y_test_label)
(3)创建卷积结构
# 导入相关库
from keras.models import Sequential # 模型库
from keras.layers import Conv2D,MaxPooling2D,Dense,Dropout,Flatten # 导入对应卷积、最大池化、Dropout、平坦和一般神经元库
model = Sequential() # 初始化
# 添加卷积层 滤波数16,卷积核大小5*5 输入shape 采用零填充法 relu激活函数
model.add(Conv2D(filters=16,kernel_size=(5,5),input_shape=(28,28,1),padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2))) # 池化大小 2*2
# 再添加一层卷积池化操作
model.add(Conv2D(filters=36,kernel_size=(5,5),padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2))) # 池化大小 2*2
model.add(Dropout(0.3))
# 平坦 全连接成一维 后面和多层感知器模型类似
model.add(Flatten())
model.add(Dense(128,activation='relu')) # 隐层神经元的数目
model.add(Dropout(0.5))
model.add(Dense(10,activation='softmax'))
(4)进行训练
# 定义训练方式
# 交叉熵损失函数 adam优化器 accuracy作为评判指标
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
# validation_split 作为验证集的百分比 80%训练 20%验证 10个训练周期 每批次300 verbose=2 显示训练过程
train_histroy=model.fit(x_train4D_normalize,y_train_label_OneHot,validation_split=0.2,
epochs=10,batch_size=300,verbose=2)
展示训练过程的准确率与损失函数的变化
import matplotlib.pyplot as plt
def show_train_history(train_history,train,validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('Train History')
plt.xlabel('epochs')
plt.ylabel(train)
plt.legend(['train','validation'],loc='upper left')
plt.show()
show_train_history(train_histroy,'accuracy','val_accuracy')
show_train_history(train_histroy,'loss','val_loss')
(5)进行预测
# 进行预测 tensorFlow2.6以上 以下用predict_classes()一个方法即可
predict_x = model.predict(x_test4D_normalize)
prediction = np.argmax(predict_x,axis=1) # 取列方向的最大值
prediction[:10]
显示前10项预测结果
def plot_images_labels_prediction(images,labels,prediction,idx,num=10):
fig = plt.gcf()
fig.set_size_inches(12,14)
if num > 25:
num = 25
for i in range(num):
ax = plt.subplot(5,5,i+1)
ax.imshow(images[idx])
title = 'label=' + str(labels[idx])
if len(prediction) > 0:
title += ',prediction' + str(prediction[idx])
ax.set_title(title)
ax.set_xticks([]);ax.set_yticks([])
idx += 1
plt.show()
plot_images_labels_prediction(x_test_image,y_test_label,prediction,0)
(6)显示混淆矩阵
import pandas as pd
pd.crosstab(y_test_label,prediction,rownames=['labels'],colnames=['prediction']) # 横轴标签 纵轴预测值