This is a self-correcting activity generated by nbgrader. Fill in any place that says YOUR CODE HERE or YOUR ANSWER HERE. Run subsequent cells to check your code.


Fashion-MNIST

Fashion-MNIST is analogous to the MNIST handwritten digits dataset. It consists of 60,000 28x28 grayscale images (pixel values between 0 and 255) of 10 fashion categories, along with a test set of 10,000 images. The goal here is to associate a fashion image with its class.

Fasion items

Each training and test example is assigned to one of the following label (fashion category):

Label

Description

0

T-shirt/top

1

Trouser

2

Pullover

3

Dress

4

Coat

5

Sandal

6

Shirt

7

Sneaker

8

Bag

9

Ankle boot

Environment setup

# Import base packages
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# Setup plots
%matplotlib inline
plt.rcParams['figure.figsize'] = 10, 8
%config InlineBackend.figure_format = 'retina'
sns.set()
# Import ML packages
import tensorflow as tf
print(f'TensorFlow version: {tf.__version__}')
print(f'Keras version: {tf.keras.__version__}')

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.datasets import fashion_mnist # Added in Keras 2.0.9
from tensorflow.keras.utils import to_categorical

Utility functions

def plot_loss_acc(history):
    """Plot training and (optionally) validation loss and accuracy"""

    loss = history.history['loss']
    epochs = range(1, len(loss) + 1)

    plt.figure(figsize=(10, 10))

    plt.subplot(2, 1, 1)
    plt.plot(epochs, loss, '.--', label='Training loss')
    final_loss = loss[-1]
    title = 'Training loss: {:.4f}'.format(final_loss)
    plt.ylabel('Loss')
    if 'val_loss' in history.history:
        val_loss = history.history['val_loss']
        plt.plot(epochs, val_loss, 'o-', label='Validation loss')
        final_val_loss = val_loss[-1]
        title += ', Validation loss: {:.4f}'.format(final_val_loss)
    plt.title(title)
    plt.legend()

    acc = history.history['accuracy']

    plt.subplot(2, 1, 2)
    plt.plot(epochs, acc, '.--', label='Training acc')
    final_acc = acc[-1]
    title = 'Training accuracy: {:.2f}%'.format(final_acc * 100)
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    if 'val_accuracy' in history.history:
        val_acc = history.history['val_accuracy']
        plt.plot(epochs, val_acc, 'o-', label='Validation acc')
        final_val_acc = val_acc[-1]
        title += ', Validation accuracy: {:.2f}%'.format(final_val_acc * 100)
    plt.title(title)
    plt.legend()

Step 1: loading the data

Question

Load the Fashion-MNIST dataset included with Keras. Use variables named train_images, train_labels, test_images and test_labels to store the data.

# YOUR CODE HERE
print(f'Training images: {train_images.shape}. Training labels: {train_labels.shape}')
print(f'Training labels: {train_labels}')
assert train_images.shape == (60000, 28, 28)
assert train_labels.shape == (60000, )
assert test_images.shape == (10000, 28, 28)
assert test_labels.shape == (10000, )

Question

Display the first 10 test images.

# YOUR CODE HERE

Step 2: preparing the data

Question

Create variables x_train and x_test from training and test images, with appropriate rescaling.

# YOUR CODE HERE
print(f'x_train: {x_train.shape}. x_test: {x_test.shape}')

# Assert tensors shapes
assert x_train.shape == (60000, 28, 28)
assert x_test.shape == (10000, 28, 28)

# Assert tensors values
assert (np.amin(x_train, axis=0) >= 0).all() and (np.amax(x_train, axis=0) <= 1).all()
assert (np.amin(x_test, axis=0) >= 0).all() and (np.amax(x_test, axis=0) <= 1).all()

Question

One-hot encode training and test targets into variables y_train and y_test.

# YOUR CODE HERE
# Show a sample of encoded targets
df_y_train = pd.DataFrame(y_train)
df_y_train.sample(n=10)
print(f'y_train: {y_train.shape}. y_test: {y_test.shape}')

# Assert one-hot encoding of training and test targets
assert y_train.shape == (60000, 10)
assert y_test.shape == (10000, 10)

# Assert samples values
assert np.array_equal([0,0,0,0,0,0,0,0,0,1], y_train[0])
assert np.array_equal([1,0,0,0,0,0,0,0,0,0], y_train[1])
assert np.array_equal([0,0,0,0,0,0,0,0,0,1], y_test[0])
assert np.array_equal([0,0,0,0,0,1,0,0,0,0], y_test[9999])

Step 3: training a model

Question

Train a model on the data to obtain a test accuracy > 84%. Store the training history in a variable named history.

# Create and train a model

# YOUR CODE HERE
# Plot training history
plot_loss_acc(history)
# Evaluate the model on test data
_, test_acc = model.evaluate(x_test, y_test, verbose=0)
print(f'Test accuracy: {test_acc * 100:.2f}%')

assert test_acc > 0.84

Question

Show the first 10 test images with their predicted category (“dress”, “shirt”, etc).

# Item descriptions, indexed by label value
descriptions = ['t-shirt', 'trouser', 'pullover', 'dress',
                'coat', 'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']

# YOUR CODE HERE