!pip install jovian --upgrade --quiet
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

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

print(tf.__version__)
2.3.0

Lab 4

1. Defining Convolutions and Pooling layers for Fashion MNIST Classifier

a. Type the code given in the lecture notes Lecture 4 page 19 into your notebook (fashion MNIST classifier using CNNs).

mnist = keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()

# Convolution layer expects a single tensor containing everything, so instead of 60,000 items
# of dimension 28x28x1 in a list, we have a single 4D list that is 60,000x28x28x1
training_images = training_images.reshape(60000, 28, 28, 1)
training_images = training_images / 255.0
test_images = test_images.reshape(10000, 28, 28, 1)
test_images = test_images / 255.0

model = keras.models.Sequential([
  keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(28, 28, 1)),
  keras.layers.MaxPooling2D(2, 2),
  keras.layers.Conv2D(64, (3,3), activation='relu'),
  keras.layers.MaxPooling2D(2, 2),
  keras.layers.Flatten(),
  keras.layers.Dense(128, activation = 'relu'),
  keras.layers.Dense(10, activation = 'softmax')
])

model.compile(optimizer='adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(training_images, training_labels, epochs = 5)
test_loss = model.evaluate(test_images, test_labels)
Epoch 1/5 1875/1875 [==============================] - 4s 2ms/step - loss: 0.4463 - accuracy: 0.8366 Epoch 2/5 1875/1875 [==============================] - 4s 2ms/step - loss: 0.2964 - accuracy: 0.8911 Epoch 3/5 1875/1875 [==============================] - 5s 2ms/step - loss: 0.2502 - accuracy: 0.9067 Epoch 4/5 1875/1875 [==============================] - 5s 2ms/step - loss: 0.2195 - accuracy: 0.9178 Epoch 5/5 1875/1875 [==============================] - 5s 2ms/step - loss: 0.1931 - accuracy: 0.9274 313/313 [==============================] - 1s 2ms/step - loss: 0.2641 - accuracy: 0.9044

b. Run the code and observe the summary and model accuracy. Does it take longer? Is the accuracy better? (Previous model had 89% accuracy on test set and 87% accuracy on test set)

It takes a bit longer, however the results are better. 93% compared to 89% accuracy for the model and 89% compared to 87% on the test data (overfitting is more severe).