Handwritten Digit → Prediction
Handwritten digit prediction is an inference workflow that turns a 28×28 image into ten model scores, converts those scores into probabilities, commits one predicted digit, and routes the result through a decision policy. The policy can react to confidence, but only a later comparison with held-out truth can say whether the prediction was correct.
Think of the result as a job packet moving down an assembly line. The model fills in scores and confidence; an 80% gate decides where the packet goes; the true label arrives later as an independent audit record. Keeping those jobs separate exposes a failure that a neat probability chart can hide: a wrong answer can look certain enough to sail through the gate.
How Handwritten Digit Prediction Works
The input is a real MNIST test image containing 784 byte intensities. This lesson sends those pixels through a fixed 784 → 25 → 25 → 10 multilayer perceptron. Its weights do not change between runs, and it never receives the true label. One pass performs 20,475 learned-weight products plus 60 bias additions; the animation groups that familiar neural-network machinery into one model station so you can follow what happens after inference.
The model emits ten logits, one signed score for each digit class. Logits are comparable, but they are not probabilities: they can be negative, do not have a fixed upper bound, and do not sum to one. The score race uses one fixed axis from -40 to 30, wide enough for every logit produced by the lesson's 30 samples. A shared axis matters because otherwise each run could make its winner look equally dominant merely by rescaling the picture.
Stable softmax converts the logits into a unit-sum distribution:
p_i = exp(logit_i - max(logits)) / sum_j exp(logit_j - max(logits))
Subtracting the maximum prevents needlessly large exponentials. It does not change the ordering, so the largest logit remains the largest probability. The visual tray rounds the distribution into exactly 100 cells using largest-remainder rounding; the printed labels keep finer, still rounded detail: two-decimal percentages, scientific notation for tiny shares, and >99.99% as a lower bound.
Argmax then commits the class with the largest probability as the prediction. That is the first answer in the workflow, but not the final verdict. A fixed illustrative policy routes packets with topProbability >= 0.80 to Auto-accept and everything below 0.80 to Review. The policy reads only the top probability. It cannot read the true label because that label is still sealed.
After routing, held-out truth enters from a separate evaluation rail. The audit compares prediction with trueLabel and marks the packet correct or wrong. This ordering is not visual theater; it is the data contract. Letting truth enter earlier would leak the answer into inference or routing and destroy the test.
Handwritten Digit Prediction Step by Step
The default run uses d7-b, an ambiguous real test image whose true label is 7. The model predicts 7 correctly at 74.84%, yet the 80% policy sends the packet to review.
- Intake. The packet receives the 28×28 image and sample id
d7-b. Its true label remains sealed. - Infer. The fixed model processes all 784 pixels in one grouped pass and emits ten logits. No training or weight update occurs.
- Scores. Ten class tickets arrive at the zero line. At this point they are signed model scores, not probabilities or predictions.
- Race. Each ticket moves to its real logit on the shared
[-40, 30]axis. Class 7 stops at4.834; class 9 stops at3.737, so the winning margin is1.097. - Softmax flow. The two leading classes keep their identity while the other eight scores compress into one quiet remainder stream. No probability cells fill yet, so movement and quantity do not compete for your attention.
- Softmax mass. The three streams settle into 100% probability mass. Class 7 receives 74.84%, class 9 receives 24.99%, and the remaining eight classes share the small remainder.
- Receipt. The model output becomes an auditable record carrying the sample id, model version, top scores, and exact confidence. The true label is still sealed.
- Predict. The largest share stamps
prediction = 7onto the packet. Correctness is still unknown. - Gate. Because 74.84% is below 80%, the packet takes the Review lane. The route is valid even though the prediction will prove correct.
- Reveal. Only after routing does the evaluation envelope open and reveal
trueLabel = 7. - Audit. Prediction 7 matches truth 7, so the verdict is correct. The route stays Review; audit does not rewrite history.
- Contrast. A second real receipt for the same digit appears beside the default:
d7-bis74.84% · REVIEW · CORRECT, whiled7-mis99.79% · AUTO-ACCEPT · WRONG. The model and policy are unchanged; only the held-out input differs. - Done. Both receipts remain parked on screen. Confidence explains the route, while the independently revealed true label determines the verdict.
The final comparison performs the important sample switch inside the default run, so the lesson's claim no longer depends on opening Config. You can still choose Decisive, Ambiguous, or Misclassified to inspect the full route for any digit from 0 through 9.
Why a Confidence Threshold Cannot Prove Correctness
The 80% gate answers one narrow question: did this model concentrate at least 80% of its softmax mass on its favorite class? It does not answer whether the favorite class matches reality. Those are different variables produced by different parts of the workflow.
Across the 30 curated samples, the distinction is exact:
| Sample kind | Results across digits 0–9 | Top probability | Route at 80% |
|---|---|---|---|
| Decisive | 10/10 correct | Above 99.999% | 10/10 Auto-accept |
| Ambiguous | 10/10 correct | 74.56%–75.29% | 10/10 Review |
| Misclassified | 10/10 wrong | 99.26%–99.99% | 10/10 Auto-accept |
The gate cleanly separates the low-confidence set from the two high-confidence sets. It completely fails to separate correct from incorrect predictions. That does not make thresholds useless. A threshold can ration human review, implement an abstention policy, or enforce a service rule. It just cannot turn an uncalibrated model score into proof.
Lowering the threshold changes traffic, not truth. At 70%, the default d7-b packet would move from Review to Auto-accept while its prediction and correctness stayed exactly the same. Raising it can send more packets to review. Choosing that number is a policy decision about operational cost and risk; evaluating the resulting decisions still requires held-out outcomes.
Edge Cases
- A top probability of
>99.99%is displayed as a finite probability, never as mathematical certainty. - Tiny nonzero probabilities use scientific notation instead of rounding to a false zero.
- A score exactly equal to 80% auto-accepts because the implemented comparison is
>=, not>. - The 100-cell tray is a display device. Largest-remainder rounding makes the groups total exactly 100 cells, while the printed percentages remain the source of numerical detail.
- Adding the same constant to every logit leaves softmax probabilities unchanged. Only score differences affect the distribution.
- An all-zero image would still produce logits because the model has biases. The surrounding system must decide whether such an input is valid.
Common Mistakes
- Calling logits probabilities. Logits are signed scores; softmax converts them into a unit-sum distribution.
- Assuming softmax checks truth. It only transforms the model's own scores and preserves their ranking.
- Treating Review as a prediction error. The default packet is reviewed and correct.
- Treating Auto-accept as proof. Every curated misclassified sample passes the 80% gate with the wrong digit.
- Choosing a threshold before stating the cost of review and error. The model cannot supply that product decision.
- Feeding the true label into inference or routing. That leaks the answer and invalidates the evaluation.
- Comparing confidence across visuals with independently scaled logit axes. This lesson keeps one
[-40, 30]axis across all samples to prevent that illusion.
A Note on Simplification
This is an explainer, not a production inference service. It uses a small dense MNIST MLP with two 25-unit hidden layers and committed weights. The model press compresses normalization, affine maps, and ReLUs into one beat; the dedicated forward-propagation and MNIST lessons teach those internals in detail.
The 80% auto-accept rule is illustrative. The lesson does not claim the softmax probabilities are calibrated, and it omits calibration measurement, distribution-shift detection, model registry and rollout machinery, monitoring, retries, queues, privacy controls, and the actual work a human reviewer would perform. Production systems also audit outcomes over many cases, not one envelope at a time. The narrow claim here survives those omissions: routing by confidence and judging correctness are separate operations.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Found a mistake or something unclear? Leave feedback.