6 CNN for MNIST with TensorFlow and Keras

import os

import numpy as np
np.random.seed(123)
print("NumPy:{}".format(np.__version__))

import tensorflow as tf
tf.set_random_seed(123)
print("TensorFlow:{}".format(tf.__version__))
NumPy:1.13.1
TensorFlow:1.4.1
DATASETSLIB_HOME = '../datasetslib'
import sys
if not DATASETSLIB_HOME in sys.path:
    sys.path.append(DATASETSLIB_HOME)
%reload_ext autoreload
%autoreload 2
import datasetslib

datasetslib.datasets_root = os.path.join(os.path.expanduser('~'),'datasets')

Get the MNIST data

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(os.path.join(datasetslib.datasets_root,
                                               'mnist'), 
                                  one_hot=True)

X_train = mnist.train.images
X_test = mnist.test.images
Y_train = mnist.train.labels
Y_test = mnist.test.labels
n_classes = 10
Extracting /home/armando/datasets/mnist/train-images-idx3-ubyte.gz
Extracting /home/armando/datasets/mnist/train-labels-idx1-ubyte.gz
Extracting /home/armando/datasets/mnist/t10k-images-idx3-ubyte.gz
Extracting /home/armando/datasets/mnist/t10k-labels-idx1-ubyte.gz

Preprocess for RNN

X_train = X_train.reshape(-1,28,28)
X_test = X_test.reshape(-1,28,28)

RNN With Keras for MNIST Data

import keras
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers.recurrent import SimpleRNN
from keras.optimizers import RMSprop
from keras.optimizers import SGD
tf.reset_default_graph()
keras.backend.clear_session()
# create and fit the SimpleRNN model
model = Sequential()
model.add(SimpleRNN(units=16, activation='relu', input_shape=(28,28)))
model.add(Dense(n_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(lr=0.01),
              metrics=['accuracy'])
model.summary()

model.fit(X_train, Y_train,
                    batch_size=100,
                    epochs=20)

score = model.evaluate(X_test, Y_test)
print('\nTest loss:', score[0])
print('Test accuracy:', score[1])
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
simple_rnn_1 (SimpleRNN)     (None, 16)                720       
_________________________________________________________________
dense_1 (Dense)              (None, 10)                170       
_________________________________________________________________
activation_1 (Activation)    (None, 10)                0         
=================================================================
Total params: 890
Trainable params: 890
Non-trainable params: 0
_________________________________________________________________
Epoch 1/20
55000/55000 [==============================] - 5s 84us/step - loss: 1.3061 - acc: 0.5084
Epoch 2/20
55000/55000 [==============================] - 5s 88us/step - loss: 1.0005 - acc: 0.6136
Epoch 3/20
55000/55000 [==============================] - 4s 77us/step - loss: 0.9252 - acc: 0.6544
Epoch 4/20
55000/55000 [==============================] - 5s 84us/step - loss: 0.8111 - acc: 0.7121
Epoch 5/20
55000/55000 [==============================] - 4s 78us/step - loss: 0.7383 - acc: 0.7450
Epoch 6/20
55000/55000 [==============================] - 4s 82us/step - loss: 0.6968 - acc: 0.7631
Epoch 7/20
55000/55000 [==============================] - 5s 84us/step - loss: 0.6685 - acc: 0.7725
Epoch 8/20
55000/55000 [==============================] - 4s 75us/step - loss: 0.6553 - acc: 0.7765
Epoch 9/20
55000/55000 [==============================] - 4s 77us/step - loss: 0.6455 - acc: 0.7851
Epoch 10/20
55000/55000 [==============================] - 5s 82us/step - loss: 0.6378 - acc: 0.7920
Epoch 11/20
55000/55000 [==============================] - 5s 84us/step - loss: 0.6313 - acc: 0.7983
Epoch 12/20
55000/55000 [==============================] - 4s 76us/step - loss: 0.6381 - acc: 0.7997
Epoch 13/20
55000/55000 [==============================] - 4s 81us/step - loss: 0.6193 - acc: 0.8049
Epoch 14/20
55000/55000 [==============================] - 4s 81us/step - loss: 0.6208 - acc: 0.8097
Epoch 15/20
55000/55000 [==============================] - 4s 79us/step - loss: 0.6021 - acc: 0.8132
Epoch 16/20
55000/55000 [==============================] - 4s 76us/step - loss: 0.6231 - acc: 0.8117
Epoch 17/20
55000/55000 [==============================] - 4s 74us/step - loss: 0.6085 - acc: 0.8164
Epoch 18/20
55000/55000 [==============================] - 5s 82us/step - loss: 0.6161 - acc: 0.8141
Epoch 19/20
55000/55000 [==============================] - 4s 81us/step - loss: 0.6146 - acc: 0.8146
Epoch 20/20
55000/55000 [==============================] - 4s 78us/step - loss: 0.5835 - acc: 0.8286
10000/10000 [==============================] - 1s 119us/step

Test loss: 0.520945608187
Test accuracy: 0.8379

results matching ""

    No results matching ""