Information theory problem set, part a: entropy, chain rules, KL and cross-entropy
Overview
Section titled “Overview”| Module | S-M11a · solve · none · Pass 2 · 3 to 4 h |
| You build | answers in solve/S-M11a.toml (16 checked by SymPy) and 2 proofs in solve/S-M11a/qN.md (self-graded against their rubrics) |
| Contract | none: a pen and paper set |
| Tests | course/solve/S-M11a/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M11a/problems.md and in section 4 |
| Needs | S-M07a (distributions, expectation, Bayes). Reading: the Information Theory topic, entropy and KL sections |
| Used by | no call site (a solve set). It checks the definitions behind M11.1 (entropy, cross_entropy, kl, kl_from_logprobs, js, kl_k3), which M08.3, L0.3, L8.1, and L12.3 call; part b, S-M11b in Pass 3, covers coding, mutual information, and maximum entropy |
| Milestone | MS-P2 (the Pass 2 gate runs ol check on every solve part of the pass) |
| Optional depth | Cover and Thomas, Elements of Information Theory, ch. 2; MacKay, Information Theory, Inference, and Learning Algorithms (free), ch. 2 and 4; Schulman, “Approximating KL Divergence” (2020 blog note) for k3 |
Key Takeaways
Section titled “Key Takeaways”- 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 (q1, q2, q7).
- The unit is the base of the logarithm: bits for , nats for ; mixing them is the most common wrong answer (q1, q3, q7).
- The chain rule is why next-token cross-entropy summed over a sequence is the sequence’s log-loss (q4, q5).
- Cross-entropy is entropy plus KL divergence, KL is never negative, and it is not symmetric (q6, q9).
- The k3 estimator is nonnegative for every sample, unlike the plain log-ratio (q8).
How to work this chapter
Section titled “How to work this chapter”ol start S-M11a # writes solve/S-M11a.toml and one file per proofol check S-M11a # SymPy checks the answers, then asks each proof rubric (y/n)ol check S-M11a --regrade # ask the rubrics again after you change a proof1. Why now
Section titled “1. Why now”Your Pass 1 bigram reported a loss of about 3.2 nats per byte, and the number meant nothing yet. In Pass 2 it starts to carry weight: L0.3 trains with cross-entropy, M11.1 writes entropy, cross_entropy, and kl that the sampler (L8.1) and the evaluation suite (L6.7) log, and the gap between your model’s loss and the data’s entropy is the KL divergence your optimizer is shrinking. Later, L12.3 keeps a fine-tuned policy close to its reference with a KL penalty estimated by k3. This set fixes the definitions, the units, and the identities (chain rule, cross-entropy equals entropy plus KL, KL is nonnegative) before you code them.
2. Principles
Section titled “2. Principles”| Symbol | Meaning | Type / shape |
|---|---|---|
| probability distributions over a finite set, , | float[V] | |
| logarithm in base : gives bits, gives nats | ||
| entropy, | scalar | |
| cross-entropy, | scalar | |
| KL divergence, | scalar | |
| joint entropy of a pair of random variables | scalar | |
| conditional entropy, | scalar | |
| binary entropy of a coin with | scalar | |
| log-ratio of one sample | scalar |
2.1 Entropy
Section titled “2.1 Entropy”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. Changing the base rescales it: .
For a coin, , symmetric about , where it peaks at 1 bit.
2.2 Joint and conditional entropy
Section titled “2.2 Joint and conditional entropy”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 (q5) 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: .
2.3 Cross-entropy and KL divergence
Section titled “2.3 Cross-entropy and KL divergence”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 (q9) 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.
2.4 Estimating KL from samples
Section titled “2.4 Estimating KL from samples”In RL fine-tuning you see one sample at a time and want . With , the estimator is unbiased but often negative for single samples. k3 is also unbiased, because , so ; and since (the same tangent-line bound as Gibbs’), every single value is nonnegative.
3. Worked example by hand
Section titled “3. Worked example by hand”This is a sibling of q4 and q6, not one of the graded problems.
Joint and conditional entropy. and in with , , , .
- Joint, in bits: .
- Marginal: , , so .
- Conditional, computed directly: given , is with entropy 1; given , surely, entropy 0. So .
- Chain rule check: .
Cross-entropy and KL. and : , bit, so bit. The other direction, , is infinite: gives zero probability to an outcome produces. In solve/ a bits answer such as is written 1 - log(3, 2)/2.
4. The problem set
Section titled “4. The problem set”Write each answer in solve/S-M11a.toml; lettered parts are their own tables:
[q1.c]answer = "3/2"[q2]answer = "log(V)"[q6.b]answer = "1 - log(3, 2)/2"[q9]proof = "S-M11a/q9.md"Bits use log(x, 2); nats use log(x). Give exact values, not decimals.
Entropy and chain rules
Section titled “Entropy and chain rules”q1. Give the entropy in bits of (a) a fair coin, (b) a uniform byte (256 equally likely values), (c) the distribution . [number]
q2. Give the entropy in nats of the uniform distribution over outcomes. [expr in V]
q3. The binary entropy is bits. (a) Which maximizes it? (b) Give . [number]
q4. and take values in with joint distribution , , , . In bits, give (a) , (b) , (c) . [number]
q5. Prove the chain rule for discrete random variables, where . [proof]
Cross-entropy and KL divergence
Section titled “Cross-entropy and KL divergence”q6. Let and . In bits, give (a) the cross-entropy , (b) , (c) . [number] (d) Is for all distributions ? [bool]
q7. 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]
q8. The k3 estimator of KL used in RL fine-tuning is , with for a sample . (a) Give . [number] (b) Is for every real ? [bool]
q9. Prove Gibbs’ inequality: for distributions on a finite set with wherever , with equality exactly when . [proof]
5. Pitfalls
Section titled “5. Pitfalls”| Pitfall | Symptom | Caught by |
|---|---|---|
| Reporting nats where bits were asked, or the reverse | bits-per-byte off by a factor | q1 (canary log(2)), q2 (canary in bits), q7 (canary 8) |
| Mixing a natural log into a bits formula | a value that is neither unit | q3 (canary 2 - 3*log(3)/4) |
| Counting a zero-probability cell | entropy too high, or NaN from in code | q4 (canary 2) |
| Taking or for | sequence losses that do not add up | q4 (canaries) |
| Swapping the arguments of KL | a penalty that weights the wrong distribution’s errors | q6 (canaries: the other direction) |
| Reporting the cross-entropy as the KL | “the model is 1.2 bits from the data” when it is 0.2 | q6 (canary: the cross-entropy) |
| Using the raw log-ratio as a per-sample KL | negative KL values in training logs | q8 (canary log(2)) |
6. Where it’s used next
Section titled “6. Where it’s used next”| Direction | Module | How it uses this |
|---|---|---|
| Back | S-M07a | distributions, expectation, and conditioning |
| Forward | M11.1 | entropy, cross_entropy, kl, js, kl_k3 are this set as code, with the Gibbs property test |
| Forward | M08.3 | the cross-entropy VJP, softmax minus one-hot |
| Forward | L0.3 | fused cross-entropy, the training loss in nats |
| Forward | L8.1 | the sampler logs the entropy of each next-token distribution |
| Forward | M11.2 | perplexity and bits per byte from the same NLL sums |
| Forward | L12.3 | the KL penalty to a reference policy, estimated with k3 |