On this page
KL divergence is a standard way to measure how far a probability model is from the truth, and two short coin calculations show almost everything about it.
What does KL divergence measure?
Kullback–Leibler divergence was introduced by S. Kullback and R. A. Leibler in the paper "On Information and Sufficiency", published in The Annals of Mathematical Statistics in March 1951 (DOI record).
For two distributions over the same outcomes, the discrete definition is D(P‖Q) = Σ P(x) log(P(x)/Q(x)), the expectation of the log difference between P and Q taken using the probabilities P (Kullback–Leibler divergence, Wikipedia).
Read it as an average: P is what really happens, Q is your model, and each outcome's log(P/Q) is weighted by how often it really occurs. Log base 2 gives bits; the natural log gives nats.
If Q gives probability 0 to an outcome P allows, the divergence is usually defined as infinite (same article).
Is KL divergence symmetric? A two-coin worked example
Using the definition above (Wikipedia), let P be a fair coin (heads 0.5, tails 0.5) and Q a biased coin (heads 0.9, tails 0.1). Logs are base 2.
Direction 1, D(P‖Q): the truth is fair, the model is biased.
Heads: 0.5 × log2(0.5/0.9) = 0.5 × (−0.848) = −0.424
Tails: 0.5 × log2(0.5/0.1) = 0.5 × 2.322 = 1.161
Total: D(P‖Q) = log2(5/3) ≈ 0.737 bits
Direction 2, D(Q‖P): the truth is biased, the model is fair.
Heads: 0.9 × log2(0.9/0.5) = 0.9 × 0.848 = 0.763
Tails: 0.1 × log2(0.1/0.5) = 0.1 × (−2.322) = −0.232
Total: D(Q‖P) ≈ 0.531 bits
Same two coins, two different answers. So KL divergence is not a true distance (a metric): it is not symmetric and does not satisfy the triangle inequality (Wikipedia).
The intuition is in the tails term. In direction 1, tails comes up half the time but the biased model calls it a 1-in-10 event, so it is badly surprised often. In direction 2, the fair model gives 0.5 to everything; it is never confidently wrong, so it pays less.
Why is KL divergence never negative?
Gibbs' inequality states that D(P‖Q) ≥ 0, with equality if and only if P = Q (Gibbs' inequality, Wikipedia). The proof needs one fact, ln x ≤ x − 1 for every x > 0, with equality only at x = 1. Sum over the outcomes where p > 0:
Write −D(P‖Q) = Σ p ln(q/p).
Apply ln x ≤ x − 1 with x = q/p: Σ p ln(q/p) ≤ Σ p(q/p − 1) = Σ q − Σ p.
Σ p = 1, and Σ q over these outcomes is at most 1, so the right side is at most 0.
So D(P‖Q) ≥ 0. Equality needs q/p = 1 for every outcome, which means Q = P.
The same result follows from the concavity of log; if that route is new, see how Jensen's inequality works for convex functions.
How is KL divergence related to cross-entropy loss?
Cross-entropy is H(P,Q) = −Σ P(x) log Q(x), and it splits as H(P,Q) = H(P) + D(P‖Q), where H(P) is the entropy of P (Cross-entropy, Wikipedia). Check it on the coins: H(P,Q) = 0.5 × 0.152 + 0.5 × 3.322 ≈ 1.737 bits, the fair coin's 1 bit of entropy plus 0.737 bits of divergence.
When you train a model, P comes from the data and is fixed, so H(P) is a constant. Cross-entropy and KL divergence are then identical up to that constant, so minimising one minimises the other (Cross-entropy, Wikipedia). PyTorch's CrossEntropyLoss "computes the cross entropy loss between input logits and target" (PyTorch documentation).
A worked case: a photo is labelled "cat", so P = (1, 0, 0) over cat, dog, bird. The model says Q = (0.7, 0.2, 0.1). Here H(P) = 0, so the loss equals the KL divergence: −ln 0.7 ≈ 0.357 nats. If the model gave cat only 0.1, the loss would be ln 10 ≈ 2.303 nats. The same −log of the probability given to what was observed sits at the heart of maximum likelihood estimation, with worked examples.
How do you check a KL divergence answer?
Is the total zero or more? A negative total means a slip: weights taken from Q while the log still reads P/Q, or probabilities that do not sum to 1.
Is it zero only when the two distributions match?
Which distribution weights the sum? Always the first one, P in D(P‖Q).
Does Q give 0 where P does not? Then the answer is infinite.
Bits or nats? Divide a value in nats by ln 2 ≈ 0.693 to get bits (units, Wikipedia).
If a step such as ln x ≤ x − 1 or entropy itself feels shaky, the Fermi Concept Library has every concept in mathematics with its own lesson and its place in the map. To practise, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



