This notebook presetns Multi-Layer Perceptron solution to MNIST dataset.
Contents
import numpy as np
import matplotlib.pyplot as plt
Limit TensorFlow GPU memory usage
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config):
pass # init sessin with allow_growth
Load MNIST Dataset from Keras API
(x_train_raw, y_train_raw), (x_test_raw, y_test_raw) = tf.keras.datasets.mnist.load_data()
Raw data shapes
print(x_train_raw.shape)
print(y_train_raw.shape)
Show example images
fig, axes = plt.subplots(nrows=1, ncols=6, figsize=[16, 9])
for i in range(len(axes)):
axes[i].set_title('Label: '+str(y_train_raw[i]))
axes[i].imshow(x_train_raw[i])
One-hot encode labels
y_train = tf.keras.utils.to_categorical(y_train_raw, 10)
y_test = tf.keras.utils.to_categorical(y_test_raw, 10)
print('y_train:')
print('shape', y_train.shape)
print('data')
print(y_train[:6])
Normalize and flatten images
x_train = (x_train_raw - x_train_raw.mean()) / x_train_raw.std()
x_test = (x_test_raw - x_train_raw.mean()) / x_train_raw.std() # reuse mean/std from train set
x_train = x_train.reshape([len(x_train), -1])
x_test = x_test.reshape([len(x_test), -1])
print('x_train:')
print('shape', x_train.shape)
print('data')
print(x_train[0, 300:400].round(2))
Simple multi-layer perceptron
from tensorflow.keras.layers import Dense, Dropout
model = tf.keras.Sequential()
model.add(Dense(units=512, activation='elu', input_dim=784))
model.add(Dropout(rate=0.2))
model.add(Dense(units=512, activation='elu'))
model.add(Dropout(rate=0.2))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
Keras only logs metrics every epoch, to get more datapoints we implement our own callback.
class Callback(tf.keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.hist = {'loss':[], 'acc':[]}
def on_batch_end(self, batch, logs={}):
self.hist['loss'].append(logs.get('loss'))
self.hist['acc'].append(logs.get('acc'))
Train model
cback = Callback()
hist = model.fit(x=x_train, y=y_train, batch_size=1000, epochs=5, callbacks=[cback])
Final result
loss, acc = model.evaluate(x_train, y_train, verbose=0)
print(f'Accuracy on train set: {acc:.3f}')
loss, acc = model.evaluate(x_test, y_test, verbose=0)
print(f'Accuracy on test set: {acc:.3f}')
Plot loss and accuracy over training period
plt.plot(cback.hist['loss'], label='loss')
plt.plot(cback.hist['acc'], label='acc', color='red')
plt.legend();