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 while the observer prices each outcome using the model's log-penalties . That expected surprise is the cross-entropy:
Notice that the roles of and are asymmetric: sits inside the logarithm to set the surprise , whereas 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 and weighting each class by its true frequency . For q = [0.60, 0.30, 0.10] and p = [0.30, 0.50, 0.20], class 0 has , class 1 has , and class 2 has . Adding those three weighted terms gives nats.
Clear days do most of the damage in that sum. They happen 60 percent of the time, yet the model gives them , contributing 0.72238 nats to the total.
Even if the model matched the true frequencies exactly, cross-entropy would not drop to zero. Evaluating yields the target distribution's own entropy nats. The gap between the two sums is the Kullback-Leibler divergence nats.
The live model
The blue bars on the left show the three cross-entropy terms , and the green bars on the right show the fixed target entropy terms . The mix slider blends the baseline forecast p0 toward q via p = p0 * (1.0 - mix) + q * mix.
At mix equal to 0.0, is 1.091, which equals the irreducible floor plus the mismatch penalty KL = 0.193. Slide mix to 0.50 so p becomes [0.45, 0.40, 0.15]: 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 settles at 0.898.

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 and . Fix terms_ce so each class's log-penalty is weighted by the true distribution, keeping the three-element answer vector [ce, hq, kl].

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 during training rather than subtracting first to compute ? Because the target distribution 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), stays locked at 0.89795.
Every reduction in 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 is identical to minimizing the KL divergence from to , without needing to know the true entropy .
Recap
- Cross-entropy averages the model's log-penalties using the true frequencies .
- The roles of and cannot be swapped: sets the surprise inside , whereas sets the frequency weights outside the logarithm.
- splits into irreducible target entropy (
0.89795) plus the avoidable distribution mismatch (0.19333atmix = 0.0).