31 Epoch 7/10
32 36s - loss: 0.0223 - acc: 0.9930 - val_loss: 0.0328 - val_acc: 0.9893
33 Epoch 8/10
34 36s - loss: 0.0198 - acc: 0.9939 - val_loss: 0.0381 - val_acc: 0.9880
35 Epoch 9/10
36 36s - loss: 0.0156 - acc: 0.9954 - val_loss: 0.0347 - val_acc: 0.9884
37 Epoch 10/10
38 36s - loss: 0.0141 - acc: 0.9955 - val_loss: 0.0318 - val_acc: 0.9893
39 CNN Error: 1.07%
迭代的结果中,loss和acc为训练集的结果,val_loss和val_acc为验证机的结果。从结果上来看,效果不错,比100次迭代的MLP(1.46%)提升了0.39%,CNN的误差率为1.07%。这里的CNN的网络结构还是比较简单的,如果把CNN的结果再加几层,边复杂一代,结果是否还能提升?
3.Larger CNN
这一次我加了几层卷积层,代码:
1 # Larger CNN
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
12 seed = 7
13 numpy.random.seed(seed)
14 # load data
15 (X_train, y_train), (X_test, y_test) = mnist.load_data()
16 # reshape to be [samples][pixels][width][height]
17 X_train = X_train.reshape(X_train.shape[0], 1, 28, 28).astype('float32')
18 X_test = X_test.reshape(X_test.shape[0], 1, 28, 28).astype('float32')
19 # normalize inputs from 0-255 to 0-1
20 X_train = X_train / 255
21 X_test = X_test / 255
22 # one hot encode outputs
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 # define the larger model
27 def larger_model():
28 # create model
29 model = Sequential()
30 model.add(Convolution2D(30, 5, 5, border_mode='valid', input_shape=(1, 28, 28), activation='relu'))
31 model.add(MaxPooling2D(pool_size=(2, 2)))
32 model.add(Convolution2D(15, 3, 3, activation='relu'))
33 model.add(MaxPooling2D(pool_size=(2, 2)))
34 model.add(Dropout(0.2))
35 model.add(Flatten())
36 model.add(Dense(128, activation='relu'))
37 model.add(Dense(50, activation='relu'))
38 model.add(Dense(num_classes, activation='softmax'))
39 # Compile model
40 model.summary()
41 model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
42 return model
43 # build the model
44 model = larger_model()
45 # Fit the model
46 model.fit(X_train, y_train, validation_data=http://www.netofthings.cn/JieJueFangAn/2016-07/(X_test, y_test), nb_epoch=69, batch_size=200, verbose=2)
47 # Final evaluation of the model
48 scores = model.evaluate(X_test, y_test, verbose=0)
49 print("Large CNN Error: %.2f%%" % (100-scores[1]*100))
结果:
___________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
convolution2d_1 (Convolution2D) (None, 30, 24, 24) 780 convolution2d_input_1[0][0]
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D) (None, 30, 12, 12) 0 convolution2d_1[0][0]
____________________________________________________________________________________________________
convolution2d_2 (Convolution2D) (None, 15, 10, 10) 4065 maxpooling2d_1[0][0]
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D) (None, 15, 5, 5) 0 convolution2d_2[0][0]
____________________________________________________________________________________________________
dropout_1 (Dropout) (None, 15, 5, 5) 0 maxpooling2d_2[0][0]
____________________________________________________________________________________________________
flatten_1 (Flatten) (None, 375) 0 dropout_1[0][0]
____________________________________________________________________________________________________
dense_1 (Dense) (None, 128) 48128 flatten_1[0][0]