- Key idea
- Surprise, and entropy as average surprise
- Real world
- The weather in a desert, sunny 99% of days, tells you almost nothing new each morning. The weather in a city where rain and sun are equally likely keeps you guessing. The second has higher entropy.
- In an LLM
- After "The capital of France is", the next token is nearly certain: low entropy. After "My favourite food is", many tokens are plausible: high entropy.
Surprise
How surprised should you be by an outcome? The rarer it is, the more surprising. A natural measure:
| Probability | Surprise (bits) |
|---|---|
| 1 (certain) | 0 |
| 0.5 (coin flip) | 1 |
| 0.25 | 2 |
| 0.01 | 6.6 |
A bit is the surprise of one fair coin flip: the answer to one yes/no question with even odds.
Entropy is average surprise
Entropy is the surprise of each outcome, weighted by how often it happens:
Rare outcomes are very surprising, but they rarely happen, so they contribute little. Entropy is high only when many outcomes are reasonably likely.
Try it
In the chart, each outcome is a column as wide as its probability and as tall as its surprise, so its area is probability × surprise and the total area is the entropy. A rare outcome is a tall, thin sliver: very surprising, but it adds little.
- Fair coin: bit.
- Desert, 99% sun: bits.
A rainy day in the desert is very surprising (6.6 bits), but it happens 1% of the time, so the average is tiny.
- "The capital of France is":
Paris90%, two others 5% each. Entropy bits. - "My favourite food is": ten foods, 10% each. Entropy bits.
A model sampling from the second distribution produces varied text; from the first, nearly always the same token.
Two facts worth remembering
- Entropy is 0 when one outcome is certain.
- For outcomes it is largest when all are equally likely, at bits. Four outcomes: 2 bits. 100,000 tokens: about 16.6 bits.
ML code usually uses natural logs, which gives entropy in nats instead of bits. 1 bit = 0.693 nats; the idea is identical.
Where this shows up
- RL tends to drive a model's entropy toward zero, which stops it exploring: entropy collapse.
- Temperature raises or lowers entropy at sampling time: softmax.
- The next two pages build directly on it: cross-entropy, KL divergence.
Definition
With the unit is bits; with , nats. Terms with count as 0. For a language model, entropy is computed per token from the next-token distribution, then averaged over the positions in an answer.