Better Predictor, Better Compressor
Every compressor in this book secretly contained a model of its data: letter counts, a code tree, a memory of what came before. This last chapter makes the link exact. The cost of a message is the surprise of a model, a wrong model pays a measurable tax, and training a language model is training a compressor.
Think of a weather forecaster who is asked to bet on tomorrow, every day, for a year. A forecaster who says "70% chance of rain" and sees rain should be rewarded more than one who said "10%". A natural score is exactly the surprise from Chapter 1: if your forecast gave the actual weather probability q, you pay log₂(1/q) points. Confident and right costs almost nothing; confident and wrong costs a lot; and saying "0%" for something that happens costs an infinite amount.
That score is not just a nice way to grade forecasters. It is literally the number of bits a compressor would spend on the actual outcome if it built its code from the forecast. Chapter 4 showed that the ideal codeword length for an outcome is its surprise. Here the probabilities come from a model, and the model may be wrong.
The price of a wrong model
Suppose the world follows probabilities p, but your code is built from a model q. On average you pay the surprise under q, averaged over what actually happens according to p. That average is called the cross-entropy. When q matches p exactly, it equals the entropy; any mismatch costs extra bits. Drag the model's bars below and watch the bill.
A model that predicts better pays fewer bits; the gap between its average and the entropy measures exactly how wrong it is.
Predict, then pay
Now apply this to text. In Chapter 3 you climbed a ladder of letter models. Each one makes a prediction before every character, a probability for each of the 27 symbols, and then pays the surprise of the character that actually comes. Type anything below and see where each model pays its bits.
Can a real compressor actually achieve those totals? Almost exactly. Arithmetic coding turns any sequence of predictions into a single binary number whose length is the total surprise plus at most a couple of bits for the whole message, not per symbol. It works by narrowing an interval: start with the range from 0 to 1, and for each symbol zoom into the slice the model assigned to it. Watch it happen.
Any predictor can drive an arithmetic coder, so a better predictor is a better compressor; and the bits a compressor saves are a measure of how much structure it has found.
The bridge to language models
A large language model is, at its core, a next-token predictor: given the text so far, it outputs a probability for every possible next token. During pretraining it is shown enormous amounts of text and adjusted to lower its average surprise at the token that actually came next. That training objective is the cross-entropy you met above, usually computed with natural logarithms and reported in nats rather than bits (divide by ln 2 ≈ 0.693 to convert nats to bits).
So lowering a language model's training loss is, quite literally, making it a better compressor of text like its training data. Pair such a model with an arithmetic coder and you get a working text compressor; when the size of the model itself is not counted, strong models compress text far better than zip-style tools. A related number you may meet is perplexity, 2 raised to the cross-entropy in bits: a perplexity of 20 means the model is, on average, as uncertain as if it were choosing evenly among 20 tokens.
Does compressing text well mean understanding it? Information theory does not answer that. Shannon deliberately set meaning aside, and every number in this book measures predictability, not meaning. What the theory does say is that you cannot predict well, and so cannot compress well, without capturing the patterns that are really there.
A forecasting model says snow has probability 0. Then it snows. What happens to the model's cost (its cross-entropy)?
On the same unseen text, model A averages 1.2 bits per character and model B averages 1.5. Which statement is right?
Connections
- Large Language Models, Chapter 1: The Next-Word Game: the predictor whose surprise this chapter measures.
- Large Language Models, Chapter 8: Pretraining & Error: how the cross-entropy loss is pushed down during training.
- Cybernetics, Chapter 7: Black Boxes & Internal Models: a good regulator, like a good compressor, must contain a model of its world.
- Chapter 3 built the letter models used here.
What you now see
- Coding with a model q costs log₂(1/q) bits per outcome; averaged over reality p, that is the cross-entropy.
- Cross-entropy is never below the entropy; the gap (the Kullback–Leibler divergence) is the tax for a wrong model, and assigning zero probability to a possible event makes it infinite.
- Arithmetic coding turns any predictor into a compressor whose output is within a couple of bits of the total surprise.
- Training a language model to lower its cross-entropy loss is training a better compressor of text.
- From the first yes-or-no question to the last, information measures surprise: how much a message changes what you expected. Meaning is a different question.
You started by halving a pile of possibilities and ended by grading a language model. Along the way: surprise and the bit, entropy as average surprise, codes that need no commas, Huffman's tree, the floor no compressor can break, look-back compression, noise, parity, Hamming's circles and Shannon's capacity. Every one of those ideas is still at work in the device you are reading this on.