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
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.
Arithmetic coding: shrink an interval
Code length equals total loss
Structure is the only way to save nats
Encode 3 tokens by hand
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
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.
Bytewise
All courses