Teaching a model to read written numbers

Draw a number here and let the model guess what it is. Did it get your number right? This experiment uses a tiny convolutional neural network (CNN) trained on the legendary MNIST dataset. A few interesting tidbits about the data, the model, and its results are below.

Try the recognizer View code
98.94%Test accuracy
5Training epochs
121,930Trainable parameters

Training, at a glance

See how accuracy rose and loss fell across the five training epochs.

Accuracy by epoch

Percent correct. Vertical axis shown from 94% to 100%.

Training and validation accuracy over five epochs Training accuracy rises from 95.64 percent to 99.47 percent. Validation accuracy rises from 98.45 percent to 99.10 percent. 100%98%96%94% 12345Epoch
TrainingValidation

Loss by epoch

Lower is better. Vertical axis starts at zero.

Training and validation loss over five epochs Training loss falls from 0.1472 to 0.0166. Validation loss falls from 0.0519 to 0.0347, with a small rise after epoch three. 0.160.120.080.040 12345Epoch
TrainingValidation

What happens when you draw

The recognizer has two parts: a model trained in Python and a small browser interface. The Python model learns patterns in digit images. The web page prepares a drawing in the same general format, passes it to the model, and displays its ten output scores.

  1. Draw: the canvas records white strokes against a dark background.
  2. Prepare: the page finds the ink, crops around it, scales it to fit inside a 28 × 28 pixel image, and normalizes pixel values to the 0–1 range.
  3. Predict: the TensorFlow Lite model returns one score for each digit, from 0 through 9.
  4. Show: the page displays the highest-scoring digit and all ten scores.

The prediction runs on the visitor’s device. The page downloads the model and JavaScript runtime, then performs inference in the browser; it does not need a prediction server.

The training data: MNIST

MNIST is a standard dataset of handwritten digits. It contains 70,000 small grayscale images: 60,000 examples in the training split and 10,000 in the test split. Each image is 28 × 28 pixels and has a label from zero to nine.

In the usual representation, the digit is light and the background is dark. The pixel values are scaled from integers in the 0–255 range to floating-point values in the 0–1 range. A channel dimension is added so each example has the shape 28 × 28 × 1 expected by the convolutional layers.

How the split was made: the Colab notebook uses a 10% validation split, leaving 54,000 images for training and 6,000 for validation. It does not set a batch size, so Keras uses its default of 32. That matches the 1,688 training steps and 313 test steps shown in the saved output.

The model

This is a convolutional neural network (CNN). The convolution layers learn local visual patterns such as edges and curves. Pooling reduces the image dimensions while keeping useful features. The final dense layers combine those features and produce ten class scores.

LayerOutput shapeParametersRole
Conv2D, 32 filters, 3 × 326 × 26 × 32320Find simple image patterns
MaxPooling2D, 2 × 213 × 13 × 320Reduce spatial size
Conv2D, 64 filters, 3 × 311 × 11 × 6418,496Combine patterns into features
MaxPooling2D, 2 × 25 × 5 × 640Reduce spatial size
Flatten1,6000Prepare features for dense layers
Dense, 64 units64102,464Combine learned features
Dense, 10 units, softmax10650Score the ten digit classes

The network has 121,930 trainable parameters. The Keras summary reported 365,792 total parameters because that total also included 243,862 optimizer parameters. The optimizer state is used during training; it is not part of the browser’s predictions.

The architecture, activations, and parameter counts above match the Colab notebook and its saved model summary. Layer names have been simplified for readability.

Epoch-by-epoch results

The model was trained for five epochs. The final evaluation on the separate MNIST test split reported 98.94% accuracy and a loss of 0.0318.

EpochTrain accuracyTrain lossValidation accuracyValidation loss
195.64%0.147298.45%0.0519
298.53%0.046998.90%0.0358
398.99%0.032899.05%0.0309
499.21%0.024099.03%0.0336
599.47%0.016699.10%0.0347

Training accuracy rose from 95.64% to 99.47%. Validation accuracy finished at 99.10%, and test accuracy was 98.94%.

A sense of scale

A tiny CNN works remarkably well for a focused task like recognizing digits, but this model’s ten possible answers are a long way from reading a whole page or holding a conversation. It has 121,930 learned parameters; OpenAI’s GPT-3 had 175 billion in 2020—about 1.4 million times as many. Size alone does not explain intelligence, but that gap gives some perspective on the computation and memory larger systems need, and why NVIDIA’s GPUs play such a prominent role in the AI boom.