Advanced Mathematics

Cross-Entropy and KL Divergence

Cross-entropy measures how many bits a model qq needs to describe data really drawn from pp; KL divergence is the excess over the minimum.

Needs a step first: 18

If outcomes come from pp but you encode them as if they came from qq, the average cost is the cross-entropy H(p,q)=−∑pilog⁡qiH(p, q) = -\sum p_i\log q_i. It is never less than the entropy H(p)H(p), and the gap DKL(p ∥ q)=H(p,q)−H(p)=∑pilog⁡(pi/qi)D_{\mathrm{KL}}(p\,\|\,q) = H(p, q) - H(p) = \sum p_i\log(p_i/q_i) is zero only when q=pq = p. For continuous distributions the sums become integrals of densities. Training a classifier or language model means minimising cross-entropy with the true labels, which is the same as maximising likelihood; KL divergence also appears in the loss of diffusion and other generative models.

Where it shows up: It is the training loss of logistic regression, neural network classifiers and language models, and KL terms appear in generative models.

How to hold it: In your head

See cross-entropy as the bits you pay for coding with the wrong model, KL divergence as the excess over the minimum, and why minimising it is maximum likelihood.

What knowing it looks like

  • Compute the cross-entropy and KL divergence between two small distributions.
  • Show that for a one-hot label the cross-entropy loss is −log⁡-\log of the probability given to the right answer.
  • Explain why minimising cross-entropy is the same as maximum likelihood.

Test yourself

A fair coin has p=(12,12)p = (\tfrac12, \tfrac12). A model predicts q=(14,34)q = (\tfrac14, \tfrac34). Compute H(p)H(p), H(p,q)H(p, q) and DKL(p ∥ q)D_{\mathrm{KL}}(p\,\|\,q) in bits. Use log⁡23≈1.585\log_2 3 \approx 1.585.
Show the worked answer
H(p)=1H(p) = 1 bit. H(p,q)=−12log⁡214−12log⁡234=12⋅2+12(2−1.585)=1+0.2075≈1.208H(p, q) = -\tfrac12\log_2\tfrac14 - \tfrac12\log_2\tfrac34 = \tfrac12\cdot2 + \tfrac12(2 - 1.585) = 1 + 0.2075 \approx 1.208 bits. DKL=H(p,q)−H(p)≈0.208D_{\mathrm{KL}} = H(p, q) - H(p) \approx 0.208 bits. Directly: 12log⁡22+12log⁡223=12(1+1−1.585)≈0.208\tfrac12\log_2 2 + \tfrac12\log_2\tfrac23 = \tfrac12(1 + 1 - 1.585) \approx 0.208.

Needs a step first: 18

Learn it with an AI

Copy this prompt into any AI assistant. It will take you through step by step at your level and finish with the test above.

Be my maths tutor. I'm learning "Cross-Entropy and KL Divergence" (Undergraduate II tier): Cross-entropy measures how many bits a model $q$ needs to describe data really drawn from $p$; KL divergence is the excess over the minimum.
I already know: Information and Entropy, Probability Densities.
Teach it Socratically: first ask what I already know; then explain the core idea with one concrete example and why it's true; then give me three problems, one at a time, easy to hard. Wait for my answer, check every step, and point out mistakes without giving the answer away.
Finish by testing me with this: A fair coin has $p = (\tfrac12, \tfrac12)$. A model predicts $q = (\tfrac14, \tfrac34)$. Compute $H(p)$, $H(p, q)$ and $D_{\mathrm{KL}}(p\,\|\,q)$ in bits. Use $\log_2 3 \approx 1.585$.
Use LaTeX for formulas. When you say where it's used later (machine learning, quantum mechanics), be specific.

On the way to

Recommended