This repository is a part of Algorithm 02 Assignment; the original repository is Jin0316/Algorithm.
You can check out the (edited) notebook at /TensorFlow_mnist_example.ipynb; use nbviewer if GitHub viewer does not work.
I used the three models provided as examples as-is, stored as model1
, model2
, and model3
respectively.
def select_model(model_number):
if model_number == 1:
model = keras.models.Sequential([
keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), # layer 1
keras.layers.MaxPool2D((2, 2)), # layer 2
keras.layers.Flatten(),
keras.layers.Dense(10, activation='softmax')]) # layer 3
if model_number == 2:
model = keras.models.Sequential([
keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), # layer 1
keras.layers.MaxPool2D((2, 2)), # layer 2
keras.layers.Conv2D(64, (3, 3), activation='relu'), # layer 3
keras.layers.MaxPool2D((2, 2)), # layer 4
keras.layers.Flatten(),
keras.layers.Dense(10, activation='softmax')]) # layer 5
if model_number == 3:
model = keras.models.Sequential([
keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), # layer 1
keras.layers.MaxPool2D((2, 2)), # layer 2
keras.layers.Conv2D(64, (3, 3), activation='relu'), # layer 3
keras.layers.Conv2D(64, (3, 3), activation='relu'), # layer 4
keras.layers.MaxPool2D((2, 2)), # layer 5
keras.layers.Conv2D(128, (3, 3), activation='relu'), # layer 6
keras.layers.Flatten(),
keras.layers.Dense(10, activation='softmax')]) # layer 7
return model
model1 = select_model(1)
model2 = select_model(2)
model3 = select_model(3)
The summary()
call reveals that each model has 54,090, 34,826, and 134,730 trainable parameters respectively; I expected that model with more trainable parameters would be more accurate in general.
Each model was tranied with the same training images and labels from the MNIST dataset with 5 epochs.
After the last epoch,
- Model 1's accuracy and loss was 0.9834 and 0.0568.
- Model 2's accuracy and loss was 0.9868 and 0.0429.
- Model 3's accuracy and loss was 0.9907 and 0.0313.
Each model was then tested with the same testing images and labels from the MNIST dataset.
- Model 2 was the most accurate one, with accuracy 0.9857 and loss 0.0517.
- Model 3 follows with accuracy 0.9792 and loss 0.0657.
- Model 1 was the least accurate, with accuracy 0.9682 and loss 0.1430.
This result disproves my former hypothesis where more trainable parameters meant more accuracy.
Each model predicted the first fifteen images similarly, with almost perfect confidence for each image.
Each model produced 318, 143, and 208 mispredictions; the first ten examples for each follows.