Mathematics for AI / Calculus & Probability
Surprise, entropy, and the loss LLMs minimize.
Reviewed by Yuvaraj
Every time a model makes a prediction, it is implicitly answering one question: "How surprised should I be by what actually happened?" Information theory turns that intuition into arithmetic. It gives us a single unit, the bit, for measuring surprise, uncertainty, and the cost of a wrong belief, and from it falls out the exact loss function that trains almost every classifier you will build. This lesson connects four quantities developers use constantly, usually without naming them: self-information, entropy, cross-entropy, and KL divergence.
The self-information (or "surprisal") of an outcome with probability is
Rare events carry more information; a certain event carries none. Using measures surprise in , and the shape of explains the unit: halving a probability adds exactly one bit of surprise.
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
| Probability | Surprise (bits) |
|---|---|
| 1.00 | 0.00 |
| 0.50 | 1.00 |
| 0.25 | 2.00 |
| 0.10 | 3.32 |
| 0.01 | 6.64 |
Entropy is the average self-information of a random variable, how uncertain you are before you see the outcome:
It is always non-negative and is maximal when every outcome is equally likely.
Fair coin with per side:
One yes/no question fully resolves the outcome.
Biased coin with :
The bias makes the coin more predictable, so it carries less than half the uncertainty of a fair one.
Cross-entropy measures the average surprise you experience when the world follows distribution but you believe :
This is exactly the classification loss. For a single labelled example the true distribution is one-hot, all mass on the correct class, so the sum collapses to : the surprise your model assigned to the right answer. Minimizing it drives .
Worked loss. Three classes, true label is class 1, model predicts :
A confident, correct prediction gives , far lower. A confident but wrong prediction is punished savagely, because as .
KL divergence is the extra surprise you pay for using instead of the true :
The three quantities lock together in one identity:
Entropy is the irreducible uncertainty of the data; KL is the penalty for a bad model. Because does not depend on your parameters, minimizing cross-entropy is identical to minimizing KL divergence. And since one-hot labels have , in standard classification cross-entropy and KL are numerically equal.
Entropy is a compression bound
Entropy is the minimum average number of bits needed to encode outcomes drawn from . Cross-entropy is what you actually spend when you build your code around a wrong distribution , and KL divergence is the wasted overhead. "A good model" and "an efficient code" are the same idea.
Common mistakes
KL divergence is not symmetric: in general, so it is not a distance metric, the order of the arguments changes the answer and the behavior. Watch the log base: bits use , but ML frameworks (PyTorch, TensorFlow) use the natural log, so their reported loss is in nats, not bits. Finally, cross-entropy needs to be a valid probability distribution, apply softmax first; feeding raw logits gives garbage. Use the convention for zero-probability terms.