模型预构建
数据集加载及预处理
from tensorflow.keras.datasets import mnist
from tensorflow.python.keras.utils import np_utils
import numpy as np
(x_train,y_train),(x_test,y_test)=mnist.load_data()
# 将训练集和测试集数据的形状进行转换
x_train4d=x_train.reshape(60000,28,28,1).astype('float32')/255
x_test4d=x_test.reshape(10000,28,28,1).astype('float32')/255
上面的数据经过转换后变为了一个全新的数据形状,拿x_train举例,60000指的是x_train中包含的数据数量,因为mnist的原始数据是一张张手写图片,所以(28,28)指的是每张图片的长宽,单位是像素。最后的1指的是每张图片只有一个维度(因为mnist的图片是灰白的,彩色的图片通常有RGB三个维度)
上面数据除以255是为了将像素值进行标准化,便于模型的收敛
y_train_onehot=np_utils.to_categorical(y_train)
y_test_onehot=np_utils.to_categorical(y_test)
y_train_onehot.shape,y_test_onehot.shape
>>> ((60000, 10), (10000, 10))
np_utils.to_categroical()函数将标签值转换为ont-hot向量
线性模型构建
导入模块
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten,Conv2D,MaxPooling2D,Dropout
model=Sequential()
# 卷积层1
model.add(Conv2D(filters=16,kernel_size=(5,5),padding='same',
input_shape=(28,28,1),activation='relu'))
# 池化层1
model.add(MaxPooling2D(pool_size=(2,2)))
# 卷积层2
model.add(Conv2D(filters=36,kernel_size=(5,5),padding='same',
input_shape=(28,28,1),activation='relu'))
# 池化层2
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
# 展开层
model.add(Flatten())
# 全连接层(隐藏层)
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.25))
# 输出层
model.add(Dense(10,activation='softmax'))
模型编译及拟合
model.compile(loss='categorical_crossentropy',optimizer='adam',
metrics=['accuracy'])
train_history=model.fit(x_train4d,y_train_onehot,validation_split=0.2,
epochs=10,batch_size=300,verbose=2)
运行结果
训练过程可视化
import matplotlib.pyplot as plt
# 定义可视化函数
def train_history_show(train_history,train,validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('train_history')
plt.xlabel('epoch')
plt.ylabel('train history')
plt.legend(['train','validation'],loc='upper left')
plt.show()
显示每个epoch训练结束后,模型准确率的变化情况
train_history_show(train_history,'accuracy','val_accuracy')
显示每个epoch结束后,模型损失变化情况
train_history_show(train_history,'loss','val_loss')
观察模型评分
score=model.evaluate(x_test4d,y_test_onehot)
score
运行结果
使用模型预测及可视化
prediction=np.argmax(model.predict(x_test4d),axis=-1)
prediction[:10]
>>> array([7, 2, 1, 0, 4, 1, 4, 9, 5, 9], dtype=int64)
定义可视化函数
def plot_image_label_prediction(images,labels,prediction,idx,num=10):
# 生成一个绘图区域
fig=plt.gcf()
# 以英寸为单位设置图形大小
fig.set_size_inches(12,14)
# 最多生成20张图片
if num>20:num=20
for i in range(0,num):
# 生成子绘图区域
ax=plt.subplot(5,5,1+i)
# 将数组的值以图片的形式展示出来
ax.imshow(images[idx],cmap='binary')
# 将实际值与模型预测值显示
title='label:'+str(labels[idx])
if len(prediction)>0:
title+='predict='+str(prediction[idx])
ax.set_title(title,fontsize=10)
# 设置x、y轴刻度
ax.set_xticks([])
ax.set_yticks([])
idx+=1
plt.show()
plot_image_label_prediction(x_test,y_test,prediction,idx=30)
运行结果
显示混淆矩阵
混淆矩阵是机器学习中总结分类模型预测结果的情形分析表,以矩阵形式将数据集中的记录按照真实的类别与分类模型预测的类别判断两个标准进行汇总
import pandas as pd
pd.crosstab(y_test,prediction,rownames=['label'],colnames=['predict'])
运行结果
在上面的混淆矩阵中我们看到,横轴为预测结果,纵轴为实际标签值。矩阵中间的对角线表明实际标签值与预测值不同或相同的个数。从上面混淆矩阵的结果来看,预测错误的个数很少,说明模型已经取得了不错的效果
本博客参考:《tensorflow+keras深度学习人工智能实践应用 林大贵著》
笔者已将本博客代码分享在:码云