Information Theory
Entropy, KL divergence, and cross-entropy — the currency behind every learning objective. Taught from zero, starting with what it means to be surprised, and built up to the exact loss function that trains every classifier and every language model.
Start here — what this is really about
Information theory has an intimidating name and exactly one idea underneath it:
Learning something is being surprised. The more surprised you are, the more you just learned.
That's it. Everything on this page — entropy, cross-entropy, KL divergence, perplexity — is that sentence turned into arithmetic. And the payoff is enormous, because the number a classifier minimizes, the number a language model reports, and the number that keeps a fine-tuned model from drifting are all the same quantity wearing three different hats.
If your friend texts "the sun rose this morning," you learned nothing — you already knew. If they text "it snowed in Dubai," you learned a lot. Same number of words, wildly different amounts of information. Information theory's whole contribution is a formula that turns that gut feeling into a number you can compute, add up, and minimize with gradient descent.
It teaches from first principles — no symbols until you already understand the idea they stand for. Flip the Depth switch at the top for the formal definitions, derivations, and edge cases; they open automatically once you've finished the prerequisite (Probability & Statistics). Nothing is hidden for good.
Surprise — how startled should you be?
Start with a single event. Something happened, and you knew in advance how likely it was. How surprising was it?
Two demands pin the answer down, and both are common sense:
- Something you were sure would happen (probability 1) should carry zero surprise.
- Something unlikely should carry a lot of surprise — and the less likely it was, the more.
So surprise must be a decreasing function of probability that hits zero at probability 1. The function that does this — and does it for one very good reason we'll get to — is the negative logarithm of the probability.
is the atom of this whole subject. Everything else is an average of it.
Measuring surprise in bits means: how many yes-or-no questions would I have needed to pin this outcome down? A fair coin landing heads is 1 bit — one question ("was it heads?"). One roll of a fair 8-sided die is 3 bits, because three halvings narrow 8 possibilities to 1. A one-in-a-million event is about 20 bits, because you'd need twenty halvings. The unit is not decoration — it is literally a count of questions.
Compute the surprise of three outcomes. The recipe: flip the probability upside down, then count how many halvings it takes to get there.
- A fair coin lands heads. Probability . One halving gets you from 2 possibilities to 1, so the surprise is 1 bit.
- A fair 8-sided die shows a 5. Probability . Halve 8 to 4, 4 to 2, 2 to 1 — three halvings, so 3 bits.
- A biased coin that comes up heads 90% of the time lands heads. Probability . This is barely news at all, and the arithmetic agrees: it takes only about 0.15 bits. Note it is not zero — you did learn something, just very little.
Now the flip side of case 3: when that same 90/10 coin lands tails (probability ), the surprise is about 3.3 bits — twenty times more than the heads case. Rare outcomes carry the information.
Two events have probabilities 0.5 and 0.25. Which is more surprising, and by how many bits?
Hint: Count halvings.
Entropy — the average surprise of a whole distribution
Surprise describes one outcome. But usually you don't care about a single outcome; you care about a source that keeps producing them. How surprising is it on average?
That average is the , and it is the single most important quantity in this lesson.
A coin that always lands heads has entropy 0 — you never learn anything, because you already knew. A fair coin has entropy 1 bit — the maximum possible for two outcomes. A fair 8-sided die has entropy 3 bits. Entropy answers: how many yes-or-no questions, on average, does it take to pin down what happened? More unpredictable source, more questions, higher entropy.
Entropy is the honest minimum size of the suitcase. A source that is wildly unpredictable (fair dice, random noise) has high entropy and genuinely needs a big suitcase — you cannot compress it. A source that is predictable (English text, a photo of a blue sky) has low entropy, and everything you thought was bulk turns out to be air. Compression algorithms are professional suitcase packers, and entropy is the wall they cannot pack past.
The recipe: for each outcome, multiply its probability by its surprise, then add up. It's a weighted average where the weights are the probabilities themselves.
1. A fair coin. Two outcomes, each with probability , each carrying 1 bit of surprise.
2. A coin that lands heads 90% of the time. Heads carries 0.152 bits, tails carries 3.322 bits, but tails almost never happens:
Less than half the fair coin's entropy. The rare outcome is very informative, but it's rare, so it barely contributes to the average.
3. A coin that always lands heads. One outcome with probability 1, carrying 0 bits. . A source that never surprises you carries no information at all.
The pattern: entropy is highest when you have no idea what's coming, and drops to zero as certainty grows.
A weather forecaster in a desert says sunny with probability 0.99 every day. Is their forecast high-entropy or low-entropy, and what does that mean practically?
Hint: Look at the far right of the curve above.
A model's training loss stops improving at 1.9 nats. Which explanation is consistent with information theory?
Cross-entropy — paying for the wrong codebook
Here is where information theory stops being a curiosity and becomes the thing you type into your training script.
So far, both the surprise and the averaging came from the same distribution — the true one. But a model doesn't know . It has its own guess, call it . So ask the question that matters:
Reality generates outcomes according to . My model believes . How surprised is my model, on average, by reality?
That number is the , and it is the loss function you already use.
You've packed a phrasebook optimised for Paris — short, handy entries for the phrases Parisians actually use. Then you land in Tokyo. Every sentence you need is buried at the back under some six-page appendix, while the front of the book is full of French you'll never say. You can still communicate, but every single exchange costs you extra flipping. Cross-entropy is the average number of pages you flip: it depends on the book you brought () and the city you're actually in (). Bring the right book and the cost drops to the city's own entropy — the irreducible minimum. It can never drop below it.
Cross-entropy has a and a in it, and they are not interchangeable. decides which outcomes come up. decides how much each one costs you. Reality picks the questions; your model pays the price. Getting these two straight is 90% of understanding every formula on the rest of this page.
A 4-class classifier sees one image. The true label is dog, which as a probability distribution is the one-hot vector — 100% dog, 0% everything else.
Model B predicts . Compute the cross-entropy: for each class, multiply the true probability by the model's surprise, and add.
- cat:
- dog: bits
- fox:
- owl:
Total: 2 bits. Notice that three of the four terms vanished. Because the truth is one-hot, every term except the correct class is multiplied by zero.
So for classification with a single correct answer, the entire cross-entropy collapses to one thing:
the surprise the model assigned to the right answer, and nothing else.
That is why CrossEntropyLoss in PyTorch takes an integer label instead of a full distribution — the other entries would all be multiplied by zero anyway. Running the same arithmetic on Model A (0.70 → 0.51 bits) and Model C (0.05 → 4.32 bits) reproduces the three numbers in the chart above.
Your classifier assigns 0.99 probability to the correct class on example 1, and 0.01 to the correct class on example 2. Which example dominates the batch loss?
Hint: Look at the far left of the very first plot on this page.
Why does PyTorch's CrossEntropyLoss accept an integer class index rather than a full probability distribution over classes?
KL divergence — the extra you pay for being wrong
Cross-entropy mixes together two very different things: how uncertain the world genuinely is (which you can't fix) and how wrong your model is (which you can). Separating them gives the last big idea.
Even a perfect model has to pay the entropy — that's the world's own randomness, and no model can beat it. Anything you pay above that is your fault. That excess is the , and it is the honest measure of how far your model is from the truth.
The true entropy is the length of the shortest possible route to work — a fact about the map, not about you. Cross-entropy is how far you actually drove. KL divergence is the detour: the extra distance you covered by following a bad set of directions. It's zero only if your directions were the optimal route, and it can never be negative, because you cannot drive less far than the shortest path.
A weather source produces sun, cloud, and rain with true probabilities . Your model predicts .
- The world's own uncertainty (entropy). Weight each outcome's own surprise by its probability: bits. No forecaster on earth beats this.
- What your model actually pays (cross-entropy). Weight the model's surprise by the true probabilities: bits.
- The overcharge (KL). Subtract: bits.
So of the 1.280 bits your model is billed per forecast, 1.157 was unavoidable and only 0.123 is your fault. That 0.123 is the only part training can remove — and it is exactly what you would compute directly from the KL formula.
The lesson generalises: cross-entropy = entropy + KL divergence. Since doesn't depend on your parameters at all, minimising cross-entropy and minimising KL are literally the same optimisation problem.
Minimising cross-entropy and minimising KL divergence give identical gradients. Why?
Hint: Write cross-entropy as entropy plus KL and ask which term depends on the model.
A colleague reports a KL divergence of -0.03 between two distributions. What should you conclude?
Perplexity — entropy in the units people actually quote
Open any language-model paper and you'll find perplexity, not entropy. It's the same number wearing friendlier clothes.
A model with a perplexity of 20 is, on average, as confused as if it were picking uniformly at random from 20 equally-likely options at every step. Perplexity 2 means it's essentially down to a coin flip. Perplexity 50,000 on a 50,000-token vocabulary means it has learned nothing at all. Lower is better, and the scale is intuitive in a way that "1.9 nats" simply isn't.
Your language model reports a cross-entropy loss of 2 bits per token.
- Undo the logarithm: perplexity .
- Read it: the model is as uncertain as someone guessing among 4 equally-likely tokens at every position.
Now suppose you improve the model and the loss drops to 1.5 bits. Perplexity becomes . Note what happened: half a bit of loss cut the effective choices from 4 to under 3. Because perplexity is exponential, small improvements in loss look large in perplexity — which is exactly why the metric is popular, and exactly why you should read it carefully.
Framework caution: PyTorch reports loss in nats, so perplexity is math.exp(loss), not 2 ** loss. Mixing the two is one of the most common reproducibility bugs in language-model reporting.
Mutual information — how much does knowing one thing tell you about another?
One last quantity, because it appears everywhere from feature selection to modern self-supervised learning.
You're uncertain about tomorrow's weather. Then someone tells you today's barometer reading, and you're less uncertain. The amount your uncertainty dropped is the mutual information between barometer and weather. If the reading told you nothing, the drop is zero — and zero mutual information is exactly what independence means.
Twenty questions: you need 20 bits to identify the answer. A friend whispers a hint that cuts your remaining questions to 12. That hint carried 8 bits of mutual information with the answer. A useless hint ("it exists") carries 0 bits; a hint that gives it away outright carries all 20.
Where this shows up in ML
Every one of these is something from above wearing a different hat:
CrossEntropyLoss/NLLLoss— cross-entropy against a one-hot label. Reported in nats.- Every language-model training run — cross-entropy against the token that actually came next, averaged over positions; quoted as perplexity in the paper.
- Label smoothing — replaces the one-hot target with a slightly softened distribution, so the loss stops pushing the model toward infinite confidence. Read it as: deliberately raise the target's entropy.
- Knowledge distillation — trains a small student to match a large teacher's whole output distribution by minimising the KL between them, not just to match the hard label. The teacher's soft probabilities carry more information than a one-hot target does.
- VAEs — the loss has a KL term pulling the learned latent distribution toward a standard Gaussian prior, in exactly the units on this page.
- RLHF and DPO — the policy is penalised by its KL divergence from the original SFT model, which is what stops it drifting into gibberish while chasing reward.
- Decision trees — split on the feature with the highest information gain, which is the mutual information between the feature and the label.
- Contrastive self-supervised learning — InfoNCE maximises a lower bound on mutual information between two augmented views.
- Compression — a good language model is a good compressor, at bits per token, by the source coding theorem.
Explain to a friend, without formulas, the difference between entropy, cross-entropy, and KL divergence — using the commute or phrasebook picture. Then explain why a training loss that stops at 1.9 does not mean the model is bad. If either explanation stalls, that is exactly the section to reread.
- Surprise is : how startled you should be by an outcome. Measured in bits (base-2, counts yes-or-no questions) or nats (natural log, what every framework uses). It has to be a logarithm, because information from independent events must add while their probabilities multiply.
- Entropy is the average surprise of a distribution — how unpredictable a source is. It is maximised by the uniform distribution (), zero for a certain outcome, and it is the hard floor on lossless compression.
- Cross-entropy is how surprised your model is by reality . With a one-hot label it collapses to , which is the negative log-likelihood — so maximum likelihood and cross-entropy minimisation are the same thing.
- KL divergence is the extra surprise caused by your model being wrong. It is never negative, zero only when , and not symmetric — forward KL is mass-covering, reverse KL is mode-seeking.
- The identity that ties it together: . Since is a constant of the data, minimising cross-entropy is minimising KL — and a loss that plateaus may just be sitting on the data's own entropy.
- Perplexity is of the cross-entropy: the effective number of equally-likely choices. Same information as the loss, only comparable across models sharing a tokenizer.
- Mutual information is how much your uncertainty about one variable drops once you know another — zero exactly when they're independent.
Practice — and how to make it stick
Three research-backed habits, built into this platform:
• Retrieval practice: attempt the problems below before rereading — pulling an answer from memory beats recognising it on the page.
• Spaced repetition: mark this topic complete and it joins your Review queue, resurfacing right before you'd forget it.
• Interleaving: mix these with problems from Probability & Statistics and Optimization rather than grinding one type — messier practice, sturdier memory.
- By hand: compute the entropy of a fair 4-sided die, then of a loaded one with probabilities . How many bits of uncertainty did the loading remove, and how does that compare to the 2-bit ceiling?
- By hand: a binary classifier predicts 0.6 for the positive class. Compute the loss in bits if the true label is positive, then if it is negative. Which mistake is more expensive, and why does that asymmetry disappear when the prediction is 0.5?
- By hand: verify for and , then swap and and confirm the KL changed but the inequality still holds.
- From scratch: implement Huffman coding, compress a paragraph of English, and compare the achieved bits-per-character against the entropy you measure from the character frequencies. You should land within 1 bit of the entropy — the source coding theorem's promise, verified by your own code.
- Prove it numerically: sample two independent variables and estimate their mutual information; confirm it hovers near zero. Then make one a noisy copy of the other and watch it climb.
- Read like a scientist: find a language-model paper reporting perplexity. Check which tokenizer and which dataset it used, and decide whether the number is comparable to another paper's. Most of the time it isn't — and the paper won't say so.
Try it right here — edit and run the code, and if you get stuck or hit an error, ask Ada on the right: she can see your code and terminal output.
Next: see this loss doing its job at scale in Language Modeling, or go back and strengthen the foundation in Probability & Statistics.