Model · Oct 2026 · runs in your browser
Small CNN
The MLP on the front page treats every pixel as its own input, so a digit shifted two pixels is a new problem. A convolution slides the same small filter everywhere. This network has 14× fewer parameters than the MLP and makes fewer mistakes.
- Test accuracy
- 99.41%
- Parameters
- 7,578
- Model file
- 32 KB, ONNX
- Training time
- 39 s, GPU
Try it
Waiting for a digit
Loading the runtime…
The exported ONNX graph runs in onnxruntime-web on WebAssembly. Besides the probabilities it returns two intermediate activations, drawn above: the first layer responds to strokes and edges at every position, the fourth to larger parts of the digit.
Training run
Results
| Metric | Value | Source |
|---|---|---|
| Test accuracy, 10,000 images | 99.41% | training/cnn_mnist/log.csv, epoch 15 |
| Test loss | 0.0199 | training/cnn_mnist/log.csv, epoch 15 |
| Train accuracy (augmented) | 99.19% | training/cnn_mnist/log.csv, epoch 15 |
| Test accuracy of the exported ONNX file | 99.41% | public/models/cnn-mnist/meta.json |
| MLP on the same test set, for comparison | 98.69% | training/mlp_numpy/log.csv |
How it works
Architecture. Five 3×3 convolutions with batch norm and ReLU, one max pool after the second, then a 1×1 convolution to ten channels and a global average pool. The spatial size goes 28, 26, 24, 12, 10, 8, 6. There is no fully connected layer: averaging each class map over the image is the classifier, which is where most of the parameter saving comes from.
Training. SGD with Nesterov momentum 0.9, batch 128, weight decay 0.0005, a one-cycle learning rate peaking at 0.05 over 15 epochs. Each batch is rotated by up to ±10° and shifted by up to 10% of the image, on the GPU.
Export. The model is exported to ONNX with the softmax and the two feature-map taps built into the graph, then the exported file itself is scored on all 10,000 test images, because that file is what runs here, not the PyTorch model.
Reproduce
- Hardware
- NVIDIA GeForce RTX 3050 Laptop GPU, PyTorch 2.6.0+cu124
- Commit
- eeb3064
- Data
- MNIST, 60,000 train / 10,000 test, downloaded and MD5-checked by the script
Environment
pip install torch numpy onnx onnxruntimeTrain, export and score the ONNX file
python training/cnn_mnist/train.py