Handwritten digit recognition
Ten experiments on the MNIST handwritten digits, from a network with a single hidden node to deep networks with batch normalization. The best one reads 98.1% of test digits correctly. Try it below with your own handwriting.
Test the model
Runs in your browser. Your photo is not uploaded anywhere.Upload a photo
Or draw a digit
Or pick a sample from the test set
How it works: your picture is turned to grey, cropped to the digit, shrunk to 28 by 28 pixels and centred, which is how the MNIST digits were prepared. The network then gives a probability for each digit from 0 to 9. It was trained only on MNIST's handwriting, so it can misread printed numbers, digits written with a tail or a crossbar, and photos with shadows or lined paper.
The experiments
Each experiment trains a fully connected network on 55,000 digits for 30 epochs, checks it on 5,000 validation digits, and scores it on the 10,000 test digits.
The first two use one and two hidden nodes, to see how much a tiny network can separate ten classes. The later ones add depth, compare activations, optimizers and regularization, and try shrinking the input with PCA and with a random forest's choice of the 70 most useful pixels.
Batch normalization gave the best result. The live demo uses that network: 784 inputs, then 128, 64 and 32 hidden nodes, then 10 outputs.
The notebook did not save its trained model, so for this page I retrained the same network with the same recipe. It scored 98.1% on the test set, matching the notebook.
| Experiment | Network | Test accuracy |
|---|---|---|
| 1 | 1 hidden node | 40.2% |
| 2 | 2 hidden nodes | 72.1% |
| 3 | 32, 16, 8 nodes, ReLU | 96.3% |
| 4 | 64, 32, 16 nodes, tanh | 97.2% |
| 5 | 128, 64, 32 nodes, batch normalization | 98.1% |
| 6 | 128, 64, 32 nodes, dropout | 97.9% |
| 7 | 128, 64, 32 nodes, AdamW | 97.7% |
| 8 | 64, 64, 64 nodes, batch normalization, SGD | 98.0% |
| 9 | 85 nodes, all 784 pixels | 97.5% |
| 9 | 85 nodes, 154 PCA components | 97.5% |
| 10 | 85 nodes, 70 pixels chosen by a random forest | 93.9% (validation) |
Accuracies are from the notebook. Experiment 10 reports validation accuracy only. PCA kept 95% of the variance in 154 components and matched the full 784 pixels.