Skip to content

1.5 Information theory

ready≈ 10.5 hoursNeeds: 1.4Unlocks: 2.4, 5.9, 6.2
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Entropy and KL visualizer)
  4. 4 Do
  5. 5 Check

Every classifier and language model in this course is trained by minimising a cross-entropy (UDL §5.7), and every VAE and diffusion model has a KL term (UDL ch. 17–18). This module explains what those numbers mean.

The surprise of an outcome with probability pp is −log⁡p-\log p: rare events are surprising, certain ones are not. Entropy H(p)H(p)entropy−∑ p(x) log p(x): the average surprise of p. In nats with natural log, bits with log₂.Introduced in 1.5 Information theory. is the average surprise. It is also the average length of the shortest code for messages drawn from pp, in bits when the log is base 2 (Olah, “Calculating Entropy”). It is largest for a uniform distribution and zero when one outcome is certain. Use the convention 0log⁡0=00\log 0 = 0.

Cross-entropy H(p,q)H(p, q)cross-entropy−∑ p(x) log q(x): average code length when data from p is coded using q. H(p, q) = H(p) + KL(p‖q).Introduced in 1.5 Information theory. H(p,q)H(p,q) is the average code length when the data comes from pp but your code was designed for qq. Using the wrong code can only cost extra bits, and that extra cost is the KL divergence DKL(p ∥ q)D_{\mathrm{KL}}(p \,\Vert\, q)KL divergence∑ p(x) log(p(x)/q(x)) ≥ 0, zero only when p = q. Not symmetric.Introduced in 1.5 Information theory. (Olah, “Cross-Entropy” and “KL Divergence”):

H(p,q)=H(p)+DKL(p ∥ q).H(p, q) = H(p) + D_{\mathrm{KL}}(p\,\Vert\,q).

KL is never negative, and it is zero only when p=qp = q. The proof below needs one inequality: Jensen’s, for the concave log (MML §7.3). KL is not symmetric, so it is not a distance in the geometric sense (UDL App. C.5.1–C.5.2). In particular, if qq gives zero probability to an outcome that pp can produce, DKL(p∥q)D_{\mathrm{KL}}(p\Vert q) is infinite, while the reverse direction may be finite. Try it in the widget.

Why this is the loss you will use. Training a classifier minimises the cross-entropy between the data’s labels and the model’s predicted distribution. Since H(data)H(\text{data}) is fixed, that is the same as minimising the KL from the data to the model, and the same as maximising the log likelihood from 1.4 (UDL §5.7). For two Gaussians, KL has a closed form, which the VAE in 6.2 relies on (UDL App. C.5.4). You will confirm it by Monte Carlo in the task.

KL divergence is never negative (Gibbs' inequality)step 1 of 4
  1. −DKL(p∥q)=∑xp(x)log⁡q(x)p(x)=Ex∼p[log⁡q(x)p(x)]-D_{\mathrm{KL}}(p\Vert q) = \sum_x p(x)\log\frac{q(x)}{p(x)} = \mathbb{E}_{x\sim p}\Big[\log\frac{q(x)}{p(x)}\Big]

Entropy and KL visualizer

Entropy, cross-entropy, why KL is asymmetric

Static view on small screens. Open this page on a laptop to interact.

0.4010.3020.1030.1040.0550.056
p: drag bars (they renormalise to sum to 1)
0.1710.1720.1730.1740.1750.176
q: drag bars (they renormalise to sum to 1)
0.5050.254-0.074-0.074-0.087-0.087pᵢ log(pᵢ/qᵢ): terms can be negative, the sum never is
H(p) = 2.146 H(q) = 2.585 H(p, q) = 2.585 KL(p‖q) = 0.439 KL(q‖p) = 0.473 bits

H(p, q) = H(p) + KL(p‖q): cross-entropy is the cost of coding p's outcomes with a code built for q. KL is zero only when the bars match, and it is not symmetric. If q gives zero probability to something p does, KL(p‖q) is infinite. The reverse direction does not care.

Two distributions over six outcomes as bar charts you can reshape. The readouts give H(p), H(q), the cross-entropy and KL in both directions, and a third chart shows each outcome’s contribution to KL(p‖q). Keyboard: Tab to a bar, then arrow up and down to change it.

Implement entropy (in any base), cross-entropy, KL with correct handling of zeros, the closed-form Gaussian KL, and a Monte Carlo KL estimator. One test checks KL ≥ 0 on 1000 random pairs.

Entropy, cross-entropy and KL stubs/m1_5_information.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m1_5_information.py and run mlc check 1.5. The tests are identical; import the results on the progress page.

Hints · entropy 0/3 revealed

Locally: mlc check 1.5.

1. What is the entropy of a fair 8-sided die, in bits?
2. H(p) = 1.2 nats and H(p, q) = 1.5 nats. What is KL(p‖q)?
3. p puts probability 0.1 on an outcome to which q gives probability 0. What is KL(p‖q)?
4. Minimising cross-entropy loss over model parameters is equivalent to…
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.