____________________________________________________________________________________________________
dense_2 (Dense) (None, 50) 6450 dense_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 10) 510 dense_2[0][0]
====================================================================================================
Total params: 59933
____________________________________________________________________________________________________
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
34s - loss: 0.3789 - acc: 0.8796 - val_loss: 0.0811 - val_acc: 0.9742
Epoch 2/10
34s - loss: 0.0929 - acc: 0.9710 - val_loss: 0.0462 - val_acc: 0.9854
Epoch 3/10
35s - loss: 0.0684 - acc: 0.9786 - val_loss: 0.0376 - val_acc: 0.9869
Epoch 4/10
35s - loss: 0.0546 - acc: 0.9826 - val_loss: 0.0332 - val_acc: 0.9890
Epoch 5/10
35s - loss: 0.0467 - acc: 0.9856 - val_loss: 0.0289 - val_acc: 0.9897
Epoch 6/10
35s - loss: 0.0402 - acc: 0.9873 - val_loss: 0.0291 - val_acc: 0.9902
Epoch 7/10
34s - loss: 0.0369 - acc: 0.9880 - val_loss: 0.0233 - val_acc: 0.9924
Epoch 8/10
36s - loss: 0.0336 - acc: 0.9894 - val_loss: 0.0258 - val_acc: 0.9913
Epoch 9/10
39s - loss: 0.0317 - acc: 0.9899 - val_loss: 0.0219 - val_acc: 0.9926
Epoch 10/10
40s - loss: 0.0268 - acc: 0.9916 - val_loss: 0.0220 - val_acc: 0.9919
Large CNN Error: 0.81%
效果不错,现在的准确率是99.19%
4.最终版本
网络结构没变,只是在每一层后面加了dropout,结果居然有显著提升。一开始迭代500次,跑死我了,结果过拟合了,然后观察到69次的时候结果就已经很好了,就选择了迭代69次。
1 # Larger CNN for the MNIST Dataset
2 import numpy
3 from keras.datasets import mnist
4 from keras.models import Sequential
5 from keras.layers import Dense
6 from keras.layers import Dropout
7 from keras.layers import Flatten
8 from keras.layers.convolutional import Convolution2D
9 from keras.layers.convolutional import MaxPooling2D
10 from keras.utils import np_utils
11 import matplotlib.pyplot as plt
12 from keras.constraints import maxnorm
13 from keras.optimizers import SGD
14 # fix random seed for reproducibility
15 seed = 7
16 numpy.random.seed(seed)
17 # load data
18 (X_train, y_train), (X_test, y_test) = mnist.load_data()
19 # reshape to be [samples][pixels][width][height]
20 X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
21 X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
22 # normalize inputs from 0-255 to 0-1
23 X_train = X_train / 255
24 X_test = X_test / 255
25 # one hot encode outputs
26 y_train = np_utils.to_categorical(y_train)
27 y_test = np_utils.to_categorical(y_test)
28 num_classes = y_test.shape[1]
29 ###raw
30 # define the larger model
31 def larger_model():
32 # create model
33 model = Sequential()
34 model.add(Convolution2D(30, 5, 5, border_mode='valid', input_shape=(1, 28, 28), activation='relu'))
35 model.add(MaxPooling2D(pool_size=(2, 2)))
36 model.add(Dropout(0.4))
37 model.add(Convolution2D(15, 3, 3, activation='relu'))
38 model.add(MaxPooling2D(pool_size=(2, 2)))
39 model.add(Dropout(0.4))
40 model.add(Flatten())
41 model.add(Dense(128, activation='relu'))
42 model.add(Dropout(0.4))
43 model.add(Dense(50, activation='relu'))
44 model.add(Dropout(0.4))
45 model.add(Dense(num_classes, activation='softmax'))
46 # Compile model
47 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
48 return model
49
50 # build the model
51 model = larger_model()
52 # Fit the model
53 model.fit(X_train, y_train, validation_data=http://www.netofthings.cn/JieJueFangAn/2016-07/(X_test, y_test), nb_epoch=200, batch_size=200, verbose=2)
54 # Final evaluation of the model
55 scores = model.evaluate(X_test, y_test, verbose=0)
56 print("Large CNN Error: %.2f%%" % (100-scores[1]*100))
结果: