Modified National Institute of Standards and Technology (MNIST) Data: Multi-Layer Perceptron (MLP) with Root Mean Square Propagation (RMSProp)

In [1]:
from __future__ import print_function
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import RMSprop
from keras.utils import np_utils

import matplotlib.pyplot as plt
%matplotlib inline

np.random.seed(1671)  # for reproducibility
Using CNTK backend
In [2]:
# network and training
NB_EPOCH = 20
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10   # number of outputs = number of digits
OPTIMIZER = RMSprop() # optimizer, explainedin this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION
DROPOUT = 0.3
In [3]:
# data: shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

#X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784
RESHAPED = 784
#
X_train = X_train.reshape(60000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
In [4]:
# normalize
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)
60000 train samples
10000 test samples
In [5]:
# M_HIDDEN hidden layers
# 10 outputs
# final stage is softmax

model = Sequential()
model.add(Dense(N_HIDDEN, input_shape=(RESHAPED,)))
model.add(Activation('relu'))
model.add(Dropout(DROPOUT))
model.add(Dense(N_HIDDEN))
model.add(Activation('relu'))
model.add(Dropout(DROPOUT))
model.add(Dense(NB_CLASSES))
model.add(Activation('softmax'))
model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer=OPTIMIZER,
              metrics=['accuracy'])

history = model.fit(X_train, Y_train,
                    batch_size=BATCH_SIZE, epochs=NB_EPOCH,
                    verbose=VERBOSE, validation_split=VALIDATION_SPLIT)

score = model.evaluate(X_test, Y_test, verbose=VERBOSE)
print("\nTest score:", score[0])
print('Test accuracy:', score[1])
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 128)               100480    
_________________________________________________________________
activation_1 (Activation)    (None, 128)               0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 128)               16512     
_________________________________________________________________
activation_2 (Activation)    (None, 128)               0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 10)                1290      
_________________________________________________________________
activation_3 (Activation)    (None, 10)                0         
=================================================================
Total params: 118,282
Trainable params: 118,282
Non-trainable params: 0
_________________________________________________________________
Train on 48000 samples, validate on 12000 samples
Epoch 1/20
 5248/48000 [==>...........................] - ETA: 1s - loss: 1.2089 - acc: 0.6239
/home/dadebarr/anaconda3/lib/python3.5/site-packages/cntk/core.py:351: UserWarning: your data is of type "float64", but your input variable (uid "Input67") expects "<class 'numpy.float32'>". Please convert your data beforehand to speed up training.
  (sample.dtype, var.uid, str(var.dtype)))
48000/48000 [==============================] - 1s - loss: 0.4832 - acc: 0.8548 - val_loss: 0.1801 - val_acc: 0.9487
Epoch 2/20
48000/48000 [==============================] - 1s - loss: 0.2226 - acc: 0.9344 - val_loss: 0.1346 - val_acc: 0.9604
Epoch 3/20
48000/48000 [==============================] - 1s - loss: 0.1738 - acc: 0.9480 - val_loss: 0.1146 - val_acc: 0.9674
Epoch 4/20
48000/48000 [==============================] - 1s - loss: 0.1468 - acc: 0.9567 - val_loss: 0.1086 - val_acc: 0.9677
Epoch 5/20
48000/48000 [==============================] - 1s - loss: 0.1293 - acc: 0.9615 - val_loss: 0.0999 - val_acc: 0.9728
Epoch 6/20
48000/48000 [==============================] - 1s - loss: 0.1182 - acc: 0.9641 - val_loss: 0.0909 - val_acc: 0.9758
Epoch 7/20
48000/48000 [==============================] - 1s - loss: 0.1108 - acc: 0.9671 - val_loss: 0.0882 - val_acc: 0.9748
Epoch 8/20
48000/48000 [==============================] - 1s - loss: 0.1011 - acc: 0.9696 - val_loss: 0.0921 - val_acc: 0.9751
Epoch 9/20
48000/48000 [==============================] - 1s - loss: 0.0968 - acc: 0.9714 - val_loss: 0.0900 - val_acc: 0.9763
Epoch 10/20
48000/48000 [==============================] - 1s - loss: 0.0922 - acc: 0.9724 - val_loss: 0.0931 - val_acc: 0.9761
Epoch 11/20
48000/48000 [==============================] - 1s - loss: 0.0898 - acc: 0.9723 - val_loss: 0.0945 - val_acc: 0.9767
Epoch 12/20
48000/48000 [==============================] - 1s - loss: 0.0874 - acc: 0.9748 - val_loss: 0.0960 - val_acc: 0.9763
Epoch 13/20
48000/48000 [==============================] - 1s - loss: 0.0850 - acc: 0.9739 - val_loss: 0.0915 - val_acc: 0.9769
Epoch 14/20
48000/48000 [==============================] - 1s - loss: 0.0786 - acc: 0.9771 - val_loss: 0.0942 - val_acc: 0.9776
Epoch 15/20
48000/48000 [==============================] - 1s - loss: 0.0768 - acc: 0.9775 - val_loss: 0.0937 - val_acc: 0.9782
Epoch 16/20
48000/48000 [==============================] - 1s - loss: 0.0741 - acc: 0.9783 - val_loss: 0.0988 - val_acc: 0.9771
Epoch 17/20
48000/48000 [==============================] - 1s - loss: 0.0718 - acc: 0.9791 - val_loss: 0.0969 - val_acc: 0.9785
Epoch 18/20
48000/48000 [==============================] - 1s - loss: 0.0743 - acc: 0.9783 - val_loss: 0.0964 - val_acc: 0.9777
Epoch 19/20
48000/48000 [==============================] - 1s - loss: 0.0728 - acc: 0.9798 - val_loss: 0.0944 - val_acc: 0.9803
Epoch 20/20
48000/48000 [==============================] - 1s - loss: 0.0700 - acc: 0.9797 - val_loss: 0.0986 - val_acc: 0.9782
 9568/10000 [===========================>..] - ETA: 0s
Test score: 0.0957163101462
Test accuracy: 0.9768
In [6]:
# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
dict_keys(['acc', 'val_loss', 'loss', 'val_acc'])
In [7]:
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
In [ ]: