Worked examples — Image classification pipeline
This page is the stress test for the image classification pipeline. The parent note showed you the "happy path": a clean dog image flowing from pixels to an 85% prediction. But real pipelines meet ugly inputs — all-black images, logits that are all equal, one giant logit that explodes the exponential, a tie between two classes. If you have never seen these cases, you will misread the output the day they appear.
Below we first lay out every case class the pipeline can throw at you, then work through each one from zero.
The scenario matrix
Everything downstream of the CNN body reduces to three tiny functions acting on a logit vector (the raw scores before probabilities). Those functions are softmax (scores → probabilities) and cross-entropy (probabilities vs truth → one loss number). The matrix enumerates the corner cases of these plus preprocessing.
| # | Case class | Concrete input | What breaks / is interesting |
|---|---|---|---|
| A | Normal, one clear winner | baseline; confident correct prediction | |
| B | All logits equal (degenerate) | softmax outputs uniform — maximum uncertainty | |
| C | Huge logit (overflow risk) | overflows — need the max-subtraction trick | |
| D | Shift invariance | vs | softmax unchanged by adding a constant |
| E | Near-tie between top two | confidence just above/below threshold | |
| F | Wrong-and-confident (loss blows up) | true = class 2, | cross-entropy large |
| G | Perfect / zero prediction (limits) | or | loss or |
| H | Degenerate preprocessing input | all-black patch, channel | division by zero in normalization |
| I | Real-world word problem | quality-control camera | threshold + reject option |
| J | Exam twist | "temperature" scaling | how sharpening/softening changes probabilities |
We now cover every cell with a worked example.
[!definition] The three functions we keep reusing
Here is Euler's number and is the natural logarithm (base ), so that exactly — this pairing is why softmax uses and loss uses : they undo each other cleanly in the gradient.
[!example] Example A — Baseline: one clear winner (cell A)
Statement. Three classes, logits . Find the probabilities and the prediction.
Forecast: class 1 should win. Guess: roughly how confident — 60%? 80%? Write your guess down.
- Exponentiate each logit. Why this step? Softmax works on , not , because is always positive (probabilities can't be negative) and monotone (bigger score ⇒ bigger share).
- Sum them. Why this step? The sum is the "whole pie" we divide into.
- Divide. Why this step? Dividing by forces the parts to sum to — the probability axiom.
- Argmax. Largest is predict class 1.
Verify: ✓ (probabilities sum to one). The winner (84%) is more confident than the naive "2 out of 3" you might have guessed — softmax amplifies gaps.
[!example] Example B — All logits equal (degenerate, cell B)
Statement. . What does the network say?
Forecast: the model has no reason to prefer any class. What must the probabilities be?
- Exponentiate. for all three. Why this step? Same input ⇒ same exponential; symmetry is preserved.
- Sum.
- Divide. Why this step? Equal shares of the pie.
Verify: exactly each; sum ✓. This is the maximum-uncertainty output. Note it does not require zero logits — any equal vector like gives the same, which motivates Example D.
[!example] Example C — Huge logits & the overflow trick (cell C)
Statement. . Compute softmax without your calculator exploding.
Forecast: is astronomically large (overflows to inf on a computer). Will the answer be extreme, or is the naive computation just numerically unsafe?
- Subtract the max logit from every entry. , so Why this step? Softmax is unchanged when you shift all logits by a constant (proved in Example D). Shifting by makes the biggest exponent — no overflow.
- Exponentiate the shifted logits.
- Sum.
- Divide.
Verify: sum ✓. The spacing of logits (gaps of ) — not their absolute size — sets the probabilities. Compare Example A's gaps: here the top class gets 66%, matching what evenly-spaced logits should give. This max-subtraction is exactly what every deep-learning library does internally.
[!example] Example D — Shift invariance (cell D)
Statement. Prove softmax gives the same answer for and (add the same constant to every logit). Check with and .
Forecast: does adding a bias to all scores change the ranking or the probabilities?
- Write the shifted softmax. Why this step? — the property that lets us factor out .
- Cancel the common factor from top and bottom: Identical to the unshifted softmax. ∎
- Numeric check. : , sum , giving — same as Example A.
Verify: the shifted probabilities equal Example A's to three decimals ✓. This is why the overflow trick in C is legal, and why a network's logit bias can drift freely without changing predictions.
[!example] Example E — Near-tie and the confidence threshold (cell E)
Statement. , three classes. The pipeline outputs "uncertain" if the top probability . What does it output?
Forecast: the top two logits are almost equal. Will confidence clear the 70% bar?
- Overflow-safe: subtract .
- Exponentiate.
- Sum.
- Divide.
- Apply threshold. Top probability output "uncertain" (even though class 1 technically wins).
Verify: sum ✓. A logit gap of only between the leaders produces a near-coin-flip (52% vs 47%). The reject-option (see Multi-label classification for a related many-answers view) exists precisely for this cell.
[!example] Example F — Wrong and confident: loss blows up (cell F)
Statement. True class is class 2, but the model gave . Compute the cross-entropy loss. Compare to a model that gave the truth .
Forecast: the model is confidently wrong about class 2. Should the loss be tiny or huge?
- Pick out the true-class probability. Why this step? Cross-entropy only reads the probability of the correct answer.
- Take negative log. Why this step? of a tiny number is a large negative number; the minus sign flips it to a large positive loss — a heavy penalty for confident mistakes.
- Compare to a decent model giving :
Verify: vs — being confidently wrong costs about 20× more loss. This asymmetry is what pushes Backpropagation to fix egregious errors first.
[!example] Example G — The two limits of loss (cell G)
Statement. What is the loss as (perfect) and as (utterly wrong)? Illustrate the curve.
Forecast: best case and worst case — sketch the shape before reading.

- Perfect prediction. Why this step? ; a perfectly confident correct answer incurs zero loss — the floor.
- Confidently wrong. Why this step? of numbers approaching heads to ; the minus makes loss . There is no ceiling — the curve (red in the figure) shoots up vertically at .
- Read off two waypoints.
Verify: the figure's red curve passes through , , ✓ and grows without bound near . This unbounded penalty is exactly why we clamp away from in code (add a tiny ).
[!example] Example H — Degenerate preprocessing: a dead channel (cell H)
Statement. A calibration frame is a flat grey patch: every pixel in the red channel equals . You try to normalize with the patch's own statistics . What happens, and what's the fix?
Forecast: every value equals the mean. What does subtracting the mean give — and then dividing by ?
- Center. for every pixel. Why this step? Subtracting the mean is fine; a constant channel just becomes all zeros.
- Scale — and hit the wall. , which is undefined (division by zero). Why this step? means "no spread"; there is nothing to rescale, so the formula is degenerate.
- The standard fix: use fixed dataset stats, not per-image stats. With ImageNet red-channel and : Why this step? Using global constants (or adding to ) guarantees a finite, meaningful output even for flat inputs. This is also why Batch normalization adds a small inside its own denominator.
Verify: and ✓ — finite. The lesson: never divide by an input's own without a guard.
[!example] Example I — Real-world word problem: a QC reject camera (cell I)
Statement. A factory camera classifies each part as good, scratched, or cracked. For one part the logits are . Policy: flag cracked (send to human) if ; otherwise auto-pass as good only if ; else route to manual review. What happens to this part?
Forecast: the cracked logit is the biggest — will it clear the 0.5 flag bar?
- Overflow-safe: subtract .
- Exponentiate.
- Sum & divide. Order: (good, scratched, cracked).
- Apply policy. flag as cracked, send to human.
Verify: sum ✓, and triggers the safety route. The good rule never even fires — correct, because we check the danger class first. Deciding on multiple thresholds like this is the bridge to Object detection confidence gating.
[!example] Example J — Exam twist: temperature scaling (cell J)
Statement. Take (same as Example A). "Temperature" scaling replaces by before softmax. Compute the class-1 probability for a cold (sharpen) and a hot (soften), and state the limits as and .
Forecast: dividing by a small blows the gaps up; dividing by a large shrinks them. Which way does confidence move?
- Cold, : scale logits. Why this step? Small multiplies every gap by , exaggerating differences.
- Softmax. , sum
- Hot, : scale logits. Why this step? Large shrinks gaps, flattening toward uniform.
- Softmax. , sum
- Limits. As : winner (a hard argmax). As : all logits ⇒ uniform (Example B).
Verify: cold (more confident than A's ), hot (less confident) ✓, and the two limits bracket everything between one-hot and uniform. Temperature is the standard knob for calibrating an overconfident classifier — often paired with Transfer learning models that ship over-sharp.
[!recall]- Quick self-test
Softmax of over 3 classes?
Why subtract before exponentiating?
Cross-entropy loss when the true-class probability is exactly ?
Cross-entropy as ?
What breaks when you normalize a flat patch with its own ?
Effect of a small temperature on softmax?
Where to go next
- Softmax activation — the function stressed here, in full.
- Cross-entropy loss — the loss whose limits Examples F & G explored.
- Batch normalization — how the -guard from cell H reappears mid-network.