基於Keras的MNIST手寫識別

Keras是基於Theano和Tensorflow的深度學習框架,參考文檔python

Keras不但提供了經常使用的Layers、Normalization、Regularation、Activation等算法,甚至還包括了幾個經常使用的數據庫例如cifar-10和mnist等等。算法

下面的代碼算是Keras的Helloworld入門例程,利用MLP實現的MNIST手寫數字識別:數據庫

from keras.models import Sequential  
from keras.layers.core import Dense, Dropout, Activation  
from keras.optimizers import SGD  
from keras.datasets import mnist  
import numpy

model = Sequential()  
model.add(Dense(784, 500, init='glorot_uniform')) # 輸入層,28*28=784  
model.add(Activation('tanh')) # 激活函數是tanh  
model.add(Dropout(0.5)) # 採用50%的dropout

model.add(Dense(500, 500, init='glorot_uniform')) # 隱層節點500個  
model.add(Activation('tanh'))  
model.add(Dropout(0.5))

model.add(Dense(500, 10, init='glorot_uniform')) # 輸出結果是10個類別,因此維度是10  
model.add(Activation('softmax')) # 最後一層用softmax

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) # 設定學習率(lr)等參數  
model.compile(loss='categorical_crossentropy', optimizer=sgd, class_mode='categorical') # 使用交叉熵做爲loss函數

(X_train, y_train), (X_test, y_test) = mnist.load_data() # 使用Keras自帶的mnist工具讀取數據(第一次須要聯網)

X_train = X_train.reshape(X_train.shape[0], X_train.shape[1] * X_train.shape[2]) # 因爲mist的輸入數據維度是(num, 28, 28),這裏須要把後面的維度直接拼起來變成784維  
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1] * X_test.shape[2])  
Y_train = (numpy.arange(10) == y_train[:, None]).astype(int) # 參考上一篇文章,這裏須要把index轉換成一個one hot的矩陣  
Y_test = (numpy.arange(10) == y_test[:, None]).astype(int)

# 開始訓練,這裏參數比較多。batch_size就是batch_size,nb_epoch就是最多迭代的次數, shuffle就是是否把數據隨機打亂以後再進行訓練
# verbose是屏顯模式,官方這麼說的:verbose: 0 for no logging to stdout, 1 for progress bar logging, 2 for one log line per epoch.
# 就是說0是不屏顯,1是顯示一個進度條,2是每一個epoch都顯示一行數據
# show_accuracy就是顯示每次迭代後的正確率
# validation_split就是拿出百分之多少用來作交叉驗證
model.fit(X_train, Y_train, batch_size=200, nb_epoch=100, shuffle=True, verbose=1, show_accuracy=True, validation_split=0.3)  
print 'test set'  
model.evaluate(X_test, Y_test, batch_size=200, show_accuracy=True, verbose=1)

輸出結果:框架

ssh://shibotian@***.***.***.***:22/usr/bin/python -u /usr/local/shared_dir/local/ipython_shibotian/shibotian/code/kreas_test1/run.py  
Using gpu device 0: Tesla K40m  
Train on 42000 samples, validate on 18000 samples  
Epoch 0  
0/42000 [==============================] - 1s - loss: 0.9894 - acc.: 0.7386 - val. loss: 0.4795 - val. acc.: 0.8807  
Epoch 1  
0/42000 [==============================] - 1s - loss: 0.5635 - acc.: 0.8360 - val. loss: 0.4084 - val. acc.: 0.8889

省略。。。。。

Epoch 98  
0/42000 [==============================] - 1s - loss: 0.2838 - acc.: 0.9116 - val. loss: 0.1872 - val. acc.: 0.9418  
Epoch 99  
0/42000 [==============================] - 1s - loss: 0.2740 - acc.: 0.9163 - val. loss: 0.1842 - val. acc.: 0.9434  
test set  
0/10000 [==============================] - 0s - loss: 0.1712 - acc.: 0.9480     

Process finished with exit code 0
相關文章
相關標籤/搜索