1. Switch
  2. Compute
  3. Deduce
  4. Probability
  5. Information
  6. Vectors
  7. Derivatives
  8. Optimize
  9. Neurons
  10. Generalize
  11. Attention
  12. LLM

Chapter 04 · Information

Measuring surprise

In 1948 Claude Shannon turned information into a measurable quantity. With a single formula, entropy, he explained how far a message can be compressed and how to measure how far a model is from reality. That measure is, literally, the function every LLM minimizes during training.

In July 1948 an engineer at Bell Labs, Claude Shannon, published “A Mathematical Theory of Communication”. On its second page he brushed meaning aside: “these semantic aspects of communication are irrelevant to the engineering problem”. What he cared about was how much information a message contains and how to transmit it efficiently. His answer founded an entire discipline and, seventy years later, gives the best description of what a language model does.

Information is surprise

If someone tells you the sun will rise tomorrow, you learn nothing: you already knew. If they tell you it will snow in Seville tomorrow, you learn a lot. Shannon formalized this intuition: the information an event carries depends only on how improbable it was.

Definition (information or surprise)

The information of an outcome x with probability p(x) is

I(x)=−log2⁡p(x)bits.

A certain event (p=1) carries 0 bits. A fair coin carries −log2⁡12=1 bit. An event with probability 11024 carries 10 bits. The logarithm is there for a reason: we want the information of two independent events to add up, while their joint probability multiplies: −log⁡(pq)=−log⁡p−log⁡q.

Entropy: the average surprise

Before learning the outcome we do not know how much surprise awaits us, but we can compute its average value. That average is the entropy, the central quantity of the whole theory.

Definition (entropy, Shannon 1948)
H(X)=𝔼[I(X)]=−∑xp(x)log2⁡p(x).

It satisfies 0≤H(X)≤log2⁡n if X takes n values. It is 0 only when one outcome is certain and reaches its maximum only when all outcomes are equally likely.

Shannon did not choose the formula for its looks: he proved it is the only reasonable measure of uncertainty.

Theorem (uniqueness, Shannon 1948)

Let H(p1,…,pn) be a function that (1) is continuous in the pi, (2) increases with n when the outcomes are equally likely, and (3) is consistent when a choice is broken down into successive choices. Then

H=−K∑i=1npilog⁡pi

for some constant K>0, which only sets the unit (bits if the logarithm is base 2).

Go back to the example on the home page. For “The cat is sitting on the…”, a distribution spread over mat, sofa and floor has high entropy: there is a lot of uncertainty about the next word. After “Once upon a…”, almost all the probability falls on time and the entropy is almost zero. Drag the bars in the figure to see how it changes.

Drag the bars to change the distribution. The entropy H is the average number of yes/no questions you need to guess the outcome. The table shows the optimal Huffman code for that distribution: frequent symbols get short codes, and the average length L always lies between H and H+1.

To compress is to predict

Entropy has a very concrete operational meaning: it is the limit of lossless compression.

Theorem (source coding, Shannon 1948)

For any prefix-free binary code for the symbols of a source with entropy H(X), the average length L=∑xp(x)ℓ(x) satisfies L≥H(X). Moreover, there is a code with

H(X)≤L<H(X)+1.
Sketch of the proof

The lengths ℓ(x) of a prefix-free code satisfy Kraft's inequality, ∑x2−ℓ(x)≤1, so q(x)=2−ℓ(x) behaves like a distribution. Then L−H=∑xp(x)log2⁡p(x)q(x)≥0 by Gibbs' inequality (below). For the upper bound, choose ℓ(x)=⌈−log2⁡p(x)⌉, which satisfies Kraft and gives L<H+1.

In 1952 David Huffman, then a PhD student at MIT, found a simple algorithm that builds the optimal code: repeatedly merge the two least probable symbols. The consequence is the key to everything that follows: to compress well you must predict well. Whoever knows the distribution can give short codes to what is likely. The converse also holds: a good compressor contains a good model of the data.

Cross-entropy: the price of being wrong

Suppose the data follow the true distribution p and your model believes they follow q. If you design the code with q, each symbol x costs you −log2⁡q(x) bits, and on average you pay the cross-entropy:

H(p,q)=−∑xp(x)log2⁡q(x)=H(p)⏟unavoidable+DKL(p‖q)⏟the model's fault.

The first term is the inherent uncertainty of the data, which no model can remove. The second, the Kullback–Leibler divergence (1951), measures the bits wasted by using q instead of p:

DKL(p‖q)=∑xp(x)log2⁡p(x)q(x).
Theorem (Gibbs' inequality)

For any distributions p and q, DKL(p‖q)≥0, with equality if and only if p=q. Hence H(p,q)≥H(p).

Proof

Use ln⁡y≤y−1, with equality only at y=1. Summing over the x with p(x)>0:

−DKL(p‖q)=∑xp(x)ln⁡q(x)p(x)≤∑xp(x)(q(x)p(x)−1)=∑xq(x)−1≤0.

(Changing the base of the logarithm only multiplies by a positive constant.)

The hollow bars are the true distribution p of the next word. The filled ones are your model q: drag them. The cross-entropy H(p,q) never drops below H(p), and the difference is exactly the KL divergence. Training a language model consists of adjusting q to close that gap.

This is the point of the site where everything clicks into place. If p is the empirical distribution of the training data, minimizing H(p,qθ) with respect to the parameters θ is the same as minimizing the negative log-likelihood from the previous chapter. Maximum likelihood, minimum cross-entropy and maximum compression are the same objective, and that objective is the loss function every LLM is trained with:

ℒ(θ)=−1T∑t=1Tlog⁡qθ(wt∣w<t).

Its exponential is called perplexity, PPL=2ℒ (with ℒ in bits): it is as if the model were hesitating, at each step, between PPL equally likely words.

Shannon's approximations to English

In the same 1948 paper Shannon ran an experiment that seems prophetic today. He generated random text with increasingly informed models: equiprobable letters, letters with their real frequency, letters that depend on the previous one (digrams), on the previous two… and then the same with words. At each level the text looked more like English. It was the first statistical language model, built by hand from frequency tables taken from books.

An n-gram model trained on the first chapter of Alice's Adventures in Wonderland (public domain). The order n is how many symbols the model looks at to predict the next one. At high orders the text sounds like Lewis Carroll, but only because the model copies fragments of the original: with so little data, memorizing is the cheapest way to reduce entropy. This generalization problem is the one neural networks solve.

In 1951 Shannon estimated the entropy of English by asking people to guess texts letter by letter, and obtained between 0.6 and 1.3 bits per letter, far below the log2⁡27≈4.75 bits of equiprobable letters. That redundancy is what a language model exploits. An n-gram model cannot go much further: the number of contexts grows exponentially with n, and almost all of them appear once or never. To generalize to unseen contexts, words must be represented so that “cat” and “feline” end up close together. For that we need vectors and linear algebra.

References

  1. C. E. Shannon (1948). “A Mathematical Theory of Communication”. Bell System Technical Journal, 27(3–4).
  2. S. Kullback and R. A. Leibler (1951). “On Information and Sufficiency”. Annals of Mathematical Statistics, 22(1).
  3. C. E. Shannon (1951). “Prediction and Entropy of Printed English”. Bell System Technical Journal, 30(1).
  4. D. A. Huffman (1952). “A Method for the Construction of Minimum-Redundancy Codes”. Proceedings of the IRE, 40(9).
  5. T. M. Cover and J. A. Thomas (2006). Elements of Information Theory, 2nd ed. Wiley.
  6. G. Delétang et al. (2024). “Language Modeling Is Compression”. ICLR 2024.