MNIST Digit Classifier
An MNIST digit classifier is a neural network trained on the MNIST dataset of handwritten digits: given a 28×28 grayscale image, it returns a probability for each digit 0–9 and predicts the most likely one. This page runs a trained 784 → 25 → 25 → 10 network live in your browser on whatever you draw.
Reading handwritten zip codes on envelopes is a classic application of digit recognition. Here, your strokes stand in for the handwritten address and the network acts as the sorting machine, turning pixels into a digit prediction in one pass of arithmetic. Draw a digit on the grid to inspect both the predicted class and the probability assigned to each digit.
How the MNIST Digit Classifier Works
Classification runs as a single forward pass. Your drawing becomes 784 numbers, one per pixel. The first hidden layer computes 25 weighted sums z = x·W + b and clamps negative values to zero with ReLU; the second hidden layer repeats the same step. A final layer produces ten raw scores, and softmax converts them into probabilities that sum to 1. The network performs 20,475 connection multiply-adds (784·25 + 25·25 + 25·10), excluding biases, activations, and softmax overhead. Because that cost is small, the browser reruns the pass on every pointer stroke and updates the prediction while you draw.
Preprocessing happens before the forward pass. MNIST images are not arbitrary 28×28 pictures: each digit was scaled to fit a 20×20 box and centered in the frame by its center of mass. Because the network was trained only on digits framed this way, your drawing goes through the same pipeline (find the ink's bounding box, scale it into the 20-pixel box, and recenter by center of mass) before evaluation. The large grid shows your strokes as drawn, while the small "what the network sees" grid below it shows the normalized copy fed into the forward pass. When a mark drawn in a corner of the large grid shifts to the middle of the small one, preprocessing is aligning the input to match the training framing.
MNIST Digit Classifier Step by Step
The canvas starts blank. Each stroke passes through five stages:
- You draw. Your pointer paints soft-edged strokes into a high-resolution ink buffer (10× finer than the 28-pixel grid), keeping curves smooth instead of blocky.
- Two downsamples happen. The display grid receives a direct box-averaged 28×28 copy as drawn. The network receives the MNIST-normalized copy: ink bounding box → scaled into a 20×20 box → recentered by center of mass → 784 input values.
- The forward pass runs. Each of the 25 first-layer neurons computes its weighted sum over all 784 pixels; ReLU zeroes negative sums; the second layer of 25 repeats this step on those activations.
- Softmax votes. Ten raw output scores become ten probabilities. The largest is the prediction, shown in the header with its percentage; the runner-up's share is also labeled so the remaining probability mass is visible.
- You test and compare. Redraw, distort your strokes, or press one of the ten sample buttons in the panel to load a real MNIST test image that this network classifies correctly as a calibration point against your own handwriting.
On the canvas, hidden neurons fill dark when their weighted sum is positive and blue when negative; connecting wires brighten in proportion to the activation they carry while idle wires fade; the ten output circles fill by probability, and the predicted class receives a green ring. A clean bold 3 concentrates activation into one output circle, whereas an ambiguous 7 that resembles a 1 splits probability across two circles.
What the Network Actually Learns
The network does not store digit templates. There is no saved image of a "7" inside these weights, only over 20,000 learned numbers whose weighted sums push the right output score upward on training examples. Individual hidden neurons rarely correspond to a nameable feature; neuron 14 responds to a weighted combination of pixel patterns that reduced training loss. Output classes have explicit meanings, whereas hidden features can be inspected but rarely admit simple names.
Softmax reports confidence rather than correctness or calibrated probability. Because the ten probabilities must sum to 1, the network has no way to say "this isn't a digit": feed it a scribble or a letter and it still produces a distribution, sometimes a peaked one. On inputs resembling MNIST, high confidence correlates with accuracy; far from MNIST, a 95% output carries no such relationship. The companion Forward Propagation lesson includes real test images that this same network misclassifies at over 90% confidence in its Misclassified variant.
Edge Cases
- Off-center or tiny drawings. Without recentering, these can be misread because their framing does not match the training data. Normalization reduces framing mismatch without guaranteeing a correct read; sampling, stroke width, and handwriting style can still differ from training data.
- A blank canvas. With no ink, the outputs show the network's fixed response to an all-zero input rather than a prediction.
- Faint strokes. Preprocessing depends on the ink's bounding box, so a mark too faint to register leaves the network reading near-zero input.
- Confident mistakes. The network can assign high confidence to the wrong class, especially on digits written in styles MNIST's writers never used. Compare both grids first: if "what the network sees" looks ambiguous to you, the miss is expected.
Common Mistakes
- Expecting production accuracy. This is a small two-hidden-layer MLP with no convolutions, so it has no built-in invariance to shape or translation beyond what its weights absorbed. Production digit readers may use CNNs or other architectures and different training data; greater model size is not a requirement in every deployment.
- Blaming the drawing tool for every miss. Some inputs are ambiguous to human readers too, such as a leaning 1. Split probabilities are often the correct answer.
- Reading hidden neurons as single concepts. A single unit lighting up is not "the loop detector." Meaning is distributed across the population and becomes directly legible only at the output.
- Treating the percentage as a guarantee. A 95% score measures how far the scores separated, not a promise that 95 of 100 similar inputs will be classified correctly.
Watch the 4K narrated walkthrough
A 6:21 English walkthrough on YouTube built directly from this interactive canvas.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Premium players need paid access. Every Explain doc is free.
Embed this lesson in a blog, docs, or course?
Found a mistake or something unclear?