用卷积神经网络实现手写字体的识别

代码如下所示:
# -*- coding: utf-8 -*-
# @Time    : 2018/4/4 13:22
# @Author  : mgliu
# @FileName: mnist.py
# @Software: PyCharm Community Edition
# -*- coding: utf-8 -*-

import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten
from keras.layers.convolutional import Conv2D,MaxPooling2D
(X_train,y_train),(X_test,y_test)=mnist.load_data()
print(X_train.shape)
print(y_train[0])
X_train=X_train.reshape(X_train.shape[0],28,28,1).astype('float32')
X_test=X_test.reshape(X_test.shape[0],28,28,1).astype('float')
X_train/=255
X_test/=255
#把标签用one-hot 从新编码
def tran_y(y):
    y_ohe=np.zeros(10)
    y_ohe[y]=1
    return y_ohe
y_train_ohe=np.array([tran_y(y_train[i])for i in range(len(y_train))])
y_test_ohe=np.array([tran_y(y_test[i])for i in range(len(y_test))])

#搭建卷积神经网络
model=Sequential()
model.add(Conv2D(filters=64,kernel_size=(3,3),strides=(1,1),padding='same',input_shape=(28,28,1),activation='relu'))
#添加MaxPooling2D,在2X2的格子中取最大值
model.add(MaxPooling2D(pool_size=(2,2)))
#设立Dropout层,常用的值为0.2,0.3,0.5
model.add(Dropout(0.5))
#重复构造,搭建深度网络
model.add(Conv2D(128,kernel_size=(3,3),strides=(1,1),padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
model.add(Conv2D(256,kernel_size=(3,3),strides=(1,1),padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))
#把当前节点铺平
model.add(Flatten())
#构建全连接层
model.add(Dense(128,activation='relu'))
model.add(Dense(64,activation='relu'))
model.add(Dense(32,activation='relu'))
model.add(Dense(10,activation='softmax'))
#定义损失函数,一般分类问题的损失函数选择Cross Entropy
model.compile(loss='categorical_crossentropy',optimizer='adagrad',metrics=['acc','mae'])
model.fit(X_train,y_train_ohe,validation_data=(X_test,y_test_ohe),epochs=20,batch_size=128)
#评估
scores=model.evaluate(X_test,y_test_ohe,verbose=0)

实验结果:
 
  
Train on 60000 samples, validate on 10000 samples
Epoch 1/20
2018-04-04 14:06:59.503767: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
60000/60000 [==============================] - 91s 2ms/step - loss: 0.4059 - acc: 0.8668 - mean_absolute_error: 0.0367 - val_loss: 0.0674 - val_acc: 0.9788 - val_mean_absolute_error: 0.0076
Epoch 2/20
60000/60000 [==============================] - 50s 841us/step - loss: 0.1079 - acc: 0.9669 - mean_absolute_error: 0.0105 - val_loss: 0.0469 - val_acc: 0.9844 - val_mean_absolute_error: 0.0055
Epoch 3/20
60000/60000 [==============================] - 71s 1ms/step - loss: 0.0840 - acc: 0.9741 - mean_absolute_error: 0.0082 - val_loss: 0.0334 - val_acc: 0.9897 - val_mean_absolute_error: 0.0040
Epoch 4/20
60000/60000 [==============================] - 50s 825us/step - loss: 0.0688 - acc: 0.9787 - mean_absolute_error: 0.0068 - val_loss: 0.0291 - val_acc: 0.9902 - val_mean_absolute_error: 0.0035
Epoch 5/20
60000/60000 [==============================] - 49s 814us/step - loss: 0.0607 - acc: 0.9810 - mean_absolute_error: 0.0060 - val_loss: 0.0266 - val_acc: 0.9902 - val_mean_absolute_error: 0.0031
Epoch 6/20
60000/60000 [==============================] - 48s 798us/step - loss: 0.0559 - acc: 0.9829 - mean_absolute_error: 0.0055 - val_loss: 0.0246 - val_acc: 0.9913 - val_mean_absolute_error: 0.0029
Epoch 7/20
60000/60000 [==============================] - 46s 774us/step - loss: 0.0523 - acc: 0.9839 - mean_absolute_error: 0.0052 - val_loss: 0.0220 - val_acc: 0.9926 - val_mean_absolute_error: 0.0026
Epoch 8/20
60000/60000 [==============================] - 51s 851us/step - loss: 0.0467 - acc: 0.9861 - mean_absolute_error: 0.0046 - val_loss: 0.0214 - val_acc: 0.9921 - val_mean_absolute_error: 0.0025
Epoch 9/20
60000/60000 [==============================] - 49s 813us/step - loss: 0.0459 - acc: 0.9858 - mean_absolute_error: 0.0046 - val_loss: 0.0211 - val_acc: 0.9926 - val_mean_absolute_error: 0.0024
Epoch 10/20
60000/60000 [==============================] - 50s 841us/step - loss: 0.0425 - acc: 0.9867 - mean_absolute_error: 0.0042 - val_loss: 0.0199 - val_acc: 0.9935 - val_mean_absolute_error: 0.0023
Epoch 11/20
60000/60000 [==============================] - 48s 798us/step - loss: 0.0419 - acc: 0.9866 - mean_absolute_error: 0.0042 - val_loss: 0.0191 - val_acc: 0.9933 - val_mean_absolute_error: 0.0022
Epoch 12/20
60000/60000 [==============================] - 46s 762us/step - loss: 0.0398 - acc: 0.9878 - mean_absolute_error: 0.0040 - val_loss: 0.0188 - val_acc: 0.9933 - val_mean_absolute_error: 0.0021
Epoch 13/20
60000/60000 [==============================] - 49s 821us/step - loss: 0.0373 - acc: 0.9885 - mean_absolute_error: 0.0037 - val_loss: 0.0182 - val_acc: 0.9936 - val_mean_absolute_error: 0.0021
Epoch 14/20
60000/60000 [==============================] - 50s 833us/step - loss: 0.0360 - acc: 0.9886 - mean_absolute_error: 0.0037 - val_loss: 0.0177 - val_acc: 0.9940 - val_mean_abslute_error: 0.0020
Epoch 15/20
60000/60000 [==============================] - 48s 798us/step - loss: 0.0349 - acc: 0.9891 - mean_absolute_error: 0.0035 - val_loss: 0.0172 - val_acc: 0.9937 - val_mean_abslute_error: 0.0020
Epoch 16/20
60000/60000 [==============================] - 49s 821us/step - loss: 0.0327 - acc: 0.9901 - mean_absolute_error: 0.0033 - val_loss: 0.0165 - val_acc: 0.9939 - val_mean_abslute_error: 0.0019
Epoch 17/20
60000/60000 [==============================] - 48s 804us/step - loss: 0.0322 - acc: 0.9896 - mean_absolute_error: 0.0033 - val_loss: 0.0166 - val_acc: 0.9937 - val_mean_abslute_error: 0.0019
Epoch 18/20
60000/60000 [==============================] - 46s 763us/step - loss: 0.0318 - acc: 0.9897 - mean_absolute_error: 0.0033 - val_loss: 0.0170 - val_acc: 0.9940 - val_mean_abslute_error: 0.0019
Epoch 19/20
60000/60000 [==============================] - 51s 856us/step - loss: 0.0311 - acc: 0.9899 - mean_absolute_error: 0.0031 - val_loss: 0.0167 - val_acc: 0.9938 - val_mean_abslute_error: 0.0019
Epoch 20/20
60000/60000 [==============================] - 51s 851us/step - loss: 0.0295 - acc: 0.9906 - mean_absolute_error: 0.0031 - val_loss: 0.0166 - val_acc: 0.9942 - val_mean_abslute_error: 0.0019

你可能感兴趣的:(卷积神经网络)