Md. Asif Uddin
I.1.X04

Cross-entropy for more than two classes

probability▲▲△

Extend I.1.B04 to KK classes. Show that categorical cross-entropy kyklogpk-\sum_k y_k \log p_k, with y\vec{y} one-hot, is the negative log-likelihood of a categorical distribution. Then evaluate it for p=(0.7,0.2,0.1)\vec{p} = (0.7, 0.2, 0.1) for each possible true class, and compare the average of those three losses with the entropy of p\vec{p}.

Hint

A one-hot y\vec{y} turns a sum into a single selected term. For the last part, weight each loss by how often that class actually occurs under p\vec{p} itself.

Solution

Step 1 — the likelihood. Generalising the indicator-exponent trick of I.1.B04 from two factors to KK:

P(Y=c)=k=1Kpkyk,yk=1[k=c]P(Y = c) = \prod_{k=1}^{K} p_k^{\,y_k}, \qquad y_k = \mathbb{1}[k = c]

Every factor with yk=0y_k = 0 is pk0=1p_k^0 = 1 and drops out, leaving pcp_c.

Step 2 — take logarithms and negate.

logP(Y=c)=k=1Kyklogpk=CE(p,y)-\log P(Y = c) = -\sum_{k=1}^{K} y_k \log p_k = \mathrm{CE}(\vec{p}, \vec{y})

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 K=2K = 2 with p2=1p1p_2 = 1 - p_1, so nothing new has been assumed — only an index has been allowed to run further.

Step 3 — evaluate. With p=(0.7,0.2,0.1)\vec{p} = (0.7, 0.2, 0.1):

True classLoss
1log0.7=0.3567-\log 0.7 = 0.3567
2log0.2=1.6094-\log 0.2 = 1.6094
3log0.1=2.3026-\log 0.1 = 2.3026

For the true class being the second, as asked: 1.6094\mathbf{1.6094} nats.

Step 4 — the comparison. The plain average of the three is (0.3567+1.6094+2.3026)/3=1.4229(0.3567 + 1.6094 + 2.3026)/3 = 1.4229 nats. The entropy of p\vec{p} is

H(p)=kpklogpk=0.7(0.3567)+0.2(1.6094)+0.1(2.3026)=0.8018\mathcal{H}(\vec{p}) = -\sum_k p_k \log p_k = 0.7(0.3567) + 0.2(1.6094) + 0.1(2.3026) = 0.8018

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 p\vec{p}. 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 p\vec{p} (Kullback–Leibler divergence 0.IT.03):

1.42290.8018=0.6211=KL ⁣(uniformp)1.4229 - 0.8018 = 0.6211 = \mathrm{KL}\!\left(\text{uniform} \,\|\, \vec{p}\right)

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.

Draws on