Skip to main content

Your turn: an exercise

This page is part of the material for the "Introduction to Tensorflow" session of the 2020 CISM/CÉCI trainings, see the table of contents for the other parts. The notebook can be downloaded with this link. Colab

As an exercise, we will train a neural network to recognize hand-written digits from the MNIST dataset; if you prefer you can classify fashion categories with the Fashion-MNIST dataset (they both have grayscale images in ten categories, so as far as the machine learning goes, they are fairly similar). These datasets are included with keras (see also the full list) for demonstration purposes; larger datasets are available in the Tensorflow datasets package.

The code below is largely taken from this keras example, but please try to figure it out by yourself first.

In [1]:
import tensorflow as tf
nClass = 10 # 0-9
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)
(60000, 28, 28) (60000,) (10000, 28, 28) (10000,)

So each example is a 28x28 image, and there are 60000 in the training, and 10000 in the test dataset.

In [2]:
from matplotlib import pyplot as plt
fig,ax = plt.subplots(4,4)
for i in range(4):
    for j in range(4):
        ax[i][j].imshow(x_test[i*4+j])
No description has been provided for this image

It turns out these are values between 0 and 255 (8 bits per pixel), so we start by rescaling them to the 0-1 range

In [3]:
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255

Image recognition architectures also have a depth dimension, of which we have only here, so we will add one

In [4]:
import numpy as np
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
print(x_train.shape)
(60000, 28, 28, 1)

Now it is your turn: try to build a neural network that classifies these 28x28 images as digits from 0 to 9.

Good luck! Colab