Speed

What Cross-Entropy Measures

Compute expected surprise under a true class distribution and decompose cross-entropy into target entropy plus KL divergence.

The problem

Suppose a coastal weather station observes three daily sky categories: clear (0.60), rain (0.30), and storm (0.10). That frequency vector q = [0.60, 0.30, 0.10] is the true data-generating distribution. A candidate forecasting model proposes its own probability vector p = [0.30, 0.50, 0.20] for the same three categories.

We need a single exact number that measures how surprised an observer will be, on average, if nature draws outcomes according to qq while the observer prices each outcome using the model's log-penalties −log⁡(pk)-\log(p_k). That expected surprise is the cross-entropy:

H(q,p)=−∑k=0K−1qklog⁡(pk)=H(q)+DKL(q∥p)H(q, p) = -\sum_{k=0}^{K-1} q_k \log(p_k) = H(q) + D_{\mathrm{KL}}(q \parallel p)

Notice that the roles of qq and pp are asymmetric: pkp_k sits inside the logarithm to set the surprise −log⁡(pk)-\log(p_k), whereas qkq_k sits outside as the frequency weight. The companion book Cross-Entropy, Honestly works row by row across a batch first, taking each row's true-class probability as given; this book works term by term inside a single row's class distribution second.

Work through the numbers

Start by computing the model's surprise −log⁡(pk)-\log(p_k) and weighting each class by its true frequency qkq_k. For q = [0.60, 0.30, 0.10] and p = [0.30, 0.50, 0.20], class 0 has −0.60log⁡(0.30)=0.72238-0.60 \log(0.30) = 0.72238, class 1 has −0.30log⁡(0.50)=0.20794-0.30 \log(0.50) = 0.20794, and class 2 has −0.10log⁡(0.20)=0.16094-0.10 \log(0.20) = 0.16094. Adding those three weighted terms gives H(q,p)=1.09127H(q, p) = 1.09127 nats.

Clear days do most of the damage in that sum. They happen 60 percent of the time, yet the model gives them p0=0.30p_0 = 0.30, contributing 0.72238 nats to the total.

Even if the model matched the true frequencies qq exactly, cross-entropy would not drop to zero. Evaluating H(q,q)=−∑kqklog⁡(qk)H(q, q) = -\sum_k q_k \log(q_k) yields the target distribution's own entropy H(q)=0.30650+0.36119+0.23026=0.89795H(q) = 0.30650 + 0.36119 + 0.23026 = 0.89795 nats. The gap between the two sums is the Kullback-Leibler divergence DKL(q∥p)=H(q,p)−H(q)≈1.09127−0.89795≈0.19333D_{\mathrm{KL}}(q \parallel p) = H(q, p) - H(q) \approx 1.09127 - 0.89795 \approx 0.19333 nats.

The live model

The blue bars on the left show the three cross-entropy terms −qklog⁡(pk)-q_k \log(p_k), and the green bars on the right show the fixed target entropy terms −qklog⁡(qk)-q_k \log(q_k). The mix slider blends the baseline forecast p0 toward q via p = p0 * (1.0 - mix) + q * mix.

At mix equal to 0.0, H(q,p)H(q, p) is 1.091, which equals the irreducible floor H(q)=0.898H(q) = 0.898 plus the mismatch penalty KL = 0.193. Slide mix to 0.50 so p becomes [0.45, 0.40, 0.15]: H(q,p)H(q, p) drops to 0.944 and the KL gap shrinks to 0.046. At mix equal to 1.0, p matches q, the KL gap vanishes to 0.000, and H(q,p)H(q, p) settles at 0.898.

Three classes with true weights 0.6, 0.3, 0.1 and candidate probabilities 0.3, 0.5, 0.2 give cross-entropy 1.091, entropy 0.898, and KL gap 0.193.
let mix = scene.parameter("mix", 0.0, 0.0, 1.0, 0.25);
let q = input.select(0, 0);
let p0 = input.select(0, 1);
let p = p0 * (1.0 - mix) + q * mix;
let terms_ce = (q * p.log()).neg();
let terms_hq = (q * q.log()).neg();
let ce = terms_ce.sum().reshape(1);
let hq = terms_hq.sum().reshape(1);
let kl = ce - hq;
let answer = torch.cat(torch.list().add(ce).add(hq).add(kl), 0);
out.set("p", p).set("ce", ce).set("hq", hq).set("kl", kl);
{
  "p": [
    0.3,
    0.5,
    0.2
  ],
  "ce": [
    1.0912716280069552
  ],
  "hq": [
    0.8979457248567797
  ],
  "kl": [
    0.1933259031501755
  ]
}
Watch the parameter change

Your turn

At mix = 0.0 the starter reports ce = 1.02965 and kl = 0.13171 because it weights the log-penalties by the model's own probabilities [0.30, 0.50, 0.20]; by hand, weighting by the true frequencies [0.60, 0.30, 0.10] gives H(q,p)=1.09127H(q, p) = 1.09127 and DKL(q∥p)=0.19333D_{\mathrm{KL}}(q \parallel p) = 0.19333. Fix terms_ce so each class's log-penalty is weighted by the true distribution, keeping the three-element answer vector [ce, hq, kl].

Three classes with true weights 0.6, 0.3, 0.1; the starter weights log(p) by p instead of q.
let mix = scene.parameter("mix", 0.0, 0.0, 1.0, 0.25);
let q = input.select(0, 0);
let p0 = input.select(0, 1);
let p = p0 * (1.0 - mix) + q * mix;
let terms_ce = (p * p.log()).neg();
let terms_hq = (q * q.log()).neg();
let ce = terms_ce.sum().reshape(1);
let hq = terms_hq.sum().reshape(1);
let kl = ce - hq;
let answer = torch.cat(torch.list().add(ce).add(hq).add(kl), 0);
out.set("p", p).set("ce", ce).set("hq", hq).set("kl", kl);
{
  "p": [
    0.3,
    0.5,
    0.2
  ],
  "ce": [
    1.0296530140645737
  ],
  "hq": [
    0.8979457248567797
  ],
  "kl": [
    0.13170728920779395
  ]
}

Expected surprise H(q, p) must weight the model log-probabilities by the true distribution rather than by the model's own probabilities. Check tests unseen probability vectors and mix values.

With the true frequency weights restored, answer matches [1.09127189, 0.89794572, 0.19332617] at mix = 0.0. The check also reruns the decomposition on q = [0.50, 0.30, 0.20] with p0 = [0.20, 0.50, 0.30] at mix = 0.25 and on q = [0.70, 0.20, 0.10] with p0 = [0.40, 0.40, 0.20] at mix = 0.75.

Read the result carefully

Why do we minimize H(q,p)H(q, p) during training rather than subtracting H(q)H(q) first to compute DKL(q∥p)D_{\mathrm{KL}}(q \parallel p)? Because the target distribution qq is fixed by the dataset and does not depend on the model's parameters. Whether p is poor (mix = 0.0) or ideal (mix = 1.0), H(q)H(q) stays locked at 0.89795.

Every reduction in H(q,p)H(q, p) as mix moves from 0.0 (1.09127) to 0.50 (0.94375) to 1.0 (0.89795) comes dollar-for-dollar out of the KL divergence term (0.19333 down to 0.04580 down to 0.00000). Minimizing cross-entropy with respect to pp is identical to minimizing the KL divergence from qq to pp, without needing to know the true entropy H(q)H(q).

Recap

  • Cross-entropy H(q,p)=−∑kqklog⁡(pk)H(q, p) = -\sum_k q_k \log(p_k) averages the model's log-penalties −log⁡(pk)-\log(p_k) using the true frequencies qkq_k.
  • The roles of qq and pp cannot be swapped: pp sets the surprise inside log⁡(pk)\log(p_k), whereas qq sets the frequency weights outside the logarithm.
  • H(q,p)=H(q)+DKL(q∥p)H(q, p) = H(q) + D_{\mathrm{KL}}(q \parallel p) splits into irreducible target entropy (0.89795) plus the avoidable distribution mismatch (0.19333 at mix = 0.0).