前幾天用CNN識別手寫數(shù)字集,后來看到kaggle上有一個比賽是識別手寫數(shù)字集的,已經(jīng)進(jìn)行了一年多了,目前有1179個有效提交,最高的是100%,我做了一下,用keras做的,一開始用最簡單的MLP,準(zhǔn)確率只有98.19%,然后不斷改進(jìn),現(xiàn)在是99.78%,然而我看到排名第一是100%,心碎 = =,于是又改進(jìn)了一版,現(xiàn)在把最好的結(jié)果記錄一下,如果提升了再來更新。
手寫數(shù)字集相信大家應(yīng)該很熟悉了,這個程序相當(dāng)于學(xué)一門新語言的“Hello World”,或者mapreduce的“WordCount”:)這里就不多做介紹了,簡單給大家看一下:
1 # Author:Charlotte
2 # Plot mnist dataset
3 from keras.datasets import mnist
4 import matplotlib.pyplot as plt
5 # load the MNIST dataset
6 (X_train, y_train), (X_test, y_test) = mnist.load_data()
7 # plot 4 images as gray scale
8 plt.subplot(221)
9 plt.imshow(X_train[0], cmap=plt.get_cmap('PuBuGn_r'))
10 plt.subplot(222)
11 plt.imshow(X_train[1], cmap=plt.get_cmap('PuBuGn_r'))
12 plt.subplot(223)
13 plt.imshow(X_train[2], cmap=plt.get_cmap('PuBuGn_r'))
14 plt.subplot(224)
15 plt.imshow(X_train[3], cmap=plt.get_cmap('PuBuGn_r'))
16 # show the plot
17 plt.show()
圖:

1.BaseLine版本
一開始我沒有想過用CNN做,因為比較耗時,所以想看看直接用比較簡單的算法看能不能得到很好的效果。之前用過機(jī)器學(xué)習(xí)算法跑過一遍,最好的效果是SVM,96.8%(默認(rèn)參數(shù),未調(diào)優(yōu)),所以這次準(zhǔn)備用神經(jīng)網(wǎng)絡(luò)做。BaseLine版本用的是MultiLayer Percepton(多層感知機(jī))。這個網(wǎng)絡(luò)結(jié)構(gòu)比較簡單,輸入--->隱含--->輸出。隱含層采用的rectifier linear unit,輸出直接選取的softmax進(jìn)行多分類。
網(wǎng)絡(luò)結(jié)構(gòu):

代碼:
1 # coding:utf-8
2 # Baseline MLP for MNIST dataset
3 import numpy
4 from keras.datasets import mnist
5 from keras.models import Sequential
6 from keras.layers import Dense
7 from keras.layers import Dropout
8 from keras.utils import np_utils
9
10 seed = 7
11 numpy.random.seed(seed)
12 #加載數(shù)據(jù)
13 (X_train, y_train), (X_test, y_test) = mnist.load_data()
14
15 num_pixels = X_train.shape[1] * X_train.shape[2]
16 X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')
17 X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32')
18
19 X_train = X_train / 255
20 X_test = X_test / 255
21
22 # 對輸出進(jìn)行one hot編碼
23 y_train = np_utils.to_categorical(y_train)
24 y_test = np_utils.to_categorical(y_test)
25 num_classes = y_test.shape[1]
26
27 # MLP模型
28 def baseline_model():
29 model = Sequential()
30 model.add(Dense(num_pixels, input_dim=num_pixels, init='normal', activation='relu'))
31 model.add(Dense(num_classes, init='normal', activation='softmax'))
32 model.summary()
33 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
34 return model
35
36 # 建立模型
37 model = baseline_model()
38
39 # Fit
40 model.fit(X_train, y_train, validation_data=http://www.netofthings.cn/JieJueFangAn/2016-07/(X_test, y_test), nb_epoch=10, batch_size=200, verbose=2)
41
42 #Evaluation
43 scores = model.evaluate(X_test, y_test, verbose=0)
44 print("Baseline Error: %.2f%%" % (100-scores[1]*100))#輸出錯誤率
結(jié)果:
1 Layer (type) Output Shape Param # Connected to
2 ====================================================================================================
3 dense_1 (Dense) (None, 784) 615440 dense_input_1[0][0]
4 ____________________________________________________________________________________________________
5 dense_2 (Dense) (None, 10) 7850 dense_1[0][0]
6 ====================================================================================================