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
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.
Rare outcomes carry more information
Surprisal and entropy, precisely
Entropy of a 4-token distribution
Same computation in numpy
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
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