Workbench / Information Theory Exercises
B1·11
Information in Machine Learning: Cross-Entropy and KL Divergence
Exercises for chapter 11: cross-entropy as entropy plus divergence, why divergence isn't a distance, why classifiers minimise log loss, what perplexity means, how to read a drift score, and which way round to take KL, in your own words; divergences and log loss by hand, the noise floor of a measured divergence, an overconfident classifier repaired with one number (its temperature), rate–distortion computed with Blahut–Arimoto, chapter 6's arithmetic coder paying exactly the cross-entropy with the wrong letter counts, evidence for a language piling up letter by letter, and senior exercises on a model that's accurate but dangerous, a drift dashboard that cries wolf, and choosing what to optimise.
Start here: how these exercises work
Write your answer in the box first; hints open one at a time as you write, and the solution a couple of minutes after the last hint. Theory is in your own words, practice and programs come with real output (Python 3.13 with NumPy and scikit-learn), cumulative projects use chapters 1 to 11 together, what the chapter leaves out explains what it skips, and senior exercises put the ideas into designs and reviews. The programs import the chapter's kl.py, chapter 10's real.py and chapter 6's ac.py.
Theory
T1. Entropy plus divergence
Explain, without formulas, why coding data with a model costs the data's entropy plus the divergence, and why the divergence is the only part a better model can reduce.
T2. Not a distance
Explain why and differ, with an example where one is infinite and the other finite.
T3. Why log loss?
A colleague asks why classifiers are trained with cross-entropy rather than accuracy. Give three reasons.
T4. What perplexity means
Explain what a perplexity of 20 means for a word-level language model, and why perplexities of models with different vocabularies can't be compared.
T5. Reading a drift score
A dashboard shows the divergence of today's input distribution from last month's as 0.03 bits. Is that a problem? Explain what you need to know.
T6. Which way round
Explain, with the two-bump example, why fitting a model by maximum likelihood gives a different answer from variational inference.
Practice and programs
P1. By hand
(a) , : compute , and . (b) A classifier assigns probabilities 0.8, 0.6, 0.95 and 0.3 to the true classes of four examples. Compute its log loss in bits. (c) A model's cross-entropy falls from 5 to 4.5 bits per token. By what factor does its perplexity fall? (d) Show that when is uniform over 3 outcomes.
P2. The noise floor of a measured divergence
Draw windows of 100, 1,000 and 10,000 samples from a distribution and measure their divergence from that same distribution. Compare with .
P3. Repairing an overconfident classifier
Train a barely regularised logistic regression (it becomes overconfident), measure its log loss and calibration on test data, then divide its log-odds by a temperature chosen to minimise log loss on separate validation data.
P4. Rate–distortion, computed
Compute the rate–distortion curve of random bits with the Blahut–Arimoto iteration (the rate–distortion version, which trades rate against distortion with a slope ), and compare with .
Cumulative projects (chapters so far: 01–11)
C1. The arithmetic coder pays the cross-entropy
Encode the English declaration's letters with chapter 6's arithmetic coder, using letter counts from English, French, German and Finnish, and compare the bits used with . Use chapter 3's add-one smoothing so no letter is impossible.
C2. Evidence, letter by letter
Train blended order-3 models on English and French (from chapter 10), then read unseen English and add up , the weight of evidence, letter by letter. How fast does it grow, and how often does it point the wrong way?
What the chapter leaves out
f-divergences and other distances
KL is one member of a family, the -divergences (Imre Csiszár, 1963), which also includes the total variation distance, the Hellinger distance and the chi-squared divergence. The Jensen–Shannon divergence is symmetric and bounded by 1 bit. The Wasserstein ("earth mover's") distance measures how far probability must move, not how much it overlaps, and stays finite between distributions that don't overlap at all, which is why it's used to train some generative models.
The information bottleneck and representation learning
Deep learning can be read in information terms: a network's hidden layers compress the input while keeping what predicts the label, (Tishby, Pereira and Bialek, 1999). How well this describes real networks is debated, but the objective has inspired methods that trade compression against prediction explicitly.
Minimum description length
Jorma Rissanen's minimum description length principle (1978) chooses the model that gives the shortest total description of the model and the data coded with it: cross-entropy plus the model's own cost. It's chapter 4's report of Squeeze (data cost plus model cost) as a principle of statistics, and it penalises overfitting automatically.
Bayesian surprise and active learning
The divergence between a belief before and after seeing data, , measures how much the data taught you. Experiments and active-learning systems choose what to measure next by maximising the expected divergence, the expected information gain, which is a mutual information again.
Senior exercises
S1. Accurate but dangerous
A medical-triage model has 95 % accuracy and the team wants to use its probabilities to decide which patients to see first. Its log loss on held-out data is 0.9 bits per case, and the labels' entropy is 0.6 bits. What does that tell you, and what do you do?
S2. A drift dashboard that cries wolf
Every feature on a team's drift dashboard is flagged every day, so the team ignores it. The dashboard computes KL divergence between each day's histogram (often a few hundred samples, up to 100 bins) and a fixed reference, with a threshold of 0.01 bits. Diagnose and redesign it.
S3. Choosing what to optimise
A product team building a recommendation model argues about the training objective: cross-entropy on clicks, or directly optimising the click-through rate of the top recommendation. Frame the trade-off.