All courses

Prediction as compression

Explain that a model with loss ๐“› can code text in about ๐“› nats per token via arithmetic coding. Argue why driving ๐“› down forces the model to capture the structure that generated the text.

Builds on: Loss per token and perplexity

โ–ถ Watch this lesson Start the whole course

What the animation says

Guessing as squeezing

Training rewards just one thing: lower loss, the average surprise at the token that actually comes next. Yet models trained this way pick up grammar, facts, even arithmetic. Why would guessing force understanding? The bridge: every predictor is a compressor. A model with loss L can store text, losslessly, in about L nats per token. And squeezing real text ever smaller only works by capturing whatever produced it.

Shrink the interval

Sender and receiver run the same model. Both start with the interval from zero to one. Step one: the model says A fifty percent, B thirty, C twenty. A occurs, so we keep A's slice, zero to one half. Step two cuts that half into a quarter, a half, a quarter. C occurs, so we keep the last slice. Each kept slice shrinks the width by that token's probability. These lines are the whole encoder. Step three: A ten percent, B eighty, C ten. B occurs. Tap the slice we keep.

Width becomes bits

The kept width is a half, times a quarter, times four fifths: one tenth. Naming a point inside it takes minus log base two of the width: about 3.3 bits. A four-bit binary number, 0.4375, lands inside. The receiver replays the model and reads back A, C, B. That log is just the sum of each token's surprisal. So code length equals total loss: tokens times loss per token, in nats. Divide nats by 0.693 to get bits. A hundred tokens at one nat each: how many bits?

The floor no model beats

About 144 bits. A bit is a smaller unit than a nat, so the count goes up. Now, can a better model push loss all the way to zero? No. Real text has genuine randomness: an entropy, H, that no model can beat. Expected loss splits in two: that floor H, plus a mismatch term, the KL between the true source and the model. Training can only shrink the mismatch. So lower loss means the model must match how the text is really produced.

17 ร— 23 in the corpus

The text contains: seventeen times twenty-three equals three ninety-one. A model that only knows the answer is some three-digit number spreads its bet over 900 options. It pays about 6.8 nats. A model that actually multiplies puts ninety percent on 391. It pays about 0.11 nats. That's 6.7 nats saved on every such line. But the corpus holds thousands of different products, many never seen in training. What's the cheapest way to collect those savings?

Loss is code length

Compute the product. One compact rule collects the savings on every line, even ones never seen. That's the whole story. Loss is code length: tokens times loss gives nats, and dividing by 0.693 gives bits. Genuine randomness sets a floor no model goes below. Every nat saved above it comes from capturing a real regularity: grammar, facts, arithmetic. Guessing well, it turns out, means understanding what wrote the text.

Why would next-token guessing teach anything?

Training rewards only one thing: lower $\mathcal{L}$, the average surprisal of the token that actually comes next. Yet models trained this way pick up grammar, facts and reasoning. Why should *guessing* force *understanding*? The bridge: **every predictor is a compressor**. A model with loss $\mathcal{L}$ can store text losslessly in about $\mathcal{L}$ nats per token. Squeezing real text ever smaller is only possible by capturing whatever produced it.

Arithmetic coding: shrink an interval

Sender and receiver run the **same model**. Start with the interval $[0,1)$. At each position, cut the current interval into slices sized by the model's probabilities for every possible next token, then keep the slice of the token that actually occurred. After $T$ tokens the width is the product of those probabilities. Sending any number inside takes about $-\log_2(\text{width})$ bits. The receiver replays the model, checks which slice the number falls in, and recovers every token exactly.

Code length equals total loss

$$\text{length} \approx \sum_{t=1}^{T} -\ln p_\theta(x_t\mid x_{<t}) = T\,\mathcal{L}\ \text{nats}$$ plus at most ~2 bits for the whole message. Divide nats by $\ln 2 \approx 0.693$ to get bits. If text comes from a true source $P$, the expected loss splits as $\mathcal{L} = H(P) + \mathrm{KL}(P\,\|\,p_\theta)$. Here $H(P)$ is the source's average per-token entropy given the preceding text. It is a floor that no model can beat, set by genuine randomness. Lowering $\mathcal{L}$ can only shrink the KL, which means $p_\theta$ must come to match how the text is really generated.

Structure is the only way to save nats

The text contains `17 ร— 23 = 391`. Suppose the model knows only that the answer is some 3-digit number, so it spreads probability uniformly over 900 options. It pays $\ln 900 \approx 6.80$ nats. A model that actually multiplies gives the answer `391` a total probability of $0.9$ and pays $-\ln 0.9 \approx 0.11$ nats. That saves about 6.7 nats on every such line. Suppose the corpus holds thousands of different products, including ones never seen in training. Memorizing individual lines cannot cover them all. The compact way to collect those savings is to compute the product.

Encode 3 tokens by hand

Each step lists the model's probabilities over a 3-token vocabulary and the token that actually occurred. The final width is $0.5\times0.25\times0.8=0.1$. That gives $2.30$ nats, or $3.32$ bits. The interval is $[0.3875, 0.4875)$, and the binary number $0.0111_2 = 0.4375$ (4 bits) falls inside it.
import numpy as np
dists  = [[0.5, 0.3, 0.2], [0.25, 0.5, 0.25], [0.1, 0.8, 0.1]]
actual = [0, 2, 1]               # token that really occurred each step
low, width = 0.0, 1.0
for p, x in zip(dists, actual):
    low += width * sum(p[:x])    # skip slices of tokens before x
    width *= p[x]                # keep the slice of token x
print(low, low + width)          # [0.3875, 0.4875)
print(-np.log(width), -np.log2(width))  # 2.30 nats, 3.32 bits

Loss is code length

With arithmetic coding, a model with loss $\mathcal{L}$ stores text losslessly in about $T\mathcal{L}$ nats, which is $T\mathcal{L}/\ln 2$ bits. True randomness sets a floor $H$ that no model can go below. Every nat saved above that floor comes from capturing a real regularity in how the text was produced: grammar, facts, arithmetic.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.