1、默认安装了Keras(终端下输入pip install keras)、Jupyter Notebook和Pandas(pip install pandas)
关于Pandas的介绍,可以参考该篇文章点击打开链接
如果在安装过程中遇见timeout的问题的话,你只能多试几次或者“你(用)懂(神)得(器)”
2、打开Jupyter Notebook,选择New-Python3
3、重命名Notebook的名称
4、下载mnist资料档案
from keras.datasets import mnist
(x_train, y_train),(x_test,y_test) = mnist.load_data() #读取MNIST资料
第一次执行mnist.load_data() 方法,程序会检查使用者目录下,是否已有mnist资料集档案,如果还没有,就会下载档案,执行时间会比较久。下载后会储存在使用者个人资料夹。
5、然后开始码代码啦
# coding: utf-8
# In[1]:
#资料预处理
from keras.datasets import mnist
from keras.utils import np_utils
import numpy as np
np.random.seed(10)
# In[2]:
(x_train, y_train),(x_test,y_test) = mnist.load_data() #读取MNIST资料
# In[4]:
#将features(数字影像特征值)以reshape转换为6000*28*28*1的4维矩阵
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[5]:
#将features标准化,可以提高模型预测的准确度,并且更快收敛
x_train4D_normalize = x_train4D / 255
x_test4D_normalize = x_test4D / 255
# In[8]:
#使用np_utils.to_categorical, 将训练资料集与测试的label,进行 Onehot encoding 转换
y_trainOneHot = np_utils.to_categorical(y_train)
y_testOneHot = np_utils.to_categorical(y_test)
# In[10]:
#建立模型
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPooling2D
# In[11]:
#建立keras的Sequential模型(线性堆积模型),后续只需要使用model.add()方法,将各神经网络层加入模型即可
model = Sequential()
# In[13]:
#建立卷积层1.
#输入的数字影像是28*28大小,执行第一次卷积运算,会产生16个影像,卷积运算并不会改变影像大小,所以仍然是28*28大小。
model.add(Conv2D(filters=16,
kernel_size=(5,5),
padding='same',#补零
input_shape=(28,28,1),
activation='relu'))
# In[14]:
#建立池化层
#输入参数pool_size=(2,2),执行第一次缩减取样,将16个28*28影像,缩小为16个14*14的影像。
model.add(MaxPooling2D(pool_size=(2,2)))
# In[15]:
#建立卷积层2.
#输入的数字影像是28*28大小,执行第2次卷积运算,将原本16个的影像,转换为36个影像,卷积运算并不会改变影像大小,所以仍然是14*14大小。
model.add(Conv2D(filters=36,
kernel_size=(5,5),
padding='same',#补零
activation='relu'))
# In[16]:
#建立池化层2
#输入参数pool_size=(2,2),执行第2次缩减取样,将36个14*14影像,缩小为36个7*7的影像。
model.add(MaxPooling2D(pool_size=(2,2)))
# In[17]:
#加入Dropout(0.25)层至模型中。其功能是,每次训练迭代时,会随机的在神经网络中放弃25%的神经元,以避免overfitting。
model.add(Dropout(0.25))
# In[19]:
#建立平坦层
#之前的步骤已经建立池化层2,共有36个7*7影像,转换为1维的向量,长度是36*7*7=1764,也就是1764个float数字,正好对应到1764个神经元。
model.add(Flatten())
# In[21]:
#建立隐藏层,共有128个神经元
model.add(Dense(128,activation='relu'))
# In[22]:
#加入Dropout(0.5)层至模型中。其功能是,每次训练迭代时,会随机的在神经网络中放弃50%的神经元,以避免overfitting。
model.add(Dropout(0.5))
# In[23]:
#建立输出层
#共有10个神经元,对应到0-9共10个数字。并且使用softmax激活函数进行转换,softmax可以将神经元的输出,转换为预测每一个数字的几率。
model.add(Dense(10,activation='softmax'))
# In[24]:
#查看模型的摘要
print(model.summary())
# In[26]:
#进行训练
#定义训练方式
#在模型训练之前,我们必须使用compile方法,对训练模型进行设定
model.compile(loss='categorical_crossentropy',
optimizer='adam',metrics=['accuracy'])
# In[27]:
#开始训练
train_history=model.fit(x=x_train4D_normalize,
y=y_trainOneHot,validation_split=0.2,
epochs=10,batch_size=300,verbose=2)
# In[32]:
#画出accuracy执行结果
#之前训练步骤,会将每一个训练周期的accuracy与loss,记录在train_history。
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[35]:
show_train_history(train_history,'acc','val_acc')
# In[36]:
#画出loss误差执行结果
show_train_history(train_history,'loss','val_loss')
# In[37]:
#用test评估模型准确度
scores = model.evaluate(x_test4D_normalize,y_testOneHot)
scores[1]
# In[40]:
#进行预测
prediction=model.predict_classes(x_test4D_normalize)
# In[41]:
#预测结果
prediction[:10]
# In[54]:
#显示前10笔预测结果
def plot_images_labels_prediction(images,labels,prediction,idx,num=10):
fig = plt.gcf()
fig.set_size_inches(12,14)
if num>25:
num =25
for i in range(0,num):
ax = plt.subplot(5,5,1+i)
ax.imshow(images[idx], cmap='binary')
title = "label="+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[55]:
plot_images_labels_prediction(x_test,y_test,prediction,idx=0)
# In[56]:
#显示混淆矩阵
import pandas as pd
pd.crosstab(y_test,prediction,
rownames=['label'],colnames=['predict'])
这是我从jupyter notebook上码完后下载下来的,运行过,是没问题的。