Unit 7 of 7 · about 3 hours · derive
Entropy, cross-entropy, KL divergence and mutual information
Classification losses, perplexity and variational objectives are cross-entropies and KL terms in disguise. Misreading their direction or units silently flips what the optimizer rewards.
You'll be able to
- Explain entropy, cross-entropy, KL divergence and mutual information in both probabilistic and coding terms.
- Derive cross-entropy as entropy plus KL divergence and identify which term changes during model fitting.
- Prove and implement stable discrete entropy, cross-entropy, KL and mutual-information estimates.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Self-information, entropy and mutual informationD2L - Information Theory
Sections 22.11.1-22.11.3 with the PyTorch tab: card-deck bits, entropy, joint and conditional entropy, mutual information
- KL divergence and cross-entropy as a lossD2L - Information Theory
Sections 22.11.4-22.11.5 with the PyTorch tab: KL definition and properties, cross-entropy identity , binary and multiclass loss
Run the PyTorch tab, not the MXNet or TensorFlow tab: the TensorFlow conditional-entropy and joint-entropy cells return a vector instead of a scalar, because its
nansumreduces over the last axis only. Treat Section 22.11.4.3 as a symmetry demo only: sorting two sample vectors and scoring them as distributions is not a valid KL estimate. - Entropy, KL non-negativity and mutual informationProbabilistic Machine Learning: An Introduction
Chapter 6, Sections 6.1-6.2 and 6.3.1-6.3.4 (pp. 207-220): entropy and chain rules, Theorem 6.2.1 ( by Jensen), KL versus maximum likelihood, mutual information identities
Focus on
- . Fitting moves only the KL term; is constant for fixed data.
- KL direction matters: punishes near zero where is positive, even infinite for a hard zero. Averaging the wrong direction answers a different question.
- Log base sets the units: base 2 gives bits, base e gives nats, and 1 nat = 1.4427 bits. ProbML Chapter 6 and the D2L entropy helpers use base 2; the D2L cross-entropy loss and PyTorch losses use base e. Convert before comparing.
- Mutual information is zero exactly under independence. Correlation only sees linear dependence; MI sees any.
Common mistakes · 3
- Calling KL a distance. It is asymmetric and violates the triangle inequality: for the Reproduce pair, bits but bits.
- Taking
abs()around a KL implementation, as the D2L helper does. A negative KL signals a bug (unnormalized inputs or swapped logs);abshides it. - Assuming differential entropy is non-negative. Continuous entropy can be negative; only the discrete form holds.
Try it yourself
Closed-book, before checking anything
float64 using log2: , , , with agreement of and to 1e-12 in absolute value.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Prove for discrete distributions and name the step that uses each assumption.Answer
by Jensen for the convex ; the expectation step needs a valid PMF. = sum over support of of , with equality iff supports match; of that is , using a valid PMF. Equality needs on the support of .
2.In Reproduce, which term changes if is fitted to , and what is its minimum?Answer
Only changes; is fixed by the data. Its minimum is 0, attained at , where CE equals .
3. assigns 0 to an outcome with . What happens to and to a Monte Carlo CE estimate?Answer
KL is infinite: the term diverges. A finite sample avoiding that outcome still estimates finite CE, which hides the infinite population KL. Support mismatch is a modeling error, not a sampling artifact.
4. and are independent bits. What are and ?Answer
since conditioning on removes nothing; . Independence zeroes mutual information in both directions.
Practice
- D2L Information Theory Exercises 1, 2 and 4
Section 22.11.7: verify the card-deck entropies, prove by Jensen, and derive
Run the PyTorch tab for the entropy helpers. Skip the TensorFlow tab per the depth note above.
- Stat 110 Penultimate Homework Q5: surprise and entropy
Penultimate Homework Q5 (in the Strategic Practice 10 file): surprise function and the uniform-maximizes-entropy proof by Jensen
Attempt before reading the solution printed in the same file.
You're done when
You recompute the 1.5, 0.0850 and 1.5850 without notes, prove naming the Jensen and support steps, and can predict which KL direction explodes for a given support mismatch.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 3 more in the section
- Probabilistic Machine Learning: An IntroductionKevin Murphy · reference
Consult targeted parts of Chapters 2, 3, 4 and 6 for ML notation, Monte Carlo, factorization, posterior prediction and information theory.
- D2L - Information TheoryD2L authors · required
Use §§22.11.1-22.11.5 for runnable entropy, cross-entropy, KL and mutual-information examples and exercises.
Also in this section
- Harvard Stat 110 - ProbabilityHarvard University · primary
Follow lectures 4-14, 17-23 and 25-30 with matching Strategic Practice. Use lectures 1-3 only for prerequisite repair.
- Stat 110 Strategic PracticeHarvard University · required
Solve the matching sets before marking a probability topic Practised; use solutions to audit reasoning, not to begin it.
- MIT 18.05 - Bayesian bridgeMIT OpenCourseWare · required
Use classes 10-16 for MLE, Bayesian updating, posterior prediction, conjugacy, prior choice and credible intervals.