Model exercises#

*The content of this notebook is mainly based on Laurence Moroney’s video tutorial Basic Computer Vision with ML and the TensorFlow tutorial “Basic classification: Classify images of clothing”

Setup#

# TensorFlow and Keras
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)
2.7.1

Data preparation#

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

train_images = train_images / 255.0
test_images = test_images / 255.0

Model#

model = keras.Sequential(
    [
        layers.Flatten(input_shape=(28, 28), name="layer1"),
        layers.Dense(128, activation='relu', name="layer2"),
        layers.Dropout(0.05, name="layer3"),
        layers.Dense(10, name="layer4")
])
2022-03-10 13:34:04.936799: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
model.compile(optimizer='adam',
              loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
Epoch 1/10
1875/1875 [==============================] - 2s 945us/step - loss: 0.5073 - accuracy: 0.8224
Epoch 2/10
1875/1875 [==============================] - 1s 782us/step - loss: 0.3785 - accuracy: 0.8631
Epoch 3/10
1875/1875 [==============================] - 2s 853us/step - loss: 0.3419 - accuracy: 0.8750
Epoch 4/10
1875/1875 [==============================] - 1s 780us/step - loss: 0.3181 - accuracy: 0.8830
Epoch 5/10
1875/1875 [==============================] - 2s 817us/step - loss: 0.3015 - accuracy: 0.8891
Epoch 6/10
1875/1875 [==============================] - 2s 882us/step - loss: 0.2901 - accuracy: 0.8920
Epoch 7/10
1875/1875 [==============================] - 1s 764us/step - loss: 0.2772 - accuracy: 0.8962
Epoch 8/10
1875/1875 [==============================] - 1s 749us/step - loss: 0.2654 - accuracy: 0.9019
Epoch 9/10
1875/1875 [==============================] - 1s 751us/step - loss: 0.2579 - accuracy: 0.9038
Epoch 10/10
1875/1875 [==============================] - 1s 758us/step - loss: 0.2508 - accuracy: 0.9059
<keras.callbacks.History at 0x7fb3987b0c40>

Evaluate accuracy#

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)

print(f' Test accuracy: {test_acc:.3f} \n Test loss {test_loss:.3f}')
313/313 - 0s - loss: 0.3360 - accuracy: 0.8829 - 226ms/epoch - 724us/step
 Test accuracy: 0.883 
 Test loss 0.336

Predictions#

probability_model = keras.Sequential([
                        model, layers.Softmax()
                        ])

predictions = probability_model.predict(test_images)

predictions[0]
array([1.9155264e-08, 5.2179705e-10, 9.0776596e-12, 3.1734105e-11,
       4.7196639e-11, 5.7633326e-04, 8.9507097e-08, 1.8252372e-03,
       4.6519593e-09, 9.9759835e-01], dtype=float32)

Exercises#

Exercise 1#

  • For this first exercise run the below code: It creates a set of classifications for each of the test images, and then prints the first entry in the classifications.

  • The output, after you run it is a list of numbers.

  • Why do you think this is, and what do those numbers represent?

predictions[0].round(4)
array([0.000e+00, 0.000e+00, 0.000e+00, 0.000e+00, 0.000e+00, 6.000e-04,
       0.000e+00, 1.800e-03, 0.000e+00, 9.976e-01], dtype=float32)

Hint: try running print(test_labels[0]) – and you’ll get a 9. Does that help you understand why this list looks the way it does?

print(test_labels[0])
9

A: What does this list represent?

  1. It’s 10 random meaningless values

  2. It’s the first 10 classifications that the computer made

  3. It’s the probability that this item is each of the 10 classes

B: How do you know that this list tells you that the item is an ankle boot?

  1. There’s not enough information to answer that question

  2. The 10th element on the list is the biggest, and the ankle boot is labelled 9

  3. The ankle boot is label 9, and there are 0->9 elements in the list

Exercise 2#

  • Let’s now look at the layers in your model.

  • Experiment with different values for the dense layer with a different amount of neurons.

  • What different results do you get for loss, training time etc? Why do you think that’s the case?

Question 1. Increase to 100 Neurons – What’s the impact?

  1. Training takes longer, but is more accurate

  2. Training takes longer, but no impact on accuracy

  3. Training takes the same time, but is more accurate

Exercise 3#

  • What would happen if you remove the Flatten() layer.

  • Why do you think that’s the case?

Exercise 4#

  • Consider the final (output) layers.

  • Why are there 10 of them?

  • What would happen if you had a different amount than 10?

  • For example, try training the network with 5

Exercise 5#

  • Consider the effects of additional layers in the network. What will happen if you add another layer?

Exercise 6#

  • Consider the impact of training for more or less epochs (e.g. 5 or 30).

  • Why do you think the results may change ?