CuDNNLSTM

98.8% accuracy


In [3]:
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, CuDNNLSTM
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(CuDNNLSTM(units=units,
                    input_shape=input_shape))
model.add(Dense(num_labels))
model.add(Activation('softmax'))
model.summary()
plot_model(model, to_file='cudnnlstm-mnist.png', show_shapes=True)

# loss function for one-hot vector
# use of adam optimizer (better than sgd)
# accuracy is good metric for classification tasks
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              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 #   
=================================================================
cu_dnnlstm_2 (CuDNNLSTM)     (None, 256)               292864    
_________________________________________________________________
dense_2 (Dense)              (None, 10)                2570      
_________________________________________________________________
activation_2 (Activation)    (None, 10)                0         
=================================================================
Total params: 295,434
Trainable params: 295,434
Non-trainable params: 0
_________________________________________________________________
Epoch 1/20
60000/60000 [==============================] - 5s 80us/step - loss: 0.4171 - acc: 0.8623
Epoch 2/20
60000/60000 [==============================] - 5s 78us/step - loss: 0.1282 - acc: 0.9605
Epoch 3/20
60000/60000 [==============================] - 5s 79us/step - loss: 0.0842 - acc: 0.9737
Epoch 4/20
60000/60000 [==============================] - 5s 85us/step - loss: 0.0636 - acc: 0.9801
Epoch 5/20
60000/60000 [==============================] - 5s 88us/step - loss: 0.0508 - acc: 0.9846
Epoch 6/20
60000/60000 [==============================] - 5s 84us/step - loss: 0.0442 - acc: 0.9859
Epoch 7/20
60000/60000 [==============================] - 5s 87us/step - loss: 0.0364 - acc: 0.9891
Epoch 8/20
60000/60000 [==============================] - 5s 87us/step - loss: 0.0309 - acc: 0.9902
Epoch 9/20
60000/60000 [==============================] - 5s 89us/step - loss: 0.0276 - acc: 0.9916
Epoch 10/20
60000/60000 [==============================] - 5s 87us/step - loss: 0.0261 - acc: 0.9917
Epoch 11/20
60000/60000 [==============================] - 5s 86us/step - loss: 0.0241 - acc: 0.9928
Epoch 12/20
60000/60000 [==============================] - 5s 90us/step - loss: 0.0218 - acc: 0.9933
Epoch 13/20
60000/60000 [==============================] - 5s 92us/step - loss: 0.0183 - acc: 0.9944
Epoch 14/20
60000/60000 [==============================] - 6s 92us/step - loss: 0.0175 - acc: 0.9942
Epoch 15/20
60000/60000 [==============================] - 5s 90us/step - loss: 0.0152 - acc: 0.9950
Epoch 16/20
60000/60000 [==============================] - 5s 89us/step - loss: 0.0145 - acc: 0.9955
Epoch 17/20
60000/60000 [==============================] - 6s 92us/step - loss: 0.0128 - acc: 0.9962
Epoch 18/20
60000/60000 [==============================] - 6s 92us/step - loss: 0.0145 - acc: 0.9955
Epoch 19/20
60000/60000 [==============================] - 5s 92us/step - loss: 0.0103 - acc: 0.9966
Epoch 20/20
60000/60000 [==============================] - 5s 89us/step - loss: 0.0126 - acc: 0.9962
10000/10000 [==============================] - 0s 45us/step

Test accuracy: 98.8%

In [ ]: