3.1.8 · D2Neural Network Fundamentals

Visual walkthrough — Loss functions - MSE, cross-entropy

2,013 words9 min readBack to topic

Step 1 — What is a prediction, really?

WHAT. A classifier does not shout "cat!". It hands you a list of confidences — one number per possible answer. If the choices are {cat, dog, fox}, it might say .

WHY these three numbers and not one. A single "cat-ness" number can't say how the remaining belief is split between dog and fox. We need a full probability distribution: a set of numbers that (a) are each between and , and (b) sum to exactly (all your belief has to go somewhere).

PICTURE. Think of a jar holding exactly one litre of water. You pour it into three cups labelled cat / dog / fox. The height in each cup is that class's probability. No water is created or destroyed — that's the "sums to 1" rule.

Figure — Loss functions -  MSE, cross-entropy

Step 2 — What is the truth we compare against?

WHAT. The true label is also written as a list, but a brutally simple one: a in the correct cup, everywhere else. If the real animal is a cat, the truth is .

WHY write truth as a list too. So truth and prediction live in the same shape — three numbers vs three numbers — and we can compare them term by term. A list that is all except a single is called one-hot.

PICTURE. The truth is a jar poured entirely into one cup: full cat cup, empty dog and fox cups.

Figure — Loss functions -  MSE, cross-entropy

Step 3 — Turning "how good?" into "how surprised?"

WHAT. We need one number saying how wrong the prediction was. The trick: measure surprise. If the true answer was cat and you gave cat a probability near , you're barely surprised. If you gave cat a probability near , you're stunned.

WHY the function and not, say, . We want surprise to have three properties:

  1. Certain truth () → zero surprise.
  2. Impossible truth () → surprise blows up to (a confident-and-wrong model must be punished savagely).
  3. Independent evidence should add: seeing two unrelated correct facts should sum their surprises, not multiply. The only function turning the multiplication of probabilities into an addition of scores is the logarithm.

The plain function fails property 2 — it caps out at , so being catastrophically wrong costs the same as being mildly wrong. Only punishes near-zero probability without mercy.

PICTURE. Plot as the true-class probability slides from to . Watch it start at on the right and rocket to the sky on the left.

Figure — Loss functions -  MSE, cross-entropy

Step 4 — Why the sum collapses to a single term

WHAT. Cross-entropy for one example is It looks like all classes contribute. They don't.

WHY it collapses. Every is either or . A term with is — it vanishes. Only the true class, where , survives: So the general formula and the "just the surprise of the true class" idea from Step 3 are the exact same thing. The sum is only there to write it in one line for any label.

PICTURE. Line up the two lists. Multiply cup by cup. Two products are zeroed out; one survives — the true-class surprise from Step 3.

Figure — Loss functions -  MSE, cross-entropy

Step 5 — Where do the come from? Softmax

WHAT. The network's last layer spits out raw scores called logits, written . They can be any real number — negative, huge, whatever. They are not probabilities (they don't sum to 1, can be negative). Softmax converts them:

WHY exponentiate then divide. Two jobs, one formula:

  • is always positive, no matter how negative is — that fixes rule (a) from Step 1.
  • Dividing by the total forces the outputs to sum to 1 — that fixes rule (b).

Exponentiation also makes the biggest logit dominate smoothly, which is exactly the "soft" version of picking the max.

PICTURE. Three logit bars go in; stretches them (big ones stretch far more); dividing by the total rescales them into three probability bars that fill one unit of height.

Figure — Loss functions -  MSE, cross-entropy

See Softmax and Sigmoid for the full treatment of this squashing step.


Step 6 — The payoff: the gradient becomes

WHAT. Gradient Descent needs the slope of CE with respect to each logit — that slope tells Backpropagation which way to nudge the score. The remarkable result is: Prediction minus truth. Nothing else.

WHY it's remarkable and why it happens. The in CE has a derivative — it explodes near . The softmax has a derivative carrying a -type factor that shrinks near . When you chain-rule them together, the exploding piece and the shrinking piece cancel exactly, leaving the tame, honest error .

Contrast this with sigmoid-plus-MSE (see the parent's mistake box): there is no to cancel the shrinking factor, so the gradient dies when the model is confidently wrong — learning stalls.

PICTURE. Two curves over the true-class probability: MSE's gradient magnitude (flattens to at both ends — the danger zone on the left) versus CE's gradient magnitude (stays strong and large exactly where the model is confidently wrong).

Figure — Loss functions -  MSE, cross-entropy

Step 7 — Every case, checked

WHAT. Let's make sure no scenario surprises us later. We reuse the parent's worked example: logits , true class .

Case Prediction Meaning
Confident & right almost no loss
The worked example moderate loss
Pure guessing (2-class) learned nothing
Confident & wrong savage punishment

WHY cover them all. The whole design of lives in these extremes: it is when you're perfect, finite in the middle, and unbounded when you're confidently wrong. There is no input that breaks it — except exactly, which is the degenerate case below.

Figure — Loss functions -  MSE, cross-entropy
Recall Reveal: verify the worked number

Logits , sum , so and . CE at the worked example ::: CE at pure guessing () :::


The one-picture summary

Everything on this page is one pipeline: logits → softmax → probabilities → compare with one-hot truth via → one loss number → gradient flows back.

Figure — Loss functions -  MSE, cross-entropy

softmax

minus log of true class

gradient y-hat minus y

logits z

probs y-hat

one-hot truth y

cross-entropy

loss number

Recall Feynman: the whole walkthrough in plain words

The model never says "cat" — it pours one litre of belief into three cups (Step 1). The truth is that same litre poured entirely into the right cup (Step 2). To score the model we ask "how surprised are you by the right answer?" and measure surprise with , because that function gives zero surprise for certainty, infinite surprise for confident-wrongness, and adds up when facts are independent (Step 3). The scoring formula sums over all cups, but truth is in only one cup, so all other terms die and we're left with the surprise of the true cup (Step 4). Those cup-heights themselves came from raw scores squashed by softmax — exponentiate to make them positive, divide to make them sum to one (Step 5). And when we ask which way to push the raw scores, the exploding log and the shrinking softmax cancel perfectly, leaving the simplest possible answer: prediction minus truth (Step 6). We checked every corner — perfect, average, coin-flip, and catastrophic — and found the only landmine is feeding a literal zero into the log, which softmax makes impossible (Step 7).


Connections