keras卷积神经网络+mnist数据集

模型预构建

在这里插入图片描述

数据集加载及预处理

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深度学习人工智能实践应用 林大贵著》
笔者已将本博客代码分享在:码云

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夺笋123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值