Classifying Handwritten Digits

Handwritten Digits


We will try to solve the problem of classifying black-white(28x28pixels) images composed of handwritten digits divided into 10 (0–9) categories. We will use MNIST dataset. The MNIST dataset consists of a total of 70000 images, 60000 for training and 10000 for testing, created by The National Institute of Standards and Technology (NIST) in the 1980s. The MNIST dataset can be considered as the “Hello World” of the deep learning.


Library Imports

We need to import necessary Keras modules.

from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical

Preparing Data

The MNIST dataset comes divided into training and test sets as numpy arrays over Keras.

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

We preprocess the data by reshaping it into the shape the network expects and scaling it so that all values are in the [0, 1] interval. We convert array of shape (60000, 28, 28) of type ‘uint8’ to array of shape (60000, 28*28) of type ‘float32’.

train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255

We need to define our labels as categorical.

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

Design Network Architecture

We define our network architecture. We use sequential model. First layer is dense(fully connected), activation function is rectified linear unit(relu). Second layer is dense(fully connected), activation function is softmax.

network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))

Training Network

We configure our compile step. The our network has RMSProp optimizer function, categorical cross entropy loss function and accuracy metrics.


We need to use the fit method to train our network. The fit method has train_images, train_labels, epochs(the number of training repetitions), batch_size (the number of training samples) parameters., train_labels, epochs=5, batch_size=128)

Testing Network

We evaluate the model according to test_images and test_labels. We get the test loss and test accuracy values.

test_loss, test_acc = network.evaluate(test_images, test_labels)

We show test accuracy as float format.

print("test_acc:", test_acc)

We show test accuracy as percentage format.

print("Test Accuracy: {:.0%}".format(test_acc))

The Output of Network




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store