Information
Last updated
How surprised should you be when something happens? Information theory turns that question into a number, and its answers run all through machine learning. Entropy measures how unpredictable something is; cross-entropy measures how badly a model’s probabilities fit the truth; KL divergence measures the gap between two distributions. Nearly every classifier in this course is trained by making its cross-entropy small, and this lesson shows why.
By the end of this lesson you can
- Work out the surprise of an outcome, in bits and in nats
- Compute the entropy of a distribution and say when it’s highest
- Compute cross-entropy and KL divergence, and explain why classifiers minimise cross-entropy
- Clip probabilities so a log of 0 can’t turn a loss into nan
Surprise: −log p
Something certain to happen tells you nothing when it does. Something rare tells you a lot. The surprise (or self-information) of an outcome with probability puts a number on that:
In words: one over the probability, then the log. Probability 1 gives surprise 0; smaller probabilities give bigger surprises, without limit. Using , the unit is the bit: a fair coin's heads, , is exactly 1 bit. In NumPy: np.log2(1 / p).
Halve the probability and the surprise goes up by 1 bit. Machine learning code usually uses the natural log, np.log, instead; then the unit is the nat. It's the same idea on a different scale (1 bit is about 0.693 nats), like metres and feet.
Entropy
The entropy of a distribution is the surprise you should expect on average, each outcome's surprise weighted by its probability:
In words: for each outcome, its probability times its surprise, added up. It's an expected value (lesson 1.10) of surprise. In NumPy: (p * np.log2(1 / p)).sum(), skipping outcomes with , which add nothing.
Drag the coin's bias:
Entropy: 1 bits. A fair coin: you can’t predict it at all, so each throw tells you a full bit.
A fair coin has the highest entropy for two outcomes, 1 bit: you can't predict it at all. A coin that lands heads 90% of the time has 0.469 bits. A certain one has 0. With three equally likely outcomes it's bits. Entropy is highest when every outcome is equally likely. Decision trees (Stage 4) use it to choose questions that make the classes in each branch as predictable as possible.
Cross-entropy
Now suppose the truth follows , but you believe . You'll be surprised by each time, because you're using your probabilities. Averaged over what really happens:
In words: the true probability of each outcome, times the surprise your model assigns it, added up. In NumPy: -(p * np.log(q)).sum(). It's the cross-entropy, and it's smallest when matches .
For one penguin that really is an Adelie, the truth is over (Adelie, Chinstrap, Gentoo). A good model says ; a bad one :
0.357 nats for the good model and 2.303 for the bad one. Lower is better.
KL divergence
Cross-entropy is never less than entropy: believing the wrong distribution can only make you more surprised on average. The extra surprise is the Kullback–Leibler (KL) divergence:
In words: how much more surprised you are, on average, for using when the truth is . It's 0 only when equals .
, , so nats: is close to , but not the same. KL divergence isn't a distance in the lesson 1.2 sense, because and usually differ. Variational autoencoders (Stage 14) and fine-tuning language models with feedback (Stage 13) use it to keep one distribution near another.
Why classifiers minimise cross-entropy
Look again at step 3. When the truth is one class, is 1 for that class and 0 for the rest, so every term but one drops out:
In words: the cross-entropy is just the surprise of the right answer under the model. That's also the negative log-likelihood of the label (lesson 1.10). So three goals turn out to be the same goal:
- make the model's probabilities match the truth (small cross-entropy);
- make the true labels as likely as possible (maximum likelihood);
- make the KL divergence from the truth small (since doesn't depend on the model).
Averaged over every example in the training data, this is the cross-entropy loss, also called log loss. Logistic regression (Stage 3), every neural network classifier (Stage 6) and every language model (Stage 13) is trained by making it small. Its gradient is simple too, which Stage 3 shows.
Wrong way: log 0 again
A model that's certain, and wrong, says probability 0 for the true class. Its surprise is : infinite. In code it's worse than infinite:
The answer is nan. The true class's term is , but the other terms are and , and is nan (lesson 1.1). One nan in a loss spreads through every gradient, and training breaks.
The standard fix is to clip the probabilities into a range like before taking logs. In NumPy: np.clip(q, 1e-12, 1.0). Now the loss is 27.63 nats: a huge penalty for being certain and wrong, but a number training can work with. scikit-learn's log_loss and PyTorch's loss functions do something like this for you; when you write a loss yourself, you have to do it.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Shannon, A Mathematical Theory of Communication, Bell System Technical Journal 27(3), 1948: where entropy and the bit as a unit of information come from
- Goodfellow, Bengio and Courville, Deep Learning, chapter 3: Probability and Information Theory: self-information, entropy, KL divergence and cross-entropy (3.13)
- NumPy: numpy.clip: limiting values to a range
- NumPy: numpy.log2: the base-2 logarithm, for bits