All courses

Surprisal and entropy

Compute surprisal −log p and entropy as expected surprisal, in nats or bits. Explain entropy as the irreducible uncertainty of a distribution.

Builds on: What an LLM computes, end to end

▶ Watch this lesson Start the whole course

What the animation says

Surprise as a number

At every position, the model gives a probability to every token in its vocabulary. Then the real next token shows up. If the model gave it ninety percent, it should barely be surprised. If it gave one in a thousand, very surprised. We measure that surprise as minus the log of the probability. With log base two, the unit is bits. Ninety percent costs about 0.15 bits. One in a thousand costs about ten bits. So, if a token's probability is cut in half, what happens to its surprisal?

Surprise adds up

Halving the probability adds exactly one bit. One half costs one bit, one quarter costs two. That makes surprisal add. Take a three-token text. Each token's probability is given the tokens before it: one half, one quarter, one half. The text's probability is their product: one sixteenth. In surprisal, the product becomes a sum: one plus two plus one, four bits. And minus log two of one sixteenth is exactly four bits.

Entropy: expected surprise

Now a different question, asked before any token is revealed: how uncertain is the model? Here is a distribution over four tokens: one half, one quarter, one eighth, one eighth. Their surprisals are one, two, three, and three bits. Entropy is the surprisal you expect on average, if tokens really are drawn from this distribution. So each surprisal counts in proportion to how often it happens. What is the entropy of this distribution?

The weighted sum and its range

Weight each surprisal by its probability. One half times one, a quarter times two, an eighth times three, twice. That's 0.5 plus 0.5 plus 0.375 plus 0.375: 1.75 bits. If all four tokens were equally likely, each would cost two bits. Entropy is then two bits, the most four tokens allow. If one token were certain, entropy would be zero. So entropy runs from zero, for a certain outcome, up to log of V when all V tokens are equally likely. Tap the distribution with the highest entropy.

Nats, numpy, and the floor

Use the natural log instead, and the unit is nats. One bit is about 0.693 nats. In numpy, this line gives each token's surprisal in nats. This line weights each one by its probability and sums: 1.213 nats. Divide by log of two, and it's 1.75 bits again. For a vocabulary of fifty thousand tokens, the maximum is about 10.82 nats. And when tokens truly follow the distribution, entropy is a floor. No prediction from the same context averages less surprise.

How surprising was that token?

At every position the model outputs a probability for each of the $V$ vocabulary tokens. Then the real next token shows up. It might have been given 0.9, or 0.001. How do we turn "how surprised should the model be" into a number that adds up sensibly over a whole text? And how do we measure, in one number, how *uncertain* a distribution is before anything is revealed?

Rare outcomes carry more information

Seeing a likely token tells you little; seeing an unlikely one tells you a lot. Taking $-\log p$ captures this, and it makes surprise **add**. A text's probability is the product of each token's probability given the tokens before it. The log turns that product into a sum of surprisals. **Entropy** is the surprisal you expect on average when outcomes really come from the distribution. It is uncertainty that cannot be removed. Even a predictor that knows the true odds exactly is still surprised this much on average.

Surprisal and entropy, precisely

Surprisal of an outcome with probability $p(x)$: $$s(x) = -\log p(x)$$ Entropy is its expected value over $p$: $$H(p) = \sum_x p(x)\,\big(-\log p(x)\big)$$ With natural log the unit is **nats**. With $\log_2$ it is **bits**. $1$ bit $= \ln 2 \approx 0.693$ nats. Terms with $p(x)=0$ count as $0$. Range: $H=0$ for a certain outcome, up to $\log V$ when all $V$ tokens are equally likely.

Entropy of a 4-token distribution

$p = (0.5, 0.25, 0.125, 0.125)$. Surprisals in bits: $-\log_2 0.5 = 1$, $-\log_2 0.25 = 2$, and $3$, $3$. $H = 0.5\cdot1 + 0.25\cdot2 + 0.125\cdot3 + 0.125\cdot3$ $= 0.5+0.5+0.375+0.375 = 1.75$ bits. In nats: $1.75 \times 0.693 = 1.213$. A uniform spread over 4 tokens gives $\log_2 4 = 2$ bits, which is the maximum for 4 tokens.

Same computation in numpy

Compute surprisal per token, then weight each by its own probability and sum. Divide nats by $\ln 2$ to get bits.
import numpy as np
p = np.array([0.5, 0.25, 0.125, 0.125])
s_nats = -np.log(p)               # surprisal per token
H_nats = np.sum(p * s_nats)       # expected surprisal
H_bits = H_nats / np.log(2)
print(s_nats.round(3))            # [0.693 1.386 2.079 2.079]
print(H_nats.round(3), H_bits.round(3))  # 1.213 1.75
print(np.log(50000).round(2))     # max H for V=50,000: 10.82 nats

What to remember

Surprisal $-\log p$ measures how unexpected one outcome was, and it adds across a sequence. Entropy $\sum p(-\log p)$ is the average surprisal under the distribution itself. When outcomes truly follow $p$, it is the floor: no prediction made from the same context can average less surprisal. It ranges from $0$ (certain) to $\log V$ (all $V$ tokens equally likely). Use natural log for nats and $\log_2$ for bits.

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.