In [1]:
import tensorflow as tf

# Importing matplotlib to plot images.
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

# Importing SK-learn to calculate precision and recall
import sklearn
import sklearn.metrics

# Used for graph export
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
from keras.models import load_model
from keras import backend as K

tf.__version__


/usr/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/usr/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
Using TensorFlow backend.
Out[1]:
'1.9.0'

In [2]:
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

In [3]:
# flatten image data
x_train = x_train.reshape(-1, 784)
x_test = x_test.reshape(-1, 784)

# convert to float
x_train = x_train.astype(np.float32)
x_test = x_test.astype(np.float32)

# normalize to a scale between 0 and 1
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 (one-hot notation)
num_classes = 10
y_train_one_hot = tf.keras.utils.to_categorical(y_train, num_classes)
y_test_one_hot = tf.keras.utils.to_categorical(y_test, num_classes)


60000 train samples
10000 test samples

In [4]:
i = 31
plt.imshow(x_train[i].reshape(28, 28)) #np.sqrt(784) = 28
plt.title("Label for image %i is: %s" % (i, y_train[i]))


Out[4]:
Text(0.5,1,'Label for image 31 is: 8')

GPU and CPU settings

If GPU is not available, comment out the bottom block.


In [5]:
# If GPU is not available: 
# GPU_USE = '/cpu:0'
# config = tf.ConfigProto(device_count = {"GPU": 0})


# If GPU is available: 
config = tf.ConfigProto()
config.log_device_placement = True
config.allow_soft_placement = True
config.gpu_options.allocator_type = 'BFC'

# Limit the maximum memory used
config.gpu_options.per_process_gpu_memory_fraction = 0.1

# set session config
tf.keras.backend.set_session(tf.Session(config=config))

In [6]:
########## HYPER PARAMETERS
batch_size = 128
epochs = 10
optimizer = tf.keras.optimizers.RMSprop()
########## HYPER PARAMETERS

########## MODEL ARCHITECTURE
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(5, activation='relu', input_shape=(784,)))
model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))
########## MODEL ARCHITECTURE

# Print summary
model.summary()

# compile model for training
model.compile(loss='categorical_crossentropy',
              optimizer=optimizer,
              metrics=['accuracy'])

history = model.fit(x_train, y_train_one_hot,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test_one_hot))


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 5)                 3925      
_________________________________________________________________
dense_1 (Dense)              (None, 10)                60        
=================================================================
Total params: 3,985
Trainable params: 3,985
Non-trainable params: 0
_________________________________________________________________
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 2s 41us/step - loss: 1.4141 - acc: 0.5471 - val_loss: 0.8490 - val_acc: 0.7516
Epoch 2/10
60000/60000 [==============================] - 2s 36us/step - loss: 0.7050 - acc: 0.7883 - val_loss: 0.5561 - val_acc: 0.8480
Epoch 3/10
60000/60000 [==============================] - 2s 35us/step - loss: 0.5213 - acc: 0.8520 - val_loss: 0.4612 - val_acc: 0.8627
Epoch 4/10
60000/60000 [==============================] - 2s 35us/step - loss: 0.4692 - acc: 0.8640 - val_loss: 0.4382 - val_acc: 0.8688
Epoch 5/10
60000/60000 [==============================] - 2s 35us/step - loss: 0.4444 - acc: 0.8714 - val_loss: 0.4227 - val_acc: 0.8736
Epoch 6/10
60000/60000 [==============================] - 2s 34us/step - loss: 0.4284 - acc: 0.8751 - val_loss: 0.4129 - val_acc: 0.8773
Epoch 7/10
60000/60000 [==============================] - 2s 38us/step - loss: 0.4162 - acc: 0.8795 - val_loss: 0.4050 - val_acc: 0.8791
Epoch 8/10
60000/60000 [==============================] - 2s 38us/step - loss: 0.4067 - acc: 0.8816 - val_loss: 0.4019 - val_acc: 0.8794
Epoch 9/10
60000/60000 [==============================] - 2s 37us/step - loss: 0.3991 - acc: 0.8844 - val_loss: 0.3949 - val_acc: 0.8845
Epoch 10/10
60000/60000 [==============================] - 2s 36us/step - loss: 0.3927 - acc: 0.8854 - val_loss: 0.3903 - val_acc: 0.8852

Plot training and test accuracy


In [7]:
# use model for inference to get test accuracy
y_test_pred = model.predict(x_test)
y_test_pred = np.argmax(y_test_pred, axis=1)

print ('\n Summary of the precision, recall, F1 score for each class:')
print (sklearn.metrics.classification_report(y_test, y_test_pred))

print ('\n Confusion matrix: ')
print (sklearn.metrics.confusion_matrix(y_test, y_test_pred))


 Summary of the precision, recall, F1 score for each class:
             precision    recall  f1-score   support

          0       0.91      0.96      0.94       980
          1       0.95      0.97      0.96      1135
          2       0.90      0.88      0.89      1032
          3       0.84      0.85      0.84      1010
          4       0.86      0.91      0.88       982
          5       0.85      0.78      0.81       892
          6       0.92      0.91      0.92       958
          7       0.89      0.91      0.90      1028
          8       0.85      0.79      0.82       974
          9       0.87      0.85      0.86      1009

avg / total       0.88      0.89      0.88     10000


 Confusion matrix: 
[[ 943    0    5    5    3   11    9    3    1    0]
 [   0 1105    8    2    0    1    3    2   14    0]
 [  16   14  912   24   12    0   17   10   21    6]
 [   9    0   37  858    2   37    1   25   36    5]
 [   2    0    4    0  894    0   18    8    5   51]
 [  21    3    7   63   15  699   16   13   45   10]
 [  26    3   16    0   19   11  875    0    8    0]
 [   4   13   16    8   15    0    0  937    2   33]
 [   5   19   10   53   19   59   12    6  771   20]
 [   5    2    1   13   60    9    3   49    9  858]]

In [8]:
import matplotlib.pyplot as plt
plt.plot(history.history['val_acc'], label="Test Accuracy")
plt.plot(history.history['acc'], label="Training Accuracy")
plt.legend()


Out[8]:
<matplotlib.legend.Legend at 0x7f83087eecf8>

In [9]:
# save model
model.save("myModel.h5")