1.3.19 · D4Probability & Statistics

Exercises — Cross-entropy concept

1,955 words9 min readBack to topic

This is a self-testing worksheet for the parent Cross-Entropy note. Every problem states its level (L1 Recognition → L5 Mastery). Try it first, then open the collapsible solution. Use natural log (, units = nats) throughout unless a problem says "bits" ().

Before we start, one reminder of every symbol we will use, in plain words:

Picture to hold in your head for the whole page — surprise as a curve:

Figure — Cross-entropy concept

Notice: as the predicted probability slides toward , the surprise shoots up to infinity. This single curve explains why confident-wrong predictions are punished so hard — that's the theme of the whole worksheet.


Level 1 — Recognition

Exercise 1.1

Which formula is the cross-entropy of relative to ? (a) (b) (c) (d)

Recall Solution

(b). Read it left to right: "weight by reality , take surprise of the guess ." Option (a) swaps the roles (that would be , a different number). Option (c) is (wrong sign, and no ). Option (d) is mean squared error, not cross-entropy.

Exercise 1.2

True or false: cross-entropy can be negative for ordinary probability distributions (all ).

Recall Solution

False. Each , so , so . A sum of non-negative surprises weighted by non-negative is . (For discrete distributions it can only reach when puts probability on the single outcome is certain of.)


Level 2 — Application

Exercise 2.1

A biased coin lands heads with true probability . Your model guesses (perfect). Compute in nats and confirm it equals .

Recall Solution

Two outcomes: heads , tails . Because , the "inside the log" and "outside" numbers are identical, so this is . The waste . This is the floor: no guess can beat a perfect one.

Exercise 2.2

Same true coin ( heads), but now the model guesses heads. Compute and the wasted bits .

Recall Solution

Waste: nats. Reading it: the model is over-confident about heads. When tails shows up (40% of the time) it pays the huge surprise, dragging the average way above the floor.

Exercise 2.3

Three-class problem, one-hot truth (the answer is class 2). Model predicts . Compute the loss.

Recall Solution

One-hot truth zeroes every term except the true class: Only the probability the model gave to the correct class matters. See Categorical Cross-Entropy.


Level 3 — Analysis

Exercise 3.1

Soft (smoothed) labels. Truth is not one-hot: (a label-smoothed cat, see Label Smoothing). Model predicts (matches exactly). Compute and .

Recall Solution

Since , . Key lesson: unlike the one-hot case, the loss floor here is not — even a perfect model pays nats because the labels themselves are uncertain. This is exactly why label smoothing changes the minimum achievable loss.

Exercise 3.2

Show numerically that cross-entropy is not symmetric: with and , compute both and and confirm they differ.

Recall Solution

Wait — sign care: , , sum nats. , so . The two questions "surprised when reality is but I assumed " versus its swap are genuinely different.

Exercise 3.3

Using , and the fact , prove that for a fixed truth the smallest possible cross-entropy is , attained only when .

Recall Solution

does not contain , so it is a constant as we vary the model. Then A constant plus a non-negative quantity is minimized by driving the non-negative part to its minimum, . And iff (Gibbs' inequality). Therefore at . This is why minimizing cross-entropy pulls the model toward reality (equivalently, toward Maximum Likelihood Estimation).


Level 4 — Synthesis

Exercise 4.1

A logistic-regression model (see Logistic Regression) outputs for the positive class, and the true label is . The per-sample loss is . Derive and show it equals , where and .

Recall Solution

Chain rule, in three visible steps. What: , so . What: . Combine: Why it's beautiful: the messy from the log exactly cancels the from the sigmoid derivative, leaving the clean prediction minus target. That cancellation is the real reason cross-entropy pairs so naturally with sigmoid/softmax — no vanishing gradient when the model is confidently wrong.

Exercise 4.2

Contrast the gradient signal. Suppose true but (confidently wrong). Compute the cross-entropy gradient magnitude and compare it to the MSE gradient magnitude for loss with the same sigmoid, which is .

Recall Solution

Cross-entropy: . A strong push. MSE: . The MSE gradient is roughly , about weaker, because the factor collapses toward exactly when the model saturates. Conclusion: MSE stalls on confident mistakes; cross-entropy keeps pushing. This is the quantitative version of Example 3 in the parent note.


Level 5 — Mastery

Exercise 5.1

Decompose a full loss. Truth (soft label), model (maximally unsure). Compute , , and , and verify the identity .

Recall Solution

(Both classes get , so the loss is just regardless of .) Check the identity: Interpretation: the model wastes nats by refusing to commit toward class 2, on top of the nats of irreducible label uncertainty.

Exercise 5.2

Optimal constant predictor. Over a large dataset the empirical class frequencies are . If a model is forced to output the same for every sample, which minimizes the average cross-entropy, and what is the minimum value?

Recall Solution

By Exercise 3.3, average cross-entropy is minimized when . So . Minimum loss : Why this is the mastery insight: cross-entropy trained on data drives the model's output distribution toward the empirical class frequencies — that's the maximum-likelihood answer. Any adds a positive .

Exercise 5.3

Bits vs nats. Convert the answer of Exercise 5.2 into bits.

Recall Solution

Same information content, different ruler. Nats use base ; bits use base ; divide nats by to convert.


[!recall]- One-line self-check

Cross-entropy of a perfect model on soft labels equals
the Shannon entropy of the labels, not zero.
The gradient of through a sigmoid w.r.t. the logit is
.
To convert nats to bits you
divide by .