import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import time
import random
mnist = tf.keras.datasets.mnist.load_data()
# Train and test datasets
(x_train, y_train), (x_test, y_test) = mnist
print(f"x_train shape: {x_train.shape}")
print(f"x_test shape: {x_test.shape}")
x_train shape: (60000, 28, 28) x_test shape: (10000, 28, 28)
# Showing the first 16 digits of the set
rows = 4
cols = 4
axes = []
fig = plt.figure()
for a in range(rows*cols):
axes.append( fig.add_subplot(rows, cols, a+1))
axes[-1].set_title(y_train[a])
img = plt.imshow(x_train[a], cmap=plt.cm.gray_r)
img.axes.get_xaxis().set_visible(False)
img.axes.get_yaxis().set_visible(False)
plt.tight_layout()
plt.show()
# Prepare data for training
# Preprocess the data
(x_train, y_train), (x_test, y_test) = mnist
x_train = x_train.reshape(x_train.shape[0], -1).astype("float32") / 255.0
x_test = x_test.reshape(x_test.shape[0], -1).astype("float32") / 255.0
print(f"x_train shape: {x_train.shape}")
print(f"x_test shape: {x_test.shape}")
x_train shape: (60000, 784) x_test shape: (10000, 784)
Since we are creating and evaluating several neural network models, with different hyperparameters, we define first some functions, for creating and training models and plotting their metrics.
def nn_compile(model, params):
model.compile(
loss=params[0],
optimizer=params[1],
metrics=[params[2]]
)
def nn_fit(model, x, y, epochs=10, batch_size=64, validation_split=0.1, verbose=2):
print("Fit model on training data")
history = model.fit(x, y,
epochs=epochs,
batch_size=batch_size,
validation_split=validation_split,
verbose=verbose
)
print("Training finished")
return history
def nn_plot(history, epochs, subplot=False, loss=False, acc=False, merge=True, title=""):
"""
history: model fit output
epochs: number of epochs to plot
subplot: if True, uses subplot for comparison
loss: plot loss graphic
acc: plot accuracy graphic
merge: if True plot train and validation together
title: use of subplot
"""
if loss == True:
if subplot == False:
if merge == True:
pd.DataFrame(history.history, columns=['loss', 'val_loss']).plot(figsize=(8, 5))
plt.grid(True)
plt.legend(['Train loss', 'Validation loss'])
plt.gca().set_xlim(0, epochs)
plt.show()
else:
pd.DataFrame(history.history['loss']).plot(figsize=(8, 5))
plt.grid(True)
plt.legend(['Train loss'])
plt.gca().set_xlim(0, epochs)
plt.show()
pd.DataFrame(history.history['val_loss']).plot(figsize=(8, 5))
plt.grid(True)
plt.legend(['Validation loss'])
plt.gca().set_xlim(0, epochs)
plt.show()
else: # subplot merged
subplot.plot(pd.DataFrame(history.history, columns=['loss', 'val_loss']))
subplot.grid(True)
subplot.set_title(title)
subplot.legend(['Train loss', 'Validation loss'])
subplot.set_xlim(0, epochs)
if acc == True:
if subplot == False:
if merge == True:
pd.DataFrame(history.history,
columns=['sparse_categorical_accuracy',
'val_sparse_categorical_accuracy']).plot(figsize=(8, 5))
plt.grid(True)
plt.legend(['Train accuracy', 'Validation accuracy'])
plt.gca().set_xlim(0, epochs)
plt.show()
else:
pd.DataFrame(history.history['sparse_categorical_accuracy']).plot(figsize=(8, 5))
plt.grid(True)
plt.legend(['Train accuracy'])
plt.gca().set_xlim(0, epochs)
plt.show()
pd.DataFrame(history.history['val_sparse_categorical_accuracy']).plot(figsize=(8, 5))
plt.grid(True)
plt.legend(['Validation accuracy'])
plt.gca().set_xlim(0, epochs)
plt.show()
else: # subplot merged
subplot.plot(pd.DataFrame(history.history, columns=['sparse_categorical_accuracy',
'val_sparse_categorical_accuracy']))
subplot.grid(True)
subplot.set_title(title)
subplot.legend(['Train accuracy', 'Validation accuracy'])
subplot.set_xlim(0, epochs)
model = []
params = []
hist = []
start_time = []
end_time = []
results = []
model.append(tf.keras.models.Sequential([
tf.keras.layers.Dense(30, activation=tf.nn.sigmoid, input_shape=(784,), name='hidden_1_layer'),
tf.keras.layers.Dense(10, activation='softmax', name='output_layer')
]))
params.append([tf.keras.losses.SparseCategoricalCrossentropy(),
tf.optimizers.SGD(learning_rate=0.5),
tf.keras.metrics.SparseCategoricalAccuracy()])
nn_compile(model[0], params[0])
model[0].summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
hidden_1_layer (Dense) (None, 30) 23550
output_layer (Dense) (None, 10) 310
=================================================================
Total params: 23,860
Trainable params: 23,860
Non-trainable params: 0
_________________________________________________________________
tf.keras.utils.plot_model(model[0], show_shapes=True, show_layer_names=True)
We start by training on a small sample of our entire train dataset. Using only 1,000 values for training, we can show the overfitting effect.
Values applied: mini batch = 10 and 200 epochs.