Entropy, in a nutshell
When we talk about “entropy” in the context of large-language models (LLMs), we’re borrowing a concept from information theory (Claude Shannon, 1948). In everyday language it simply measures uncertainty – how spread-out or “flat” a probability distribution is.
Mathematically
H(p) = - Σᵢ pᵢ · log₂ pᵢ (bits)
where
• pᵢ = probability the model assigns to token i
• the log can be in any base (base-2 ⇒ bits; base-e ⇒ nats).
Why LLMs care about entropy
| Where it shows up | What entropy tells us | Why it’s useful |
|---|
| Training loss (cross-entropy) | How wrong the model’s predicted distribution is vs. the correct one | Lower cross-entropy ⇒ the model is better. |
| Perplexity | 2^{H} (or e^{H} if using natural logs) | Popular headline metric: “How surprised is the model on average?” |
| Decoding / sampling | Instantaneous entropy of the next-token distribution | High entropy ⇒ model is unsure ⇒ more diverse completions. |
| Uncertainty / confidence scores | Same “how spiky vs. flat” idea | Useful for filtering, fallback routing, or active learning. |
| Temperature scaling | A knob to artificially raise or lower entropy | Temp↑ → entropy↑ (creative), Temp↓ → entropy↓ (focused). |
A quick concrete example
Suppose after the prompt:
“The capital of France is …”
the model assigns these probabilities:
| Token | Prob pᵢ |
|---|
| “Paris” | 0.92 |
| “Lyon” | 0.03 |
| “Marseille” | 0.02 |
| “Berlin” | 0.01 |
| “other” | 0.02 |
Entropy = −Σ pᵢ log₂ pᵢ ≈ 0.55 bits (very low).
The distribution is peaky ⇒ the model is confident.
Now feed it a more open-ended cue:
“My favorite fruit is …”
| Token | Prob pᵢ |
|---|
| “apple” | 0.20 |
| “banana” | 0.18 |
| “mango” | 0.15 |
| “strawberry” | 0.12 |
| “peach” | 0.10 |
| others | 0.25 |
Entropy here ≈ 2.5 bits (much higher).
The model is unsure; many words plausibly follow. When you sample, you’ll see variety.
Relationship to cross-entropy & K-L divergence (30-second view)
-
Cross-entropy H(p, q) = - Σ pᵢ log qᵢ
• p = true distribution (one-hot for “gold” token)
• q = model’s predicted distribution
• Used directly as training loss.
-
Kullback–Leibler divergence KL(p || q) = H(p, q) − H(p)
• Measures the extra entropy (inefficiency) when you use q to encode data that actually follows p.
So while “entropy” alone is about the model’s own uncertainty, cross-entropy/KL tell us how well the model matches the ground truth.
Key takeaways
• Entropy quantifies uncertainty in the next-token probabilities.
• Low entropy ⇒ confident / deterministic; high entropy ⇒ ambiguous / diverse.
• During training it shows up as cross-entropy loss; at inference it drives metrics (perplexity) and controls sampling behavior (temperature, top-p, top-k).
• Knowing the entropy lets engineers calibrate creativity vs. reliability and helps researchers measure progress.
That’s it! When you hear “entropy” around LLMs, just think: “How sure is the model about what comes next?” 🔑