Concept · Chapter 2: The Math Toolkit
KL Divergence
KL divergence measures how much one probability distribution differs from another — the extra surprise you pay for using the wrong distribution.
The problem
We often need to compare two distributions — a student model and a teacher, a fine-tuned model and its starting point — with a single number.
The solution
Compute the expected log-ratio of the two distributions' probabilities; it is zero only when they match and positive otherwise.
The consequence
It shows up as a penalty or target throughout modern AI: distillation, the RLHF penalty that keeps a tuned model near its base, variational methods and more.
You should understand first
Intuition
If you design a code for distribution but the data actually follows , you'll use more bits than necessary. KL divergence is exactly that extra cost. If there's no waste; the further apart they are, the more you pay.
Tiny numeric example (nats)
(a fair coin), (a model that thinks heads is very likely):
Different numbers: direction matters.
The equation
Where it appears in AI
- Knowledge distillation (Chapter 11): train a small student to match a large teacher's output distribution by minimizing KL between them.
- RLHF (Chapter 10): a KL penalty keeps the tuned model from drifting too far from its original behaviour while chasing reward.
- DPO and other preference methods are derived from objectives with a KL constraint.
What to remember
- KL(p ‖ q) = Σ p log(p/q) ≥ 0, and = 0 only when p = q.
- Not symmetric: KL(p ‖ q) ≠ KL(q ‖ p) — it isn't a true distance.
- Cross-entropy H(p, q) = H(p) + KL(p ‖ q).
- Used to keep models close to a reference (RLHF), and to match a teacher (distillation).
Key papers
On Information and Sufficiency
S. Kullback, R. A. Leibler · 1951 · The Annals of Mathematical Statistics
Introduced the divergence now called KL divergence — used in distillation, RLHF's penalty term, variational methods and more.
Watch
Aurélien Géron
A Short Introduction to Entropy, Cross-Entropy and KL-Divergence
Ten minutes that tie entropy, cross-entropy and KL divergence together — the three quantities behind language-model training.