keras框架的CNN手写数字识别MNIST
2024-10-18 04:52:21
参考:林大贵.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
产生如下神经网络:
其中加入的dropout层用于避免过度拟合。在每次迭代时,随机舍弃一部分的训练样本。
训练效果较好。
林大贵.TensorFlow+Keras深度学习人工智能实践应用[M].北京:清华大学出版社,2018.
最新文章
- js学习笔记:操作iframe
- servlet 之request
- System.Data.OleDb.OleDbException: 未指定的错误的解决方法
- DataNode,NameNode,JobTracker,TaskTracker用jps查看无法启动解决办法
- Form – 保存自動關閉當前窗口
- C#反射的应用【转】
- java学习之三种常用设计模式
- [NOI2016]循环之美
- WebGL three.js学习笔记 纹理贴图模拟太阳系运转
- Java服务器内存过高&;CPU过高问题排查
- PM过程能力成熟度3级
- MT【322】绝对值不等式
- hadoop之数据压缩与数据格式
- AJ的笔记之上拉电阻的工作原理分析
- 学习使用github
- Nginx详解十六:Nginx场景实践篇之缓存服务
- 怎么样从多列的DataTable里取需要的几列
- 【原创 Hadoop&;Spark 动手实践 8】Spark 应用经验、调优与动手实践
- 【Java算法】冒泡排序
- Python 编码风格指南