Worked examples — Loss functions - MSE, cross-entropy
This page is the exhaustive worked-examples companion to the parent note on MSE & cross-entropy. The parent told you what the formulas are and why they exist. Here we make sure you never meet a case you have not already solved by hand: every sign of error, the zero-error and worst-case extremes, both binary and multi-class, a real-world word problem, and an exam-style trap.
Before we compute anything, some reminders so no symbol is unearned:

The scenario matrix
Every case this topic can throw at you falls into one of these cells. The examples that follow are labelled with the cell(s) they cover, and together they fill every row.
| Cell | Loss | The scenario it stress-tests |
|---|---|---|
| A — negative error | MSE | prediction below truth () |
| B — positive error | MSE | prediction above truth () |
| C — zero error (degenerate) | MSE | perfect prediction, loss |
| D — mixed batch | MSE | many points, signs mix, we average |
| E — confident & correct | BCE | when : loss |
| F — confident & WRONG (limit) | BCE | when : loss |
| G — maximum uncertainty | BCE | : the coin-flip baseline |
| H — the branch | BCE | truth is the negative class |
| I — multi-class from logits | CE + softmax | raw scores probs loss |
| J — real-world word problem | MSE | units, scaling, interpretation |
| K — exam trap | any | why MSE-on-classification stalls |
Cells A, B, C — one point, every sign of error (MSE)
- Compute the raw errors .
- , , . Why this step? The sign tells us the direction we under/over-shot. Notice A and B have opposite signs — squaring will erase that, which is exactly what we want for a "how wrong" number.
- Square each error.
- , , . Why this step? Squaring makes every penalty positive and bends the penalty curve upward, so the miss costs the miss — not . That is the quadratic bowl in the figure above.
Verify: ratio of penalties , matching "twice as far ⇒ four times as bad". Cell C confirms the degenerate case: a perfect prediction has exactly zero loss, the bottom of the bowl. Units: (lakh)² — squared, which is why we usually quote MSE relative to a baseline, not as money.
Cell D — a mixed batch, averaged (MSE)
- Square first, then average — never the other way. Why this step? If we averaged the raw errors we'd get , which pretends the model is "almost right" — the and mislead by cancelling. Squaring before summing kills all cancellation, so a batch of half-too-high and half-too-low still reports real error.
- Divide by . Why this step? Dividing by makes the number comparable across batch sizes — a 3-house MSE and a 3000-house MSE live on the same scale. This is the same that sits on the front of BCE and CE too.
Verify: . Sanity check: the mean loss must sit between the smallest per-point loss () and the largest () — and does. ✓
Cells E, F, G — binary cross-entropy across its whole range
Because , the BCE formula collapses: . Only the first term survives.
- Confident & correct (Cell E): . Why so small? The truth was highly probable under the model, so little surprise. On the surprise curve (right panel of the figure) is far right, near the floor.
- Confident & wrong (Cell F): .
Why so large? The truth had only probability — a nasty surprise. As , : the loss is unbounded, punishing overconfidence savagely. (Recall is never exactly — libraries clamp it, so you get a big finite number, not
NaN.) - Maximum uncertainty (Cell G): . Why this exact value? means "I'm guessing." is the famous coin-flip baseline — if your training BCE hovers here, the model has learned nothing.
Verify: ratio — over twenty times worse, far beyond most people's forecast. And . ✓ Limit check: try , then , still finite but growing without bound as we approach — confirming Cell F's "" claim.
Cell H — the OTHER branch: truth is the negative class
- Substitute into . Why this step? When the first term is multiplied by zero and vanishes; the second term switches on. This is the mirror image of Cells E–G. Many people forget this branch exists — that is exactly why it is its own cell.
- Evaluate. . Why this size? The model gave the true (negative) class probability — fairly confident and correct, so a modest loss.
Verify: . Symmetry check: a prediction of must give the same loss as a prediction of , because both assign to the true class. Indeed either way. ✓
Cell I — multi-class, starting from raw logits (softmax + CE)
- Exponentiate every logit. . Why this step? is always positive and turns "bigger logit" into "much bigger weight" — this is the softmax numerator. We cannot take of a raw logit (it can be negative, and is undefined for negatives), so we must convert to probabilities in first.
- Sum and normalise. Sum . Why this step? Dividing by the sum forces the outputs to add to — now they are a valid probability distribution, the legal input to .
- Pick the true-class term only. With one-hot , . Why only class 0? The one-hot zeros multiply away the other two terms — cross-entropy always reduces to .
- Evaluate. .
Verify: probabilities sum to ✓, and . Contrast: if the model had instead given the true class , loss would be — near zero, as a confident correct classifier should score. ✓
Cell J — real-world word problem (MSE with units)
- Errors . Why this step? Signs mix (over and under-forecast) — perfect illustration of why we must square before averaging.
- Square: . Sum . Why square? The °C blunder now dominates ( of the ), which is the point: MSE cares most about the worst day.
- Average: (°C²). Why divide by 4? days, keeps it per-day comparable.
- RMSE: °C. Why take the root? MSE lives in °C² — meaningless to a human. The square root drags it back into °C so we can say "typically off by about 1.94°C."
Verify: and . Forecast check: RMSE (°C) is below the worst single miss (°C) but above the smallest (°C) — RMSE always sits inside the range of absolute errors. ✓
Cell K — the exam trap: why MSE stalls on classification
First, one prerequisite we must not assume — the sigmoid:
We differentiate each loss with respect to the logit , using the chain rule with . The whole trick is what happens to that factor in each case — so we show explicitly.
BCE branch — watch the factor cancel. With , per-example , so Now multiply by : Why the cancellation happens: BCE's derivative carries a that exactly meets the inside ; they annihilate, leaving the clean with no saturating factor left. (The general case gives the same — the branch uses , and the in cancels it instead.)
MSE branch — the factor survives. With our no- MSE for one point, Multiply by : Why nothing cancels: MSE's derivative has no to meet the in — so the saturating factor stays and throttles the gradient.
Now plug in , :
- BCE gradient. . Why this size? Being badly wrong yields a large-magnitude push — exactly what learning needs.
- MSE gradient. First the saturating factor: . Then Why so tiny? The sigmoid is saturated near , so crushes the signal. Even the honest factor of can't rescue it — MSE tells a confidently-wrong model to barely change. Learning stalls.
- Compare. for BCE versus for MSE.
Verify: ratio — BCE sends a correction over twenty-five times stronger at this saturated point. That is precisely why we never use MSE for classification. ✓
Coverage check
Recall Did every cell get filled?
A,B,C ::: single-point MSE, both signs + zero (worked example A/B/C). D ::: averaged mixed batch (worked example D). E,F,G ::: BCE confident-correct, confident-wrong-limit, coin-flip (worked example E/F/G). H ::: BCE with branch (worked example H). I ::: multi-class softmax from logits (worked example I). J ::: real-world temperature word problem with units/RMSE (worked example J). K ::: exam trap — MSE gradient saturation vs BCE, with full cancellation shown (worked example K).
See also: Maximum Likelihood Estimation (where both losses come from) and Regularization (what we add on top once the loss is chosen).