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

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

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()

【python-keras深度学习-基本卷积神经网络mnist数字识别】_第1张图片
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])

【python-keras深度学习-基本卷积神经网络mnist数字识别】_第2张图片
运行结束,可以查看训练的准确率为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])

你可能感兴趣的:(深度学习,python,keras)