keras框架的CNN手写数字识别MNIST

参考:林大贵.TensorFlow+Keras深度学习人工智能实践应用[M].北京:清华大学出版社,2018.

首先在命令行中写入 activate tensorflow和jupyter notebook,运行如下代码。当然,事先准备好MNIST数据集。

 # coding: utf-8

 # In[4]:

 from keras.datasets import mnist
from keras.utils import np_utils
import numpy as np
np.random.seed(10) # In[5]: (x_train,y_train),(x_test,y_test)=mnist.load_data() # In[6]: 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') # In[7]: x_train4d_normalize = x_train4d/255
x_test4d_normalize = x_test4d/255 # In[8]: y_train_oneHot = np_utils.to_categorical(y_train)
y_test_oneHot = np_utils.to_categorical(y_test) # In[9]: from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPooling2D # In[10]: model = Sequential() # In[11]: model.add(Conv2D(filters = 16,
kernel_size = (5,5),
padding = 'same',
input_shape = (28,28,1),
activation = ('relu')
)) # In[12]: model.add(MaxPooling2D(pool_size=(2,2))) # In[13]: model.add(Conv2D(filters = 36,
kernel_size = (5,5),
padding = 'same',
activation = 'relu')) # In[14]: model.add(MaxPooling2D(pool_size=(2,2))) # In[15]: model.add(Dropout(0,255)) # In[16]: model.add(Flatten()) # In[17]: model.add(Dense(128,activation = 'relu')) # In[18]: model.add(Dropout(0.5)) # In[19]: model.add(Dense(10,activation = 'sigmoid')) # In[20]: print(model.summary()) # In[21]: model.compile(loss='categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy']) # In[22]: train_history = model.fit(x = x_train4d_normalize,
y = y_train_oneHot,
validation_split = 0.2,
epochs = 10,
batch_size = 300,
verbose = 2) # In[23]: import matplotlib.pyplot as plt
def show_train_history(train_history,train,validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('Train_History')
plt.ylabel(train)
plt.xlabel('Epoch')
plt.legend(['train','validation'], loc = 'upper left')
plt.show() # In[24]: show_train_history(train_history,'acc','val_acc') # In[25]: show_train_history(train_history,'loss','val_loss') # In[26]: scores = model.evaluate(x_test4d_normalize,y_test_oneHot)
scores[1] # In[27]: def plot_image_labels_prediction(images,labels,prediction,idx,num=10):
fig = plt.gcf()
fig.set_size_inches(12,24)
if num>50 : num = 50
for i in range(0,num):
ax = plt.subplot(10,5,1+i)
ax.imshow(images[idx],cmap='binary')
title = "lable="+str(labels[idx])
if len(prediction)>0:
title+=",predict="+str(prediction[idx])
ax.set_title(title,fontsize=10)
ax.set_xticks([]);ax.set_yticks([])
idx+=1
plt.show() # In[28]: prediction = model.predict_classes(x_test4d_normalize) # In[29]: plot_image_labels_prediction(x_test,
y_test,
prediction,
0,
50) # In[30]: import pandas as pd # In[31]: pd.crosstab(y_test,
prediction,
rownames=['label'],
colnames=['predict']) # In[ ]:

卷积神经网络简介:https://www.cnblogs.com/bai2018/p/10413889.html

产生如下神经网络:

keras框架的CNN手写数字识别MNIST

其中加入的dropout层用于避免过度拟合。在每次迭代时,随机舍弃一部分的训练样本。

训练效果较好。

keras框架的CNN手写数字识别MNIST

林大贵.TensorFlow+Keras深度学习人工智能实践应用[M].北京:清华大学出版社,2018.

上一篇:android 动态控制状态栏显示和隐藏


下一篇:IOS 状态栏 显示与隐藏网络活动状态