Skip to content

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.

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 H(X,Y)=H(X)+H(Y∣X)H(X, Y) = H(X) + H(Y \mid X) 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 surprise of an outcome with probability pip_i is −log⁡pi-\log p_i: certain outcomes surprise you not at all, rare ones a lot, and the surprises of independent outcomes add. Entropy is the expected surprise, H(p)=−∑ipilog⁡piH(p) = -\sum_i p_i \log p_i, with 0log⁡0=00 \log 0 = 0 (the limit of tlog⁡tt \log t as t→0t \to 0). 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 log⁡V\log V over VV outcomes, with the maximum exactly at the uniform distribution. The unit is the base of the logarithm, bits for log⁡2\log_2 and nats for ln⁡\ln, and changing the base rescales it: Hnats=Hbits⋅ln⁡2H_{\text{nats}} = H_{\text{bits}} \cdot \ln 2. For a coin with P(1)=pP(1) = p, the binary entropy is Hb(p)=−plog⁡2p−(1−p)log⁡2(1−p)H_b(p) = -p \log_2 p - (1-p)\log_2(1-p), symmetric about p=1/2p = 1/2, where it peaks at 1 bit.

For a pair (X,Y)(X, Y) the joint entropy H(X,Y)H(X, Y) is the entropy of the joint distribution. The conditional entropy H(Y∣X)=∑xP(x)H(Y∣X=x)H(Y \mid X) = \sum_x P(x) H(Y \mid X = x) averages, over xx, the entropy of YY‘s distribution once X=xX = x is known. The chain rule H(X,Y)=H(X)+H(Y∣X)H(X, Y) = H(X) + H(Y \mid X), 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, H(X1,…,XT)=∑tH(Xt∣X<t)H(X_1, \dots, X_T) = \sum_t H(X_t \mid X_{<t}). A language model is a product of next-token conditionals, and its training loss is this sum. Conditioning never increases entropy on average: H(Y∣X)≤H(Y)H(Y \mid X) \le H(Y).

If data come from pp but you encode or predict with qq, the expected surprise is the cross-entropy H(p,q)=−∑ipilog⁡qiH(p, q) = -\sum_i p_i \log q_i. A model’s training loss on a corpus is an estimate of H(pdata,qmodel)H(p_{\text{data}}, q_{\text{model}}). The excess over the best possible, DKL(p ∥ q)=H(p,q)−H(p)=∑ipilog⁡piqiD_{KL}(p \,\Vert\, q) = H(p, q) - H(p) = \sum_i p_i \log \frac{p_i}{q_i}, is the KL divergence. Gibbs’ inequality (Problem 9) says it is at least 0, with equality only when q=pq = p, so minimizing cross-entropy in qq drives qq toward pp. KL is not symmetric and is not a distance: DKL(p ∥ q)D_{KL}(p \,\Vert\, q) is infinite when qq gives zero probability to something pp can produce, which is why a model must never assign probability exactly 0.

Finally, in RL fine-tuning you see one sample x∼px \sim p at a time and want DKL(p ∥ pref)D_{KL}(p \,\Vert\, p_{\text{ref}}). With r=log⁡pref(x)−log⁡p(x)r = \log p_{\text{ref}}(x) - \log p(x), the estimator k1=−rk_1 = -r is unbiased but often negative for single samples. The k3 estimator er−r−1e^r - r - 1 is also unbiased, because Ep[er]=∑xp(x)pref(x)p(x)=1E_p[e^r] = \sum_x p(x) \frac{p_{\text{ref}}(x)}{p(x)} = 1, so E[k3]=1+E[−r]−1=DKLE[k_3] = 1 + E[-r] - 1 = D_{KL}; and since er≥1+re^r \ge 1 + r (the same tangent-line bound as Gibbs’), every single value is nonnegative.

Before the problems, here are two siblings of Problems 4 and 6, worked in full.

Take XX and YY in {0,1}\{0, 1\} with P(0,0)=1/4P(0,0) = 1/4, P(0,1)=1/4P(0,1) = 1/4, P(1,0)=1/2P(1,0) = 1/2, P(1,1)=0P(1,1) = 0. In bits, the joint entropy is H(X,Y)=14⋅2+14⋅2+12⋅1+0=3/2H(X, Y) = \tfrac14 \cdot 2 + \tfrac14 \cdot 2 + \tfrac12 \cdot 1 + 0 = 3/2. The marginal is P(X=0)=1/2P(X=0) = 1/2, P(X=1)=1/2P(X=1) = 1/2, so H(X)=1H(X) = 1. Computing the conditional directly: given X=0X = 0, YY is (1/2,1/2)(1/2, 1/2) with entropy 1; given X=1X = 1, Y=0Y = 0 surely, with entropy 0. So H(Y∣X)=12⋅1+12⋅0=1/2H(Y \mid X) = \tfrac12 \cdot 1 + \tfrac12 \cdot 0 = 1/2. The chain rule checks out: H(X)+H(Y∣X)=1+1/2=3/2=H(X,Y)H(X) + H(Y \mid X) = 1 + 1/2 = 3/2 = H(X, Y).

For cross-entropy and KL, take p=(1,0)p = (1, 0) and q=(1/2,1/2)q = (1/2, 1/2). Then H(p)=0H(p) = 0 and H(p,q)=−1⋅log⁡2(1/2)=1H(p, q) = -1 \cdot \log_2(1/2) = 1 bit, so DKL(p ∥ q)=1D_{KL}(p \,\Vert\, q) = 1 bit. The other direction, DKL(q ∥ p)=12log⁡21/21+12log⁡21/20D_{KL}(q \,\Vert\, p) = \tfrac12 \log_2 \frac{1/2}{1} + \tfrac12 \log_2 \frac{1/2}{0}, is infinite: pp gives zero probability to an outcome qq produces.

Each problem carries a tag for the kind of answer it wants ([number], [expr in ...], [bool], [proof]). Answers are exact; log⁡2\log_2 means bits and ln⁡\ln 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.

Problem 1. Give the entropy in bits of (a) a fair coin, (b) a uniform byte (256 equally likely values), (c) the distribution p=(1/2,1/4,1/4)p = (1/2, 1/4, 1/4). [number]

Answer

(a) 1, (b) 8, (c) 3/23/2 bits.

  1. (a) −12log⁡212−12log⁡212=12+12=1-\tfrac12\log_2\tfrac12 - \tfrac12\log_2\tfrac12 = \tfrac12 + \tfrac12 = 1. A common mistake is ln⁡2\ln 2: that is the same entropy in nats, not bits.
  2. (b) Uniform over 256: H=log⁡2256=8H = \log_2 256 = 8. A common mistake is 256, the number of outcomes rather than its log.
  3. (c) 12⋅1+14⋅2+14⋅2=3/2\tfrac12 \cdot 1 + \tfrac14 \cdot 2 + \tfrac14 \cdot 2 = 3/2. A common mistake is log⁡23≈1.585\log_2 3 \approx 1.585, treating the three outcomes as equally likely; the uneven distribution is less uncertain than the uniform one, as the bound H≤log⁡VH \le \log V says.

Problem 2. Give the entropy in nats of the uniform distribution over VV outcomes. [expr in V]

Answer

ln⁡V\ln V.

Every pi=1/Vp_i = 1/V, so H=−∑i1Vln⁡1V=ln⁡VH = -\sum_i \frac1V \ln\frac1V = \ln V. 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 log⁡2V\log_2 V (ln⁡V/ln⁡2\ln V / \ln 2), which is bits; another is 1/V1/V, the probability itself.

Problem 3. The binary entropy is Hb(p)=−plog⁡2p−(1−p)log⁡2(1−p)H_b(p) = -p \log_2 p - (1 - p) \log_2 (1 - p) bits. (a) Which pp maximizes it? (b) Give Hb(1/4)H_b(1/4). [number]

Answer

(a) p=1/2p = 1/2, (b) Hb(1/4)=2−34log⁡23≈0.811H_b(1/4) = 2 - \tfrac34\log_2 3 \approx 0.811 bits.

  1. (a) Hb′(p)=log⁡21−ppH_b'(p) = \log_2\frac{1-p}{p}, which is zero only at p=1/2p = 1/2, and HbH_b is concave, so that is the maximum (1 bit). A common mistake is 1, the maximum value instead of where it occurs.
  2. (b) −14log⁡214−34log⁡234=14⋅2+34(2−log⁡23)=2−34log⁡23-\tfrac14\log_2\tfrac14 - \tfrac34\log_2\tfrac34 = \tfrac14 \cdot 2 + \tfrac34(2 - \log_2 3) = 2 - \tfrac34\log_2 3. A common mistake is 1/21/2, keeping only the first term; another is 2−34ln⁡32 - \tfrac34\ln 3, a natural log inside a bits answer, which is neither unit.

Problem 4. XX and YY take values in {0,1}\{0, 1\} with joint distribution P(0,0)=1/2P(0,0) = 1/2, P(0,1)=1/4P(0,1) = 1/4, P(1,0)=0P(1,0) = 0, P(1,1)=1/4P(1,1) = 1/4. In bits, give (a) H(X,Y)H(X, Y), (b) H(X)H(X), (c) H(Y∣X)H(Y \mid X). [number]

Answer

(a) 3/23/2, (b) 2−34log⁡23≈0.8112 - \tfrac34\log_2 3 \approx 0.811, (c) 34log⁡23−12≈0.689\tfrac34\log_2 3 - \tfrac12 \approx 0.689 bits.

  1. (a) The nonzero cells are 1/2,1/4,1/41/2, 1/4, 1/4: H(X,Y)=12⋅1+14⋅2+14⋅2=3/2H(X, Y) = \tfrac12 \cdot 1 + \tfrac14 \cdot 2 + \tfrac14 \cdot 2 = 3/2. The cell P(1,0)=0P(1,0) = 0 contributes 0log⁡0=00 \log 0 = 0. A common mistake is 2, treating all four cells as 1/41/4.
  2. (b) P(X=0)=3/4P(X = 0) = 3/4, P(X=1)=1/4P(X = 1) = 1/4, so H(X)=Hb(1/4)H(X) = H_b(1/4), the Problem 3(b) value. A common mistake is 1, assuming XX is uniform.
  3. (c) By the chain rule, H(Y∣X)=H(X,Y)−H(X)=32−2+34log⁡23H(Y \mid X) = H(X, Y) - H(X) = \tfrac32 - 2 + \tfrac34\log_2 3. Directly: given X=0X = 0, YY is (2/3,1/3)(2/3, 1/3) with entropy log⁡23−2/3\log_2 3 - 2/3; given X=1X = 1, Y=1Y = 1 surely, entropy 0; so H(Y∣X)=34(log⁡23−23)=34log⁡23−12H(Y \mid X) = \tfrac34(\log_2 3 - \tfrac23) = \tfrac34\log_2 3 - \tfrac12. The two routes agree. A common mistake is to report H(X,Y)H(X, Y) or H(X)H(X) here.

Problem 5. Prove the chain rule H(X,Y)=H(X)+H(Y∣X)H(X, Y) = H(X) + H(Y \mid X) for discrete random variables, where H(Y∣X)=∑xP(x)H(Y∣X=x)H(Y \mid X) = \sum_x P(x) H(Y \mid X = x). [proof]

Answer

Proof. For every pair with P(x,y)>0P(x, y) > 0, P(x,y)=P(x) P(y∣x)P(x, y) = P(x)\,P(y \mid x), so

−log⁡P(x,y)=−log⁡P(x)−log⁡P(y∣x).-\log P(x, y) = -\log P(x) - \log P(y \mid x).

Multiply by P(x,y)P(x, y) and sum over all pairs with P(x,y)>0P(x, y) > 0 (pairs with P(x,y)=0P(x, y) = 0 contribute 0log⁡0=00 \log 0 = 0 to every sum below, so dropping them changes nothing):

H(X,Y)=−∑x,yP(x,y)log⁡P(x)  −  ∑x,yP(x,y)log⁡P(y∣x).H(X, Y) = -\sum_{x, y} P(x, y)\log P(x) \;-\; \sum_{x, y} P(x, y)\log P(y \mid x).

In the first sum, log⁡P(x)\log P(x) does not depend on yy, and ∑yP(x,y)=P(x)\sum_y P(x, y) = P(x), so it is −∑xP(x)log⁡P(x)=H(X)-\sum_x P(x)\log P(x) = H(X). In the second, write P(x,y)=P(x)P(y∣x)P(x, y) = P(x)P(y \mid x):

−∑xP(x)∑yP(y∣x)log⁡P(y∣x)=∑xP(x) H(Y∣X=x)=H(Y∣X).-\sum_x P(x) \sum_y P(y \mid x)\log P(y \mid x) = \sum_x P(x)\, H(Y \mid X = x) = H(Y \mid X).

So H(X,Y)=H(X)+H(Y∣X)H(X, Y) = H(X) + H(Y \mid X). ■\blacksquare

The structure is “take logs of the product rule, then average”: the same step, applied repeatedly to P(x1,…,xT)=∏tP(xt∣x<t)P(x_1, \dots, x_T) = \prod_t P(x_t \mid x_{<t}), gives the sequence chain rule that makes a language model’s summed next-token loss equal the sequence’s log-loss.

Problem 6. Let p=(1/2,1/2)p = (1/2, 1/2) and q=(1/4,3/4)q = (1/4, 3/4). In bits, give (a) the cross-entropy H(p,q)H(p, q), (b) DKL(p ∥ q)D_{KL}(p \,\Vert\, q), (c) DKL(q ∥ p)D_{KL}(q \,\Vert\, p). [number] (d) Is DKL(p ∥ q)=DKL(q ∥ p)D_{KL}(p \,\Vert\, q) = D_{KL}(q \,\Vert\, p) for all distributions p,qp, q? [bool]

Answer

(a) 2−12log⁡23≈1.2082 - \tfrac12\log_2 3 \approx 1.208, (b) 1−12log⁡23≈0.2081 - \tfrac12\log_2 3 \approx 0.208, (c) 34log⁡23−1≈0.189\tfrac34\log_2 3 - 1 \approx 0.189 bits, (d) false.

  1. (a) H(p,q)=−12log⁡214−12log⁡234=12⋅2+12(2−log⁡23)=2−12log⁡23H(p, q) = -\tfrac12\log_2\tfrac14 - \tfrac12\log_2\tfrac34 = \tfrac12 \cdot 2 + \tfrac12(2 - \log_2 3) = 2 - \tfrac12\log_2 3. A common mistake is 1, which is H(p)H(p).
  2. (b) DKL(p ∥ q)=H(p,q)−H(p)=2−12log⁡23−1D_{KL}(p \,\Vert\, q) = H(p, q) - H(p) = 2 - \tfrac12\log_2 3 - 1. 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.
  3. (c) DKL(q ∥ p)=14log⁡21/41/2+34log⁡23/41/2=−14+34(log⁡23−1)=34log⁡23−1D_{KL}(q \,\Vert\, p) = \tfrac14\log_2\frac{1/4}{1/2} + \tfrac34\log_2\frac{3/4}{1/2} = -\tfrac14 + \tfrac34(\log_2 3 - 1) = \tfrac34\log_2 3 - 1. A common mistake is to give the (b) value, swapping the arguments.
  4. (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 1/2561/256 to every next byte is evaluated on any text. Give its cross-entropy loss in nats per byte. [number]

Answer

8ln⁡2=ln⁡256≈5.5458\ln 2 = \ln 256 \approx 5.545 nats per byte.

The loss on each byte is −ln⁡(1/256)=ln⁡256-\ln(1/256) = \ln 256, whatever the byte is, so the cross-entropy is ln⁡256\ln 256 for any text. A common mistake is 8, which is the same number in bits; another is 1/2561/256, the probability rather than its surprise.

Problem 8. The k3 estimator of KL used in RL fine-tuning is k3(r)=er−r−1k_3(r) = e^r - r - 1, with r=log⁡pref(x)−log⁡p(x)r = \log p_{\text{ref}}(x) - \log p(x) for a sample x∼px \sim p. (a) Give k3(log⁡2)k_3(\log 2). [number] (b) Is k3(r)≥0k_3(r) \ge 0 for every real rr? [bool]

Answer

(a) 1−ln⁡2≈0.3071 - \ln 2 \approx 0.307, (b) true.

  1. (a) k3(ln⁡2)=eln⁡2−ln⁡2−1=2−ln⁡2−1=1−ln⁡2k_3(\ln 2) = e^{\ln 2} - \ln 2 - 1 = 2 - \ln 2 - 1 = 1 - \ln 2. A common mistake is 1+ln⁡21 + \ln 2, a sign slip on rr; another is ln⁡2\ln 2, which is the plain log-ratio estimator, not k3.
  2. (b) er≥1+re^r \ge 1 + r for every real rr, because the exponential is convex and 1+r1 + r is its tangent line at 0. So er−r−1≥0e^r - r - 1 \ge 0, with equality only at r=0r = 0. The plain estimator −r-r is negative whenever pref(x)>p(x)p_{\text{ref}}(x) > p(x), which is why k3 is what GRPO with verifiable rewards logs.

Problem 9. Prove Gibbs’ inequality: DKL(p ∥ q)≥0D_{KL}(p \,\Vert\, q) \ge 0 for distributions p,qp, q on a finite set with qi>0q_i > 0 wherever pi>0p_i > 0, with equality exactly when p=qp = q. [proof]

Answer

Proof. Let S={i:pi>0}S = \{i : p_i > 0\}. Terms outside SS are 0, and by assumption qi>0q_i > 0 on SS, so

−DKL(p ∥ q)=∑i∈Spilog⁡qipi.-D_{KL}(p \,\Vert\, q) = \sum_{i \in S} p_i \log\frac{q_i}{p_i}.

Use log⁡t≤t−1\log t \le t - 1 for t>0t > 0 (the concave log⁡\log lies below its tangent at t=1t = 1), with equality only at t=1t = 1, and t=qi/pit = q_i/p_i:

−DKL(p ∥ q)≤∑i∈Spi(qipi−1)=∑i∈Sqi−∑i∈Spi=∑i∈Sqi−1≤0,-D_{KL}(p \,\Vert\, q) \le \sum_{i \in S} p_i\left(\frac{q_i}{p_i} - 1\right) = \sum_{i \in S} q_i - \sum_{i \in S} p_i = \sum_{i \in S} q_i - 1 \le 0,

since qq‘s mass on SS is at most 1. So DKL(p ∥ q)≥0D_{KL}(p \,\Vert\, q) \ge 0.

Equality needs both inequalities to be tight. The first is tight only if qi/pi=1q_i/p_i = 1 for every i∈Si \in S; the second only if ∑i∈Sqi=1\sum_{i \in S} q_i = 1, so qq has no mass outside SS, where pp is also 0. Together, q=pq = p. Conversely p=qp = q makes every log-ratio 0. ■\blacksquare

(In natural log; in base 2 the same argument goes through, since log⁡2t=ln⁡t/ln⁡2\log_2 t = \ln t / \ln 2 only rescales.) The second equality condition is easy to forget: matching pp on its support is not enough if qq also puts mass elsewhere, because then qi<piq_i < p_i somewhere on SS.

The wrong answers in this set cluster into a few patterns, each tied to the problem where it shows up.

MistakeSymptomHow to avoid it
Reporting nats where bits were asked, or the reversebits-per-byte off by a factor ln⁡2\ln 2fix the base before computing: log⁡2\log_2 for bits, ln⁡\ln for nats (Problems 1, 2, 7)
Mixing a natural log into a bits formulaa value that is neither unitevery log in a bits answer is log⁡2\log_2 (Problem 3(b))
Counting a zero-probability cellentropy too high, or NaN from 0log⁡00 \log 0 in codedrop zero cells by 0log⁡0=00 \log 0 = 0 (Problem 4(a))
Taking H(Y)H(Y) or H(X,Y)H(X, Y) for H(Y∣X)H(Y \mid X)sequence losses that do not add upcompute H(Y∣X)H(Y \mid X) both directly and by the chain rule (Problem 4(c))
Swapping the arguments of KLa penalty that weights the wrong distribution’s errorsweight 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.2subtract H(p)H(p) from H(p,q)H(p, q) (Problem 6(b))
Using the raw log-ratio as a per-sample KLnegative KL values in training logsuse k3=er−r−1k_3 = e^r - r - 1, nonnegative by the tangent-line bound (Problem 8)

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 eHe^{H} and bits per byte from the same NLL sums.

  • 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.
  • Entropy is the expected surprise −log⁡p-\log p; it is log⁡V\log V for a uniform distribution over VV outcomes and less for anything else, so an untrained byte model starts at log⁡256≈5.55\log 256 \approx 5.55 nats (Problems 1, 2, 7).
  • The unit is the base of the logarithm: bits for log⁡2\log_2, nats for ln⁡\ln; mixing them is the most common wrong answer (Problems 1, 3, 7).
  • The chain rule H(X,Y)=H(X)+H(Y∣X)H(X, Y) = H(X) + H(Y \mid X) 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 er−r−1e^r - r - 1 is nonnegative for every sample, unlike the plain log-ratio (Problem 8).