1.3.19 · D3Probability & Statistics

Worked examples — Cross-entropy concept

2,881 words13 min readBack to topic

This page is the "brute-force" companion to the parent note. We built the definition there. Here we hunt down every kind of input cross-entropy can face and work each one fully, so you never meet a case you have not seen.

Before anything, one reminder of the machine we are feeding numbers into, plus two supporting symbols we will lean on repeatedly.

Everything below is: pick , pick , weight surprise by reality, add up. The bar-chart figures show what (blue) and (orange) actually look like side by side.


The scenario matrix

Cross-entropy takes two probability vectors. The "scenarios" come from what those vectors can look like — hard vs soft truth, good vs bad guess, and the dangerous edges where a probability is or .

# Cell (case class) What makes it special Example
A Soft truth, soft guess, close Both distributions spread out, model near-correct Ex 1
B Soft truth, soft guess, mismatched Model confident in the wrong direction Ex 2
C Hard (one-hot) truth, decent guess True vector like , model reasonable Ex 3
D Hard truth, confidently WRONG guess The huge-penalty case Ex 4
E Degenerate: where , the blow-up Ex 5
F Degenerate: term — does it vanish? Ex 5
G Perfect match (lower bound) Cross-entropy hits its floor Ex 6
H Real-world word problem Translate a story into Ex 7
I Exam twist: label smoothing Truth is deliberately softened Ex 8
J Soft truth, HARD (one-hot) guess Model commits to one class; finite unless it commits to a class Ex 8b
K Limiting behaviour of a single term How grows as or Ex 9 (figure)

We now sweep every row.


Figure — Cross-entropy concept

Look at the bar chart: the blue truth bars and orange guess bars almost overlap — that visual near-coincidence is exactly why we expect the loss to sit barely above the floor.

Step 1 — Write out the surprises. , . Why this step? Surprise is per-outcome and uses the model's probability — that is the "code length" we committed to.

Step 2 — Weight by reality and sum. Why this step? Reality decides how often each surprise is actually incurred, so we weight by , not .

Verify. True entropy . Since always (the rule from the definition), and ✓. The gap nats is tiny — matches "close guess". Units: nats ✓.


Figure — Cross-entropy concept

In the bar chart the orange guess is now flipped relative to blue truth — tall where truth is short. That crossing is the visual signature of a mismatched guess and warns us the loss will be large.

Step 1 — Surprises. , . Why this step? Same machine; only changed.

Step 2 — Weight by the true . Why this step? Rain happens 70% of the time, and every rainy day the model is very surprised — that heavy weight on the big surprise is what inflates the loss.

Verify. from Ex 1 ✓. nats — a large mismatch cost, as expected. Still ✓.


Figure — Cross-entropy concept

Notice in the chart: the blue truth is a single full bar on "cat" (that is what one-hot looks like). Every orange bar sitting over a zero blue bar will be multiplied away.

Step 1 — Kill the zero-weight terms. Why this step? , so those surprises never actually happen — their contribution is .

Step 2 — Evaluate the one survivor.

Verify. For one-hot truth, (no uncertainty about a known cat), so — the whole loss is the mismatch. Sanity: a perfect would give ✓. This is exactly the negative-log-likelihood shortcut and links to MLE.


Step 1 — Only the true class matters. Why this step? One-hot truth again zeroes the dog/bird terms — cross-entropy only reads the probability the model gave to what actually happened.

Step 2 — Evaluate.

Verify. Compare Ex 3 () vs here (): dropping the true-class probability from to multiplied the loss ~8×. That steep penalty for confident wrongness is the whole reason we prefer cross-entropy over MSE. Note the model's confidence in "dog" () never entered — cross-entropy doesn't reward being sure about the wrong thing ✓.


Sub-case E — zero where positive. Step 1. . Why this step? The true class occurs with probability , but the model swore it was impossible. Step 2. . Infinite loss. Why this matters: a model that rules out reality is infinitely surprised. This is why real code adds a tiny floor (e.g. ) — or uses Label Smoothing so no probability is ever exactly .

Sub-case F — zero for a term. Here the dog term is . Since is a finite number (), the product is genuinely . Why this step? The danger only appears when is also there. As long as , the zero weight cleanly deletes the term. (By convention , its limit.)

Verify. E: ✓. F: exactly ✓. So the only dangerous zero is on the true class.


Step 1 — Plug in. Why this step? When the code lengths are already optimal for reality — zero wasted bits.

Step 2 — Evaluate. , .

Verify. This equals Shannon Entropy by construction, so ✓. And is a lower bound: no other can beat it (that is the rule again). This floor is why we can't drive classification loss to unless truth is one-hot ().


Step 1 — Translate the story to vectors. Truth ; guess . Here (spam / ham). Why this step? "30% are truly spam" is the frequency — that's . "Model always says 50-50" is .

Step 2 — Surprises are equal. for both classes. Why this step? A uniform guess assigns the same code length everywhere.

Step 3 — Weighted sum. Why this step? When is uniform over outcomes, every surprise is , and the -weights sum to , so regardless of (here ).

Verify. ✓. Compare . Wasted bits nats — the price of refusing to commit. A smarter model outputting would save exactly that.


Step 1 — Note every term now counts. Why this step? This is exactly Pitfall 1 from the parent: soft means we can no longer drop terms. See Label Smoothing.

Step 2 — Surprises. , (twice). Why this step? Each surprise is the "how many nats does this outcome cost me" reading off the model's — and because is small, its surprise () is large, ten-fold the surprise of the confident bar. The smaller the bar the taller the surprise; that inverse relationship is the whole geometry of .

Step 3 — Weighted sum. Why this step? The two small weights on the other classes now nudge the model to keep a little probability there — that is what label smoothing buys (calibration, less overconfidence).

Verify. One-hot loss on the same would be . Smoothed loss ✓ — the extra comes from the two side terms. Floor check: for , and ✓.


Step 1 — Near-one-hot guess, finite case. Surprises: , (twice). Why this step? A hard-ish guess makes the "wrong" surprises huge (), but the truth only weights them each — so they contribute but don't explode.

Step 2 — Weighted sum. Why this step? Because still puts on each side class, over-confidence is penalised — a soft truth fights one-hot guesses. This is precisely why label smoothing discourages arrogant outputs.

Step 3 — The degenerate hard-wrong guess. If , the cat term is , so . Why this step? Same rule as Cell E: a positive truth weight () times blows up. A one-hot guess is only safe when it lands exactly on classes the truth also cares about.

Verify. Finite case from Ex 8 (arrogance costs more than the mild guess) ✓, and ✓. Degenerate case: ✓.


Figure — Cross-entropy concept

How to read this figure. The horizontal axis is , the probability your model assigns to the class that actually happened — it runs from (model swore it was impossible) to (model was fully certain and correct). The vertical axis is the resulting cross-entropy loss in nats. Follow the blue curve left-to-right: it plunges from the top-left () down to at the right edge. The three coloured dots mark the checkpoints below.

Step 1 — Read the endpoints. : (perfectly confident and correct, no surprise). : (confident and impossibly wrong — the Ex 5 blow-up). Why this step? These are the extreme corners every classifier lives between.

Step 2 — Read the middle checkpoints. ; ; . Why this step? Notice the curve is convex and steep on the left — the slope is , so at the gradient magnitude is , versus at . That is the "strong gradient when confidently wrong" property from the parent's MSE comparison.

Verify. Slope : at it is , at it is ✓. Convexity () guarantees a single minimum at ✓. This is the geometric root of why cross-entropy trains fast — see Logistic Regression.


Recall Quick self-check

One-hot truth , model — what is the loss? ::: nats (only the true-class term survives). Which single zero makes cross-entropy infinite? ::: on a class where (model rules out reality). If , cross-entropy equals what? ::: The Shannon entropy — its minimum, with . Why does a uniform guess give regardless of ? ::: Every surprise equals , and the -weights sum to .

Related deep concepts: KL Divergence, Categorical Cross-Entropy, Mutual Information.