Kayavak / Information Theory / IT·11 Information in Machine Learning: Cross-Entropy and KL Divergence / Deeper
Deeper · Information Theory
Information in Machine Learning: Cross-Entropy and KL Divergence
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Statistics and information theory grew up separately and met in the KL divergence. Kullback and Leibler, statisticians working on codebreaking, wanted to measure how well data can discriminate between two hypotheses; Turing and Good had already weighed evidence in the same units at Bletchley Park; and Shannon's coding theorems implied that the extra cost of coding with the wrong distribution is exactly the same quantity. Once that link was made, the tools of each field became tools of the other: likelihood became code length, hypothesis testing became a question about divergence, and model selection (Akaike, Rissanen) became a question about compression.
Machine learning inherited the whole package. Training a probabilistic model is minimising cross-entropy, judging a language model is measuring perplexity, distilling a large model into a small one is minimising a divergence, keeping a fine-tuned model close to its original is penalising one, and approximating an intractable posterior is minimising one in the other direction. The vocabulary of this book is, to a large extent, the working vocabulary of modern machine learning.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
A model's surprise on real data is the data's own information plus the cost of being wrong about it. Training makes the cost small, held-out data shows whether the saving is real, and the same cost measures drift, evidence and approximation.
| Question | Answer |
|---|---|
| What is it? | Cross-entropy (a model's average surprise) and KL divergence (its excess over the entropy) |
| Problem it solves | Scoring probabilistic models, training them, comparing distributions, weighing evidence |
| Parts | ; Gibbs' inequality; log loss; perplexity; direction; Stein and Sanov |
| How it works | Average or over data from |
| Assumptions | Probabilities that add to 1, nonzero wherever data can fall, evaluated on data the model hasn't seen |
| When they change | Small samples, zero counts, different tokenisations, numerical features where distance matters |
| Where it fails | Infinite for impossible events; blind to how far apart values are; says nothing about decision costs |
| Related ideas | Entropy (ch04), code lengths (ch05, ch06), mutual information (ch07), capacity (ch09), real sources (ch10) |
What if?
What if we trained classifiers to maximise accuracy directly?
Reason it through, then open
Accuracy changes only when a prediction crosses the threshold, so it gives no gradient for most parameter changes and can't be optimised by the methods that train neural networks; it's also indifferent to confidence, so the resulting probabilities would mean nothing. Surrogates that behave like accuracy near the threshold (the hinge loss of support vector machines) give good decisions but uncalibrated scores. Cross-entropy is used because it's smooth, rewards honest probabilities, and is maximum likelihood; accuracy is measured afterwards.
What if KL divergence were symmetric?
Reason it through, then open
Then mistaking English for French would cost the same as mistaking French for English, and a model that rules out something that happens would be no worse than one that's merely too vague. Real costs aren't like that: KL's asymmetry records which way the mistake goes. Symmetric versions exist (Jensen–Shannon, which is also bounded), and they're useful for comparing distributions when neither is "the truth", but they lose the interpretation as extra code length or as the rate at which evidence accumulates.
What if a model could reach zero cross-entropy?
Reason it through, then open
Only if the data had no information at all: cross-entropy is at least the entropy, which is zero only for a source that always produces the same thing. A model that reaches near-zero cross-entropy on its training data has memorised it (chapter 7's seven-letter contexts, the companion's leaked split), and on new data it will do worse. The floor for any real source is its entropy rate, unknown for language but certainly above zero.
Where the boundary is
| Aspect | (forward) | (reverse) |
|---|---|---|
| Averages over | The data | The model |
| Punishes | Missing where data occurs | Putting probability where data doesn't occur |
| Infinite when | somewhere | somewhere |
| Fitting behaviour | Covers all modes, spreads out | Picks a mode, stays inside |
| Used by | Maximum likelihood, log loss, compression | Variational inference, some reinforcement learning penalties |
| Lab result (two bumps) | Centre 0, width 3.15 | Centre +3, width 1.00 |
Derive it
Why maximum likelihood minimises KL divergence.
- Data come from an unknown ; a model has parameters .
- Maximum likelihood chooses to maximise , equivalently to minimise , the model's average surprise on the data.
- By the law of large numbers (chapter 6), that average tends to as grows.
- , and doesn't depend on .
- So, with enough data, maximum likelihood chooses the model in the family closest to the truth in forward KL divergence, the "covering" direction, which is why it spreads probability over everything the data does.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Identity | The definitions | |
| , zero only for | Theorem | Gibbs' inequality, from |
| An arithmetic coder pays bits, within two | Theorem, and measured | Chapter 6; the exercises' C1 |
| Log loss is a proper scoring rule | Theorem | The exercises' challenge 13 |
| Evidence accumulates at bits per observation | Theorem (Stein's lemma) | Cover and Thomas, chapter 11 |
| Temperature scaling repairs overconfidence | Measured | The exercises' P3; Guo and others (2017) for neural networks |
| Rate–distortion of random bits is | Theorem, and computed | Shannon (1959); the exercises' P4 |
Still open
How to estimate divergences and mutual information between high-dimensional continuous variables reliably is unsolved: neural estimators give bounds that can be loose or biased, and some results show that large values can't be estimated accurately without enormous samples. Why large neural networks, trained to minimise cross-entropy far below what classical theory would call overfitting, still generalise well is an active research question. And how calibrated large language models are, and how to make their stated confidence trustworthy, matters more as their probabilities feed decisions.
What would change your mind? The chapter claims that no model can have a cross-entropy below the data's entropy rate on long, unseen data from the same source. A model that compressed genuinely new text from a stationary source below every reasonable estimate of its entropy rate, consistently and on data it couldn't have seen, would challenge the estimates of the entropy rate, not the theorem; the theorem itself follows from Gibbs' inequality.
Recall
What is cross-entropy?
A model's average surprise on data from the true distribution, H(p, q) = Σ p log₂(1/q).
How do cross-entropy, entropy and KL relate?
H(p, q) = H(p) + D(p || q): the unavoidable cost plus the cost of the wrong model.
Is KL divergence symmetric?
No: D(English || French) was 0.263 bits, D(French || English) 0.155.
What does maximum likelihood minimise?
Cross-entropy on the data, equivalently forward KL D(data || model).
Why do confident mistakes dominate log loss?
A mistake made with probability 1 − ε costs log₂(1/ε) bits, unbounded as ε shrinks.
What is perplexity?
2 to the cross-entropy in bits: the effective number of equally likely choices per token.
What sets the noise floor of a measured KL divergence?
About (k − 1)/(2n ln 2) bits for k categories and n samples.
How fast does evidence accumulate between two hypotheses?
At D(p || q) bits per observation on average (Stein's lemma).
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.