From surprise to entropy, cross-entropy and the Kullback–Leibler divergence, why cross-entropy is the training loss of likelihood-based models, and the bounded, symmetric Jensen–Shannon divergence.
A model believes a coin is fair. We flip it ten times and get ten heads. The model is surprised; someone who knew the coin was rigged would not be. How wrong is the model, as a single number we can minimize?
The course’s answer is the Kullback–Leibler divergence, and the trainable Gaussian of the previous note will be fitted with it:
DKL(P∥Q)=x∑P(x)logQ(x)P(x),
where P is the real distribution that produces the outcomes and Q the distribution the model believes in. Each piece of the formula has a meaning of its own, and the first piece is surprise.
The route below follows Josh Starmer’s StatQuest (Starmer, 2021); the formal treatment is chapter 2 of Cover and Thomas (2006). Take two boards with ten marks each. Board A has 5 noughts and 5 crosses; board B has 9 noughts and 1 cross. Draw one mark at random. On which board is an X more surprising?
Figure
Where is drawing an X more surprising?
Both boards hold ten marks. On board B a cross is rare (1 in 10), so drawing it is more surprising, while the board as a whole is more predictable: its entropy, the average surprise defined in the next section, is lower than that of the balanced board A.
On board A a cross has probability 21; on board B, 101. The cross is five times rarer on B, and seeing it there should surprise us more: surprise must grow as the probability falls. A first attempt is 1/P(x): it gives 2 on board A and 10 on board B, which ranks them correctly.
It fails on a certain outcome. A rigged coin that always lands heads has P(H)=1, and heads should then carry no surprise at all, yet 1/P(H)=1. Taking the logarithm moves that point to zero. The surprise, or self-information, of an outcome is
s(x)=logP(x)1=−logP(x).
It is 0 when P(x)=1 and grows without bound as P(x)→0. An outcome of probability zero never happens, so its infinite surprise is never needed.
Figure
Surprise must vanish for a certain outcome
The first attempt, 1/P(x), ranks rare outcomes correctly but still equals 1 when P(x) = 1. Taking the logarithm moves that point to zero. Surprise −log₂ P(x) is in bits; 1/P(x) has no unit. The two marked points are the probabilities 0.1 and 0.9 used in the biased-coin example below.
The base of the logarithm only sets the unit. Base 2 gives bits, in which one fair binary choice carries exactly 1 bit; base e gives nats, and 1 bit=ln2≈0.693 nats. The coin and board examples below use bits, the cross-entropy examples nats.
For a biased coin with P(H)=0.9 and P(T)=0.1, heads carries log20.91≈0.152 bits and tails log20.11≈3.322 bits. The rare outcome carries the most surprise.
For independent flips the probability of the sequence H, H, T is a product. Its logarithm splits into a sum, so the total surprise is the sum of the per-flip surprises: almost all of it comes from the single rare tails.
No other continuous function turns products into sums. If s(pq)=s(p)+s(q) for all p,q∈(0,1], then g(t)=s(e−t) satisfies g(t+u)=g(t)+g(u) for all t,u≥0. Adding t to itself n times gives g(nt)=ng(t); putting t=1/n gives g(1/n)=g(1)/n, and so g(m/n)=nmg(1) for every positive rational. A continuous g is fixed by its values on the rationals, so g(t)=ct with c=g(1). Hence s(p)=clogp1, with c>0 so that rare outcomes are surprising; choosing c is choosing the base. Shannon proved the matching uniqueness result for entropy itself (Shannon, 1948, Theorem 2).
Over 100 flips of the biased coin we expect about 90 heads and 10 tails, for a total surprise of about 90⋅0.152+10⋅3.322≈46.9 bits. Per flip that is
0.9⋅0.152+0.1⋅3.322≈0.469 bits.
Each probability multiplies its own surprise: this is the expected value of the surprise s(X) of a random outcome X∼P. The expected surprise is the entropy (Shannon, 1948):
H(P)=x∑P(x)logP(x)1=−x∑P(x)logP(x).
Outcomes with P(x)=0 are left out of the sum, which is the convention 0log01=0, the limit of plogp1 as p→0: with p=e−t the product is te−t, which tends to 0 as t→∞ (Cover and Thomas, 2006, §2.1). The coin that always lands heads has entropy 0.
Board B has the biased coin’s distribution and so its entropy. The balanced board A is less predictable, and higher entropy means more uncertainty. For two outcomes no distribution exceeds 1 bit, and only the uniform one reaches it; the next section proves this.
To compute an entropy we must know the real distribution P. When we fit a generative model to data, we never do.
Back to the opening coin. The model believes it is fair, Q=(0.5,0.5), while in reality it is rigged, P=(0.99,0.01). Ten heads in a row have very different probabilities under the two:
Probability of 10 heads
Surprise (nats)
Model Q
0.510≈0.001
6.93
Reality P
0.9910≈0.90
0.10
Figure
Ten heads: surprising under the model, ordinary in reality
We believe the coin is fair, but it is rigged. Ten heads in a row have probability about 0.001 under the belief (6.93 nats of surprise) and about 0.90 in reality (0.10 nats). The difference is surprise produced by the wrong model, not by the data. Natural logarithms.
The outcome was ordinary; the surprise comes from the wrong belief. The cross-entropyH(P,Q) is the average surprise when outcomes come from P while we measure surprise with Q:
H(P,Q)=x∑P(x)logQ(x)1.
The weights say how often x happens, and the logarithms say how surprised the believer is to see it.
Figure
Reading the cross-entropy formula
Two distributions over the same discrete outcomes x: outcomes come from the real P, surprise is measured with the believed Q. The marked x₀ is common under P (P(x₀) = 0.12) yet nearly impossible under Q (Q(x₀) ≈ 9.0·10⁻⁵), so each occurrence costs ln 1/Q(x₀) ≈ 9.3 nats. The shapes are illustrative.
If Q=P, cross-entropy is entropy: H(P,P)=H(P). Otherwise it is larger. Gibbs’ inequality states
H(P,Q)≥H(P),
with equality only when Q=P (Cover and Thomas, 2006, Theorem 2.6.3). Believing in a wrong distribution can only raise the average surprise. The proof takes one line once the difference has a name, in the next section.
Gibbs’ inequality bounds entropy from above. Let Q be uniform over ∣X∣ outcomes. Then logQ(x)1=log∣X∣ for every x, so H(P,Q)=log∣X∣ and therefore H(P)≤log∣X∣, with equality only for the uniform P (Cover and Thomas, 2006, Theorem 2.6.4). The Mathematics course reaches the same result with Lagrange multipliers.
Cross-entropy is not symmetric, and the first argument is always the distribution that generates the outcomes. For one flip, in nats:
Cross-entropy mixes two sources of surprise: what the real distribution produces anyway, and the extra caused by the wrong model. Subtracting the entropy isolates the second:
The KL divergence (Kullback and Leibler, 1951) is the extra surprise from using Q as the model instead of P. The sum on the right is the general definition, and the subtraction holds when both terms are finite. A term with P(x)=0 contributes 0. A term with P(x)>0 and Q(x)=0 is +∞, and so is the divergence. KL is finite only when Q gives positive probability to every outcome to which P does, that is, when P is absolutely continuous with respect to Q (Cover and Thomas, 2006, §2.3). The next note shows what this condition does in training.
A second reading comes from comparing hypotheses. Bayes’ rule for two hypotheses P and Q, divided one by the other, cancels the evidence Pr(x); taking logarithms turns the product into a sum:
One observation x adds logq(x)p(x) to the log-odds of P against Q: the weight of evidence it carries for P. DKL(P∥Q) is the expected weight of evidence per observation when P is true. Because it is never negative, data push belief towards the true hypothesis on average (Murphy, 2023, §5.1.3).
So KL is non-negative and zero only when Q=P. It is neither symmetric nor does it satisfy the triangle inequality (Cover and Thomas, 2006, §2.3), so it is called a divergence and not a distance. For the two coins, in nats with four decimals so that the parts add up,
Each bar is the average surprise per flip under a wrong belief. The red part is the real coin's own surprise, its entropy; the amber part is the extra caused by the wrong model, the KL divergence. Swapping belief and reality changes both parts. Nats, to four decimals so that the parts add up.
The two boards give every mark positive probability, so their divergence is finite in both directions, and its value depends on the direction:
As a check, H(B,A)=HB+DKL(B∥A)=0.469+0.531=1 bit: measuring surprise with the uniform board costs exactly one bit per draw, whatever the true board.
Figure
The two boards compared with KL
The two boards as distributions over {O, X}, noughts in blue and crosses in red. Entropies are 1 bit and 0.469 bits. KL is 0.737 bits one way and 0.531 bits the other, both finite because every outcome has positive probability. Each arrow runs from the distribution treated as the truth to the model.
The same distributions break the triangle inequality. Read O as heads and X as tails and compare board A with the rigged coin P=(0.99,0.01). Directly, DKL(A∥P)≈2.329 bits. Through board B, DKL(A∥B)+DKL(B∥P)≈0.737+0.208=0.945 bits, which is less.
For the true data distribution P and a model Qθ, the decomposition reads
DKL(P∥Qθ)=H(P,Qθ)−H(P).
The entropy of the data does not depend on θ, so minimizing KL and minimizing cross-entropy have the same solution. We never need the value of H(P), which for images would be a hard estimation problem of its own. Cross-entropy is the part we can estimate from samples x1,…,xN∼P, by the average negative log-likelihood:
Minimizing it is maximum-likelihood estimation. For discrete outcomes the average is exactly the cross-entropy H(P^,Qθ) between the empirical distribution P^, which puts 1/N on each sample, and the model: P^(x) is the share of samples equal to x, so ∑xP^(x)(−logQθ(x)) adds −logQθ(xi)/N once for each sample. Hence maximum likelihood minimizes DKL(P^∥Qθ) (Goodfellow, Bengio and Courville, 2016, §5.5). As N grows the average converges to H(P,Qθ) by the law of large numbers. Minimizing forward KL, minimizing cross-entropy and maximizing likelihood are one objective under three names: exactly for the empirical distribution, and in the limit for P itself (Isachenko, lecture 1). The standing of maximum likelihood rests on asymptotic results, and they need conditions. The model family must contain P and be identifiable, its support must not depend on θ, the log-likelihood must be smooth and the true parameter must lie inside the parameter set. Then, with the model fixed and N growing, the estimate converges to the true θ and, for a single parameter, is approximately normal with variance 1/(NI(θ)), where I is the Fisher information. That is the Cramér–Rao bound, below which no unbiased estimator goes, so the estimate is asymptotically efficient. The Probability course states these results in Fisher information. If no member of the family equals P, the estimate converges instead to the member closest to P in forward KL (when maximum likelihood goes wrong). With few samples per parameter none of this is promised, and that is where priors enter (BMML lecture notes, 2026, lecture 1).
The same argument explains the cross-entropy loss of classifiers and language models, applied to the conditional distribution of the label or next token given the input. Each training example supplies one observed label, a one-hot vector, and the average negative log-likelihood estimates ExH(P(⋅∣x),Q(⋅∣x)). Soft targets, such as label smoothing or a teacher model’s probabilities, replace the one-hot vector by another estimate of P(⋅∣x).
The direction matters. Forward KL averages over the data, so it heavily penalizes a model that gives low probability to points the data produces, and a model that gives zero probability to one of them pays an infinite loss (Stanford CS236 notes). Generative models differ mostly in how they deal with logQθ(x) being hard to compute: VAEs bound it, normalizing flows compute it exactly, GANs avoid it, and diffusion models bound it again.
The Jensen–Shannon divergence (Lin, 1991) builds a symmetric and bounded quantity from the same pieces. It compares both distributions with their mixture M=21(P+Q):
DJS(P∥Q)=21DKL(P∥M)+21DKL(Q∥M).
The mixture is positive wherever P or Q is, so both terms are always finite, and both are non-negative, with DJS=0 only when P=Q. For the upper bound, P(x)≤P(x)+Q(x)=2M(x), so every log ratio in DKL(P∥M) is at most log2, and equality needs Q(x)=0 wherever P(x)>0. The same holds for the second term. Hence
0≤DJS(P∥Q)≤ln2 nats=1 bit,
with the maximum exactly when P and Q do not overlap. For the two coins, DJS≈0.193 nats, or 0.279 bits.
Its square root DJS is a true metric on probability distributions: symmetric, zero only for P=Q, and with the triangle inequality (Endres and Schindelin, 2003; HSE, lecture 1). The proof is beyond this course; the local picture shows why a square root is needed. For Q=P+δ with a small δ, a second-order expansion of the logarithm gives DJS≈∑xδ(x)2/(8P(x)), a squared length, like the square of a Euclidean distance. A squared distance breaks the triangle inequality (for points 0, 1 and 2 on a line, 4>1+1), and its root restores it.
JS matters twice later. In the next note it is one of the losses compared on a two-mode target. In the GAN note, the original GAN objective with an optimal discriminator equals 2DJS(P∥Q)−2ln2, with P the data and Q the generator (Goodfellow et al., 2014). Its saturation at ln2 for non-overlapping distributions then explains why GAN training can stall (where it breaks; Arjovsky and Bottou, 2017).
KL and JS are two members of a larger family. Every f-divergence∫qf(p/q)dx with a convex f and f(1)=0 compares densities through their ratio (HSE, lecture 1). Substituting shows the special cases: f(u)=ulnu gives ∫plnqpdx=DKL(P∥Q), f(u)=−lnu gives the reverse KL, and f(u)=21∣u−1∣ gives the total variation distance 21∫∣p−q∣dx. The f-GAN lists the f of JS and of other divergences and trains with any of them. The Wasserstein GAN leaves the family and measures how far mass has to move, which stays informative when the supports do not overlap.
We now have a number for how far a model Q is from the data P, and we know that minimizing DKL(P∥Qθ) is maximum likelihood. The next note trains the Gaussian of the previous note with a sample estimate of KL, asks what the formula assumes, and compares what forward KL, reverse KL and JS do when one Gaussian must fit two modes.
C. E. Shannon. A Mathematical Theory of Communication. Bell System Technical Journal, 27(3), 379–423 and 27(4), 623–656, 1948. Theorem 2: uniqueness of the entropy formula.
D. M. Endres, J. E. Schindelin. A new metric for probability distributions. IEEE Transactions on Information Theory, 49(7), 1858–1860, 2003. The square root of JS is a metric.
T. M. Cover, J. A. Thomas. Elements of Information Theory, 2nd ed. Wiley, 2006. §2.1 (entropy, the convention 0log0=0), §2.3 (relative entropy), Theorems 2.6.3 (information inequality) and 2.6.4 (maximum entropy); §8.1 and §8.5–8.6 (differential entropy and relative entropy for densities).
I. Goodfellow, Y. Bengio, A. Courville. Deep Learning. MIT Press, 2016. §3.13 (information theory) and §5.5 (maximum likelihood and cross-entropy).
BMML team. Байесовские методы в анализе данных (Bayesian methods in data analysis), lecture notes, 2026. GitHub repository. Lecture 1: maximum likelihood, its asymptotic properties and the Bayesian alternative.
S. Ermon, A. Grover et al. Stanford CS236 course notes: “Autoregressive models”, section on learning: KL, its asymmetry and maximum likelihood.
R. Isachenko. Deep Generative Models, MIPT/YSDA, 2025. Lecture 1: divergences, forward and reverse KL, maximum likelihood as a Monte Carlo estimate of forward KL.