RNN for MNIST digits classification

~98.0% test accuracy in 20epochs


In [5]:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation, SimpleRNN
from keras.utils import to_categorical, plot_model
from keras.datasets import mnist

# load mnist dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# compute the number of labels
num_labels = len(np.unique(y_train))

# convert to one-hot vector
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

# resize and normalize
image_size = x_train.shape[1]
x_train = np.reshape(x_train,[-1, image_size, image_size])
x_test = np.reshape(x_test,[-1, image_size, image_size])
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

# network parameters
input_shape = (image_size, image_size)
batch_size = 128
units = 256
dropout = 0.2

# model is RNN with 256 units, input is 28-dim vector 28 timesteps
model = Sequential()
model.add(SimpleRNN(units=units,
                    input_shape=input_shape))
model.add(Dense(num_labels))
model.add(Activation('softmax'))
model.summary()
plot_model(model, to_file='rnn-mnist.png', show_shapes=True)

# loss function for one-hot vector
# use of sgd optimizer
# accuracy is good metric for classification tasks
model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])
# train the network
model.fit(x_train, y_train, epochs=20, batch_size=batch_size)

loss, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
print("\nTest accuracy: %.1f%%" % (100.0 * acc))


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
simple_rnn_5 (SimpleRNN)     (None, 256)               72960     
_________________________________________________________________
dense_5 (Dense)              (None, 10)                2570      
_________________________________________________________________
activation_5 (Activation)    (None, 10)                0         
=================================================================
Total params: 75,530
Trainable params: 75,530
Non-trainable params: 0
_________________________________________________________________
Epoch 1/20
60000/60000 [==============================] - 5s 79us/step - loss: 0.6501 - acc: 0.8246
Epoch 2/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.2712 - acc: 0.9230
Epoch 3/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.2014 - acc: 0.9413
Epoch 4/20
60000/60000 [==============================] - 4s 75us/step - loss: 0.1639 - acc: 0.9524
Epoch 5/20
60000/60000 [==============================] - 4s 75us/step - loss: 0.1385 - acc: 0.9599
Epoch 6/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.1209 - acc: 0.9645
Epoch 7/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.1079 - acc: 0.9681
Epoch 8/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.0974 - acc: 0.9711
Epoch 9/20
60000/60000 [==============================] - 4s 75us/step - loss: 0.0877 - acc: 0.9745
Epoch 10/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.0818 - acc: 0.9758
Epoch 11/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.0757 - acc: 0.9777
Epoch 12/20
60000/60000 [==============================] - 4s 75us/step - loss: 0.0712 - acc: 0.9790
Epoch 13/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.0662 - acc: 0.9799
Epoch 14/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.0615 - acc: 0.9820
Epoch 15/20
60000/60000 [==============================] - 5s 83us/step - loss: 0.0587 - acc: 0.9833
Epoch 16/20
60000/60000 [==============================] - 5s 81us/step - loss: 0.0554 - acc: 0.9838
Epoch 17/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.0526 - acc: 0.9846
Epoch 18/20
60000/60000 [==============================] - 5s 78us/step - loss: 0.0497 - acc: 0.9854
Epoch 19/20
60000/60000 [==============================] - 4s 74us/step - loss: 0.0461 - acc: 0.9866
Epoch 20/20
60000/60000 [==============================] - 5s 75us/step - loss: 0.0453 - acc: 0.9868
10000/10000 [==============================] - 1s 55us/step

Test accuracy: 97.9%

In [ ]: