Keras卷积神经网络识别手写数字

该博客介绍了如何利用Keras库构建卷积神经网络(CNN)来识别MNIST数据集的手写数字。首先进行数据预处理,包括读取数据、转换特征和标签编码。接着,构建了包含卷积层、池化层和Dropout的CNN模型,并进行了训练,展示训练的准确率和损失。然后,评估模型的准确率并进行预测,最后展示了预测的前10项结果和混淆矩阵。
摘要由CSDN通过智能技术生成

一、进行数据预处理

步骤1:导入所需模块

import tensorflow as tf
import tensorflow.keras
from tensorflow.keras.datasets import mnist
import numpy as np
import pandas as pd
from tensorflow.keras.utils import to_categorical
np.random.seed(10)

步骤2:读取MNIST数据

(x_train, y_train), \
(x_test, y_test) = mnist.load_data()

步骤3:将features(数字图像特征值)转换为四维矩阵
将features(数字图像特征值)以reshape转换为600002828*1的四维矩阵

x_train4d=x_train.reshape(x_train.shape[0],28,28,1).astype('float32')
x_test4d=x_test.reshape(x_test.shape[0],28,28,1).astype('float32')

步骤4:将features(数字图像特征值)标准化

x_train4d_normalize = x_train4d/255
x_test4d_normalize = x_test4d/255

步骤5:label(数字真实的值)以One-Hot Encoding进行转换

y_trainonehot =tf.keras.utils. to_categorical(y_train)
y_testonehot = tf.keras.utils.to_categorical(y_test)

二、 建立模型

1.导入所需模块

from tensorflow.keras.models impor
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值