import numpy as np
np.random.seed(1337)
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
batch_size = 128
nb_classes = 10
nb_epoch = 12
img_rows, img_cols = 28, 28
nb_filters = 32
pool_size = (2,2)
kernel_size = (3,3)
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
X_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size[0] ,kernel_size[1],
border_mode='valid',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
convolution2d_3 (Convolution2D) (None, 26, 26, 32) 320 convolution2d_input_2[0][0]
____________________________________________________________________________________________________
activation_5 (Activation) (None, 26, 26, 32) 0 convolution2d_3[0][0]
____________________________________________________________________________________________________
convolution2d_4 (Convolution2D) (None, 24, 24, 32) 9248 activation_5[0][0]
____________________________________________________________________________________________________
activation_6 (Activation) (None, 24, 24, 32) 0 convolution2d_4[0][0]
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D) (None, 12, 12, 32) 0 activation_6[0][0]
____________________________________________________________________________________________________
dropout_3 (Dropout) (None, 12, 12, 32) 0 maxpooling2d_2[0][0]
____________________________________________________________________________________________________
flatten_2 (Flatten) (None, 4608) 0 dropout_3[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 128) 589952 flatten_2[0][0]
____________________________________________________________________________________________________
activation_7 (Activation) (None, 128) 0 dense_3[0][0]
____________________________________________________________________________________________________
dropout_4 (Dropout) (None, 128) 0 activation_7[0][0]
____________________________________________________________________________________________________
dense_4 (Dense) (None, 10) 1290 dropout_4[0][0]
____________________________________________________________________________________________________
activation_8 (Activation) (None, 10) 0 dense_4[0][0]
====================================================================================================
Total params: 600,810
Trainable params: 600,810
Non-trainable params: 0
____________________________________________________________________________________________________
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))
score = model.evaluate(X_test, Y_test, verbose=0)
Train on 60000 samples, validate on 10000 samples
Epoch 1/12
60000/60000 [==============================] - 18s - loss: 0.3675 - acc: 0.8886 - val_loss: 0.0877 - val_acc: 0.9722
Epoch 2/12
60000/60000 [==============================] - 13s - loss: 0.1346 - acc: 0.9598 - val_loss: 0.0623 - val_acc: 0.9802
Epoch 3/12
60000/60000 [==============================] - 13s - loss: 0.1039 - acc: 0.9691 - val_loss: 0.0527 - val_acc: 0.9837
Epoch 4/12
60000/60000 [==============================] - 13s - loss: 0.0887 - acc: 0.9736 - val_loss: 0.0462 - val_acc: 0.9849
Epoch 5/12
60000/60000 [==============================] - 13s - loss: 0.0778 - acc: 0.9763 - val_loss: 0.0420 - val_acc: 0.9860
Epoch 6/12
60000/60000 [==============================] - 13s - loss: 0.0698 - acc: 0.9794 - val_loss: 0.0383 - val_acc: 0.9871
Epoch 7/12
60000/60000 [==============================] - 14s - loss: 0.0659 - acc: 0.9802 - val_loss: 0.0374 - val_acc: 0.9868
Epoch 8/12
60000/60000 [==============================] - 14s - loss: 0.0616 - acc: 0.9818 - val_loss: 0.0385 - val_acc: 0.9877
Epoch 9/12
60000/60000 [==============================] - 14s - loss: 0.0563 - acc: 0.9829 - val_loss: 0.0338 - val_acc: 0.9881
Epoch 10/12
60000/60000 [==============================] - 14s - loss: 0.0531 - acc: 0.9845 - val_loss: 0.0320 - val_acc: 0.9889
Epoch 11/12
60000/60000 [==============================] - 13s - loss: 0.0498 - acc: 0.9855 - val_loss: 0.0323 - val_acc: 0.9890
Epoch 12/12
60000/60000 [==============================] - 14s - loss: 0.0479 - acc: 0.9852 - val_loss: 0.0329 - val_acc: 0.9892
print('Test score:', score[0])
print('Test accuracy:', score[1])
Test score: 0.032927570413
Test accuracy: 0.9892