【python-keras深度学习-基本卷积神经网络mnist数字识别】

该文介绍了如何利用Keras库构建一个简单的卷积神经网络(CNN)来识别MNIST数据集中的手写数字。首先,进行了数据预处理,包括数据归一化和one-hot编码。接着,创建了一个全连接网络模型,并用softmax激活函数进行多类别预测。然后,使用SGD优化器和交叉熵损失函数编译并训练了模型,最终在测试集上获得了约92%的准确率。
摘要由CSDN通过智能技术生成

搭建基本卷积神经网络进行数字识别

mnist数据集中有0-9共10个数字,如何使用卷积神经网络进行识别,除了keras封装好的函数外,还需要进行one-hot编码,将类别特征转化为数值变量,比如我要识别的数字为1,除了1的位置为1,其他9个位置则为0,如此就可以将类别问题转化为识别概率问题。

搭建步骤

1:库的导入
使用keras高级API进行网络搭建时,需要导入必须的包,比如keras,网络使用的模型model,激活函数,优化函数,矩阵计算包,网络层。

from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense,Activation
from keras.optimizers import SGD
from keras.utils import np_utils
np.random.seed(1671)

2.参数设置
需要设置网络的训练轮次,每次训练的批次数据,训练、验证比例等

#参数设置
epoch = 200
batch_size = 128
verbose = 1
NB_CLASSES = 10
OPTIMIZER = SGD()
hidden = 128
val = 0.2

3.数据处理
导入网络中的数据指定不是你要识别的图片,而是图像的矩阵数据(张量),所以需要进行数据处理和归一化,将标签数据转化为二值类别矩阵。

#数据处理
(x_train,y_train),(x_test,y_test) = mnist.load_data()
a=784

x_train = x_train.reshape(60000,a)
x_test = x_test.reshape(10000,a)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

x_train /= 255
x_test /=255

print(x_train.shape[0],'train samples')
print(x_test.shape[0],'test sammples')

y_train = np_utils.to_categorical(y_train,NB_CLASSES)
y_test = np_utils.to_categorical(y_test,NB_CLASSES)

4.模型的建立
数据准备好后,需要进行网络模型的搭建,其中可以按照自己的需求进行网络搭建,比如卷积、池化等。

model = Sequential()
model.add(Dense(NB_CLASSES,input_shape=(a,)))
model.add(Activation('softmax'))

5.查看网络结构,可以看到参数、计算量和网络结构

model.summary()

在这里插入图片描述
6.网络模型的编译和运行

#模型编译
model.compile(loss = 'categorical_crossentropy',optimizer = OPTIMIZER,metrics = ['accuracy'])

#运行设置
history = model.fit(x_train,y_train,batch_size = batch_size,epochs = epoch,verbose = verbose,validation_split = val)
score = model.evaluate(x_test,y_test,verbose = verbose)
print("test score:",score[0])
print('test accuracy',score[1])

在这里插入图片描述
运行结束,可以查看训练的准确率为0.9225

完整代码

from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense,Activation
from keras.optimizers import SGD
from keras.utils import np_utils
np.random.seed(1671)

#参数设置
epoch = 200
batch_size = 128
verbose = 1
NB_CLASSES = 10
OPTIMIZER = SGD()
hidden = 128
val = 0.2

#数据处理
(x_train,y_train),(x_test,y_test) = mnist.load_data()
a=784

x_train = x_train.reshape(60000,a)
x_test = x_test.reshape(10000,a)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

x_train /= 255
x_test /=255

print(x_train.shape[0],'train samples')
print(x_test.shape[0],'test sammples')

y_train = np_utils.to_categorical(y_train,NB_CLASSES)
y_test = np_utils.to_categorical(y_test,NB_CLASSES)

#模型建立
model = Sequential()
model.add(Dense(NB_CLASSES,input_shape=(a,)))
model.add(Activation('softmax'))
#查看网络结构
model.summary()
#模型编译
model.compile(loss = 'categorical_crossentropy',optimizer = OPTIMIZER,metrics = ['accuracy'])

#运行设置
history = model.fit(x_train,y_train,batch_size = batch_size,epochs = epoch,verbose = verbose,validation_split = val)
score = model.evaluate(x_test,y_test,verbose = verbose)
print("test score:",score[0])
print('test accuracy',score[1])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值