Cross-entropy
Cross-entropy is the target-weighted average of the forecast’s negative log probabilities.
On this page 8 sections
- Overview
- Cross-entropy evaluates a forecast using outcomes from a target distribution
- For observed category counts, divide by the total to obtain target frequencies
- If Q assigns probability 0 to any outcome with positive P mass, cross-entropy is infinite
- For observed labels, target frequencies are counts divided by the number of examples
- Key takeaway
- Sources & further reading
- Concept connections
01Cross-entropy evaluates a forecast using outcomes from a target distribution#
Cross-entropy evaluates a forecast using outcomes from a target distribution. The target tells you how often each category occurs; the forecast tells you how surprising that category would be. Separating those two roles is essential for understanding classification log loss.
Cross-entropy measures how much surprise a forecast assigns to outcomes drawn from a target distribution. The target P supplies the weights; forecast Q supplies the probabilities inside the log.
Use target P=(0.625,0.25,0.125) and forecast Q=(0.5,0.25,0.25). Forecast surprisals are 1,2,2 bits; the weighted contributions total 1.375 bits.
Suppose the target is and the forecast is . The forecast assigns 1 bit of surprise to either outcome. Weight by the target frequencies: bit. Replacing the target weights with forecast weights would evaluate a different expectation.
In the same category order, target P: ; forecast Q: . Find cross-entropy H(P,Q) in bits.
Show answer and explanation
P-weighted forecast surprisals: 1, 0.25, 0.5; total: bits.
02For observed category counts, divide by the total to obtain target frequencies#
For observed category counts, divide by the total to obtain target frequencies. Averaging forecast log loss over observations gives this empirical cross-entropy.
Weighting with Q instead would evaluate a different quantity. Match categories before multiplying their target weights and forecast surprisals.
Target category counts: . Forecast Q: . In the same category order, find mean log loss in bits.
Show answer and explanation
P-weighted forecast surprisals: 1, 0.5, 0.25; total: bits.
03If Q assigns probability 0 to any outcome with positive P mass, cross-entropy is infinite#
If Q assigns probability 0 to any outcome with positive P mass, cross-entropy is infinite. That outcome cannot simply be dropped.
An outcome with P mass 0 contributes 0, including when Q is also 0 there. Inspect the target mass before deciding what a zero forecast means.
P=Q=. Draft: zero Q makes loss infinite. Correct H(P,Q), in bits?
Show answer and explanation
Zero-target term: 0. Others: 0.5+0.5=1 bit.
04For observed labels, target frequencies are counts divided by the number of examples#
For observed labels, target frequencies are counts divided by the number of examples. Averaging the forecast’s log loss over those examples gives empirical cross-entropy. A forecast probability zero for an outcome that occurs makes its log loss infinite, rather than removing that outcome from the average.
Use target weights and forecast logs; positive target mass at a zero forecast makes the result infinite.
- Compute expected negative log probability under a supplied target distribution.
Sources & further reading
- [1]Cosma Shalizi: Information Theory I, cross-entropy and divergence ↗Cosma Shalizi · Article
- [2]Dive into Deep Learning §22.11 ↗Dive into Deep Learning §22.11 · Article