Cross-entropy for more than two classes
probability▲▲△Extend I.1.B04 to classes. Show that categorical cross-entropy , with one-hot, is the negative log-likelihood of a categorical distribution. Then evaluate it for for each possible true class, and compare the average of those three losses with the entropy of .
Hint
A one-hot turns a sum into a single selected term. For the last part, weight each loss by how often that class actually occurs under itself.
Solution
Step 1 — the likelihood. Generalising the indicator-exponent trick of I.1.B04 from two factors to :
Every factor with is and drops out, leaving .
Step 2 — take logarithms and negate.
Identical, for the same reason as the binary case: a product of powers becomes a weighted sum of logarithms. Equation (I.1.6) is this result at with , so nothing new has been assumed — only an index has been allowed to run further.
Step 3 — evaluate. With :
| True class | Loss |
|---|---|
| 1 | |
| 2 | |
| 3 |
For the true class being the second, as asked: nats.
Step 4 — the comparison. The plain average of the three is nats. The entropy of is
These are different numbers, and the difference is instructive. The entropy is the probability-weighted average — the expected loss if the labels really were drawn from . The plain average treats all three classes as equally likely, which is a different distribution, and its excess over the entropy is the KL divergence from uniform to (Kullback–Leibler divergence 0.IT.03):
The lesson. A model’s expected cross-entropy equals its entropy only when its predictions match the true distribution. Any excess is exactly the divergence between what the model believes and what is true, which is why cross-entropy is bounded below by the data’s own entropy and can never be trained to zero on genuinely noisy labels.