17.4 Information theory problem set, part a: entropy, chain rules, KL and cross-entropy
The first bigram model in AI Engineering reports a loss of about 3.2 nats per byte, and at first that number means nothing. It starts to carry weight quickly. Losses with a fused backward trains with cross-entropy; Entropy, cross-entropy, KL, JS, and the k3 estimator writes the entropy, cross_entropy, and kl functions that the sampler in Sampling and logit processors and the evaluation suite in The evaluation harness and the model zoo log; and the gap between the model’s loss and the data’s entropy is the KL divergence the optimizer is shrinking. Later, GRPO with verifiable rewards keeps a fine-tuned policy close to its reference with a KL penalty estimated by k3.
Concepts from other sections (19)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- bigram model: a language model that predicts the next token from the current token alone, stored as a table. Read more: 6.1 Byte bigram: counts, logits, safetensors v0 in AI Engineering
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator
- Entropy: , the average surprise of a distribution. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator
- KL divergence: , the excess cost of predicting with ; nonnegative and asymmetric. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator
- Chain rule: . Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP
- Mutual information: , the nats that knowing one variable saves about the other. Read more: 17.3 Mutual information, PMI, and PPMI
- Joint entropy: , the uncertainty of a pair of random variables taken together; it is at most , with equality when they are independent. Read more: Chapter 17: Information Theory
- Conditional entropy: , the uncertainty that remains about once is known; conditioning never increases entropy on average, so . Read more: Chapter 17: Information Theory
- Gibbs’ inequality: , with equality if and only if ; coding data from with a code built for always costs extra bits, so cross-entropy is never below entropy. Read more: Chapter 17: Information Theory
- k3 estimator: with , an unbiased, nonnegative per-sample estimate of . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator
- Joint distribution: the table over pairs of values; it determines marginals and conditionals. Read more: 13.10 Probability problem set, part b: joint distributions, covariance, MLE
- Random variable: a number assigned to each outcome of a random experiment; a discrete one is a table of values and probabilities. Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller
- Product rule / quotient rule: and , the rules for differentiating a product or a ratio of two functions. Read more: Chapter 7: Calculus 1
- Byte-level: the base alphabet is the 256 bytes (through the byte map), so every text encodes and there is no unknown token. Read more: 7.2 Byte-level BPE in AI Engineering
- VJP (vector-Jacobian product): , the gradient of ; what reverse mode computes. Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP
- reference policy: the frozen model (usually the SFT checkpoint) against which the policy’s log-ratios are measured. Read more: 18.2 Direct preference optimization and IPO in AI Engineering
- Perplexity: , the geometric mean of ; the effective number of choices per token. Read more: 17.2 Perplexity, bits per byte, NLL accumulator
- Bits per byte (bpb): , total information divided by the UTF-8 length; comparable across tokenizers. Read more: 17.2 Perplexity, bits per byte, NLL accumulator
- Negative log-likelihood (NLL): , the surprise of the true token under the model, in nats. Read more: 17.2 Perplexity, bits per byte, NLL accumulator
This problem set fixes the definitions, the units, and the identities (the chain rule, cross-entropy equals entropy plus KL, KL is nonnegative) on paper, before the code implements them. It has nine problems, sixteen exact parts and two proofs, each with a worked answer folded underneath. The set assumes the distributions, expectation, and conditioning of Probability problem set, part a. Part b continues with coding, mutual information, and maximum entropy.
Learning objectives
- Compute entropy, joint entropy, and conditional entropy exactly, in bits or nats as asked.
- Prove the chain rule and Gibbs’ inequality.
- Compute cross-entropy and KL divergence in both directions and recognize that KL is not symmetric.
- Evaluate the k3 estimator and explain why it is nonnegative for every sample.
The ideas you need
Section titled “The ideas you need”The surprise of an outcome with probability is : certain outcomes surprise you not at all, rare ones a lot, and the surprises of independent outcomes add. Entropy is the expected surprise, , with (the limit of as ). In bits it is the average number of yes/no questions an optimal strategy needs to identify the outcome: 1 for a fair coin, 8 for a uniform byte. It is 0 for a certain outcome and at most over outcomes, with the maximum exactly at the uniform distribution. The unit is the base of the logarithm, bits for and nats for , and changing the base rescales it: . For a coin with , the binary entropy is , symmetric about , where it peaks at 1 bit.
For a pair the joint entropy is the entropy of the joint distribution. The conditional entropy averages, over , the entropy of ‘s distribution once is known. The chain rule , proved in Problem 5, says that the surprise of a pair is the surprise of the first plus the surprise of the second given the first. For a sequence, . A language model is a product of next-token conditionals, and its training loss is this sum. Conditioning never increases entropy on average: .
If data come from but you encode or predict with , the expected surprise is the cross-entropy . A model’s training loss on a corpus is an estimate of . The excess over the best possible, , is the KL divergence. Gibbs’ inequality (Problem 9) says it is at least 0, with equality only when , so minimizing cross-entropy in drives toward . KL is not symmetric and is not a distance: is infinite when gives zero probability to something can produce, which is why a model must never assign probability exactly 0.
Finally, in RL fine-tuning you see one sample at a time and want . With , the estimator is unbiased but often negative for single samples. The k3 estimator is also unbiased, because , so ; and since (the same tangent-line bound as Gibbs’), every single value is nonnegative.
Two warm-up examples
Section titled “Two warm-up examples”Before the problems, here are two siblings of Problems 4 and 6, worked in full.
Take and in with , , , . In bits, the joint entropy is . The marginal is , , so . Computing the conditional directly: given , is with entropy 1; given , surely, with entropy 0. So . The chain rule checks out: .
For cross-entropy and KL, take and . Then and bit, so bit. The other direction, , is infinite: gives zero probability to an outcome produces.
The problems
Section titled “The problems”Each problem carries a tag for the kind of answer it wants ([number], [expr in ...], [bool], [proof]). Answers are exact; means bits and means nats. Try each one on paper first, then open the answer and compare the derivation, not just the number. Each answer names the common wrong answer, most often a unit mix-up or a swapped KL, and why it is wrong.
Entropy and chain rules
Section titled “Entropy and chain rules”Problem 1. Give the entropy in bits of (a) a fair coin, (b) a uniform byte (256 equally likely values), (c) the distribution . [number]
Answer
(a) 1, (b) 8, (c) bits.
- (a) . A common mistake is : that is the same entropy in nats, not bits.
- (b) Uniform over 256: . A common mistake is 256, the number of outcomes rather than its log.
- (c) . A common mistake is , treating the three outcomes as equally likely; the uneven distribution is less uncertain than the uniform one, as the bound says.
Problem 2. Give the entropy in nats of the uniform distribution over outcomes. [expr in V]
Answer
.
Every , so . This is the loss of an untrained model with a uniform output, the number the autograd bigram of Training loop and the autograd bigram starts near. A common mistake is (), which is bits; another is , the probability itself.
Problem 3. The binary entropy is bits. (a) Which maximizes it? (b) Give . [number]
Answer
(a) , (b) bits.
- (a) , which is zero only at , and is concave, so that is the maximum (1 bit). A common mistake is 1, the maximum value instead of where it occurs.
- (b) . A common mistake is , keeping only the first term; another is , a natural log inside a bits answer, which is neither unit.
Problem 4. and take values in with joint distribution , , , . In bits, give (a) , (b) , (c) . [number]
Answer
(a) , (b) , (c) bits.
- (a) The nonzero cells are : . The cell contributes . A common mistake is 2, treating all four cells as .
- (b) , , so , the Problem 3(b) value. A common mistake is 1, assuming is uniform.
- (c) By the chain rule, . Directly: given , is with entropy ; given , surely, entropy 0; so . The two routes agree. A common mistake is to report or here.
Problem 5. Prove the chain rule for discrete random variables, where . [proof]
Answer
Proof. For every pair with , , so
Multiply by and sum over all pairs with (pairs with contribute to every sum below, so dropping them changes nothing):
In the first sum, does not depend on , and , so it is . In the second, write :
So .
The structure is “take logs of the product rule, then average”: the same step, applied repeatedly to , gives the sequence chain rule that makes a language model’s summed next-token loss equal the sequence’s log-loss.
Cross-entropy and KL divergence
Section titled “Cross-entropy and KL divergence”Problem 6. Let and . In bits, give (a) the cross-entropy , (b) , (c) . [number] (d) Is for all distributions ? [bool]
Answer
(a) , (b) , (c) bits, (d) false.
- (a) . A common mistake is 1, which is .
- (b) . A common mistake is to report the cross-entropy itself: “the model is 1.2 bits from the data” when the excess is 0.2.
- (c) . A common mistake is to give the (b) value, swapping the arguments.
- (d) (b) and (c) already differ (0.208 vs 0.189), so KL is not symmetric in general. It weights the log-ratio by its first argument.
Problem 7. A byte-level model that assigns probability to every next byte is evaluated on any text. Give its cross-entropy loss in nats per byte. [number]
Answer
nats per byte.
The loss on each byte is , whatever the byte is, so the cross-entropy is for any text. A common mistake is 8, which is the same number in bits; another is , the probability rather than its surprise.
Problem 8. The k3 estimator of KL used in RL fine-tuning is , with for a sample . (a) Give . [number] (b) Is for every real ? [bool]
Answer
(a) , (b) true.
- (a) . A common mistake is , a sign slip on ; another is , which is the plain log-ratio estimator, not k3.
- (b) for every real , because the exponential is convex and is its tangent line at 0. So , with equality only at . The plain estimator is negative whenever , which is why k3 is what GRPO with verifiable rewards logs.
Problem 9. Prove Gibbs’ inequality: for distributions on a finite set with wherever , with equality exactly when . [proof]
Answer
Proof. Let . Terms outside are 0, and by assumption on , so
Use for (the concave lies below its tangent at ), with equality only at , and :
since ‘s mass on is at most 1. So .
Equality needs both inequalities to be tight. The first is tight only if for every ; the second only if , so has no mass outside , where is also 0. Together, . Conversely makes every log-ratio 0.
(In natural log; in base 2 the same argument goes through, since only rescales.) The second equality condition is easy to forget: matching on its support is not enough if also puts mass elsewhere, because then somewhere on .
Common mistakes
Section titled “Common mistakes”The wrong answers in this set cluster into a few patterns, each tied to the problem where it shows up.
| Mistake | Symptom | How to avoid it |
|---|---|---|
| Reporting nats where bits were asked, or the reverse | bits-per-byte off by a factor | fix the base before computing: for bits, for nats (Problems 1, 2, 7) |
| Mixing a natural log into a bits formula | a value that is neither unit | every log in a bits answer is (Problem 3(b)) |
| Counting a zero-probability cell | entropy too high, or NaN from in code | drop zero cells by (Problem 4(a)) |
| Taking or for | sequence losses that do not add up | compute both directly and by the chain rule (Problem 4(c)) |
| Swapping the arguments of KL | a penalty that weights the wrong distribution’s errors | weight the log-ratio by the first argument (Problem 6(b) and (c)) |
| Reporting the cross-entropy as the KL | “the model is 1.2 bits from the data” when it is 0.2 | subtract from (Problem 6(b)) |
| Using the raw log-ratio as a per-sample KL | negative KL values in training logs | use , nonnegative by the tangent-line bound (Problem 8) |
Where this leads
Section titled “Where this leads”Every result here becomes code. Entropy, cross-entropy, KL, JS, and the k3 estimator implements entropy, cross_entropy, kl, js, and kl_k3 from these definitions, with Gibbs’ inequality as a property test. Matrix differentials, the trace trick, and closed-form VJPs derives the cross-entropy VJP, softmax minus one-hot. In AI Engineering, Losses with a fused backward fuses cross-entropy into the training loss in nats, Sampling and logit processors logs the entropy of each next-token distribution, and GRPO with verifiable rewards estimates the KL penalty to a reference policy with k3. Back in this chapter, Perplexity, bits per byte, NLL accumulator computes perplexity and bits per byte from the same NLL sums.
Further reading
Section titled “Further reading”- Cover and Thomas, Elements of Information Theory, chapter 2.
- MacKay, Information Theory, Inference, and Learning Algorithms (free online), chapters 2 and 4.
- Schulman, “Approximating KL Divergence” (2020 blog note), for k3.
Summary
Section titled “Summary”- Entropy is the expected surprise ; it is for a uniform distribution over outcomes and less for anything else, so an untrained byte model starts at nats (Problems 1, 2, 7).
- The unit is the base of the logarithm: bits for , nats for ; mixing them is the most common wrong answer (Problems 1, 3, 7).
- The chain rule is why next-token cross-entropy summed over a sequence is the sequence’s log-loss (Problems 4, 5).
- Cross-entropy is entropy plus KL divergence, KL is never negative, and it is not symmetric (Problems 6, 9).
- The k3 estimator is nonnegative for every sample, unlike the plain log-ratio (Problem 8).