Entropy: how much information there is and how a model learns by measuring it
How much information a result carries, how far it can be compressed and why a classifier is trained by measuring it. AI course, 1.5.

AI from the First Bit › Level 1 — Uncertainty: the raw material of AI part 5 of 5
- 1The unlikely happens too: chance and distributions
- 2Bayes without formulas: why a positive does not mean what it seems
- 3A sample is not the whole truth: minimal statistics
- 4Is my improvement real? When a difference is more than chance
- 5Entropy: how much information there is and how a model learns by measuring it
Some results teach you something and some tell you nothing new. If you know a die is loaded to always land on 6, seeing it land on 6 does not surprise you. Entropy measures that with a number: how much surprise, or how much information, a result brings on average. And that same number, in one of its variants, is the one used to train an AI classifier.
One way to see it: put eight numbered balls in a bag, like those of a bingo game, shake them, draw one without looking and count how many yes-or-no questions you need to guess its number. If they all have a 1, none. If they run from 1 to 8, three: “Is it greater than 4?”, “Is it greater than 6?” and a last one that decides between the two left.
Try this first
Open the Entropy / KL tab of the Probability for AI tool. There are five possible results, from A to E, and two distributions (think of the weather in your city: A is a sunny day, B a cloudy one, C a rainy one, D a stormy one and E a snowy one; a distribution spreads the probability over the five, and here the one in the blue bars says how many days of each kind there really are and the one in the orange bars, what the forecast predicts; that is, reality against prediction). The blue one is called P and the orange one, Q. P starts with A at 40%, B at 25%, C at 20%, D at 10% and E at 5%. Below, H(P) = 1.415 nats: that is its entropy (nats are its unit; they are explained shortly).
Now open the same tab with all five P values equal. H(P) rises to 1.609: it could be your city if any weather (sun, clouds, rain, storm or snow) were equally likely. That way there is none to bet on, because nothing tells you that one will come up more than another, and it is the hardest case to guess: with five results, the entropy cannot come out higher. It is not a limit of the tool: 1.609 is ln 5, the most the formula gives with five results. And open the P concentrated on A: A has all the probability and the other four, zero. H(P) falls to 0: it is like a desert where it is always sunny. A always comes up, and knowing that A has come up tells you nothing.
This is like the weather forecast. In a desert where it is always sunny nobody checks it: you know what it will say, and when it says it you learn nothing. In a city where every kind of day is equally likely, tomorrow’s is a surprise every time, and that is why the forecast tells you the most. Your city, with more sun than snow, sits in the middle: the sun you already expected; the snow catches you by surprise.
What just happened
The idea comes from Claude Shannon, who published it in 1948 in A Mathematical Theory of Communication (Entropy (information theory), on Wikipedia). Entropy is the average surprise: a frequent result surprises little, a rare one a lot, and the average is taken according to how often each one comes up. With all five P equal, any result is equally surprising and the entropy is at its maximum. With all of P on A, there is only one possible result, there is no surprise at all and the entropy is zero.
If you want the calculation: the surprise of a result that comes up with probability is , and the entropy is the average of those surprises, weighted by the probability of each result.
The units depend on the logarithm used. The tool uses the natural logarithm, and then entropy is measured in nats. With a base-2 logarithm it is measured in bits, the unit Shannon used, and whose name he himself attributes in that paper to a suggestion by J. W. Tukey (the original 1948 paper). To go from nats to bits you multiply by 1.443: the 1.609 nats of five equally likely results are 2.32 bits.
Entropy also says how far something can be compressed. Shannon proved that it is the limit of how far a message can be compressed without losing anything (Entropy (information theory), on Wikipedia). A text that always repeats the same letter has entropy close to zero and compresses almost entirely; one in which every letter is equally likely is the one that compresses least. But that limit has a condition: that each letter comes up independently of the previous ones, with the order not mattering (Shannon’s source coding theorem, on Wikipedia). In a real text the order matters, and you will check that at the end.
With independent letters, below the entropy you can only compress by losing something along the way: when you decompress, what comes back is no longer the same. That is what “lossy” formats do, like JPEG with photos or MP3 with music.
Now the other distribution. The orange one, Q, is what a model predicts (in the example, the forecast). The tool shows a sum: H(P) + KL(P‖Q) = cross-entropy. The KL (Kullback-Leibler divergence) measures how far the prediction is from reality, and it is zero only when Q is equal to P; the cross-entropy is the sum of the two. With the default values, 1.415 + 0.194 = 1.609. Press Q = P (KL→0): the KL goes to zero and the cross-entropy drops to H(P). That is training. Training a classifier means making the cross-entropy between the data and what the model predicts smaller. Since H(P) does not depend on the model, the only thing training can lower is the KL, that is, bringing the prediction closer to reality.
Seen step by step, training is this: Q starts anywhere (in the drawing, with everything at 20%) and each step brings it a little closer to P. In the weather example, it is a forecast that at first treats every kind of day as equally likely and that, day after day, learns how much sun and how much snow your city really gets.
The classic mistake
You might think that a lot of entropy is a lot of information in the sense of “things worth knowing”. A string of random letters has the highest possible entropy and says nothing. Shannon made it clear on the first page of his paper: messages often have meaning, but “these semantic aspects of communication are irrelevant to the engineering problem”. Entropy measures how much it costs to transmit or store a message, not whether what it says is true or useful.
With AI the confusion has its own version: a model that lowers its cross-entropy a lot predicts its data very well, but that does not mean it knows what they mean (the same thing happened to the line that “predicted” impossible grades in What is a model?).
Try it yourself
Test yourself
In the Entropy / KL tab of Probability for AI, turn the five categories into a coin: set the P of A and B to the same value and those of C, D and E to zero (or open the coin already made). Before looking, what should H(P) be?
It is ___ bit, which in nats is ___.
A fair coin is one yes-or-no question. To go to nats, multiply by ln 2.
See the answer
Answers: 1 and 0.693 ± 0.001.
A fair coin brings exactly 1 bit per flip, and 1 × ln 2 = 0.693 nats, which is what the tool shows. The text in “Only two letters”, further down, also comes out at 1 bit per character: it is the same coin.
Then go back to the default values and set the Q of E to zero; in reality E comes up 5% of the time. See what happens to the KL.
Test yourself
What happens to a model that rules out something that does happen?
- Nothing serious: E only comes up 5% of the time.
- The KL shoots up without limit: every time E comes up, the model is infinitely surprised.
- The KL drops, because the model saves itself a category.
See the answer
The answer is B.
The KL adds up P × log(P/Q), and with Q = 0 that logarithm has no end. That is why a model trained with cross-entropy never gives zero probability to anything: the softmax always leaves a little for each class.
Finally, go down to the box How much information does a text carry? with a normal text. Here P is the letters of the text, with how often each one appears, and the entropy is in bits: 4.148 bits per character. Below it is what the text really takes up compressed with the browser’s compressor: about 5.6 bits per character (5.631 in the screenshot below; each browser ships its own compressor, and the exact figure changes a little from one to another). Now open the same text repeated 10 times.
Test yourself
Before looking at the text repeated 10 times, bet: does the entropy per character change? And the compressed size?
The entropy per character [goes up / does not change / goes down], and the compressed size drops to about ___ bits per character.
Compare “Entropy per character” and “Actually compressed” in the two versions.
See the answer
Answers: does not change and 0.65 ± 0.05.
Entropy only looks at how often each letter comes up, and when the text is repeated the frequencies are the same: 4.148 bits. The compressor sees that everything repeats and drops far below the entropy. It does not break Shannon’s limit: the limit requires letters independent of the previous ones, and in a repeated text each letter can be guessed from the ones before it.
And press Only two letters: “abab…”, two equally frequent letters, like your coin from the first exercise. The entropy comes out at 1 bit per character, but the compressed size, at 1.077: worse than the limit. With a text this short, the compressor also has to store its own table, and entropy does not count that.
Further reading
- Entropy (information theory), on Wikipedia: the definition, the one-bit coin and the compression limit.
- A Mathematical Theory of Communication, by Claude Shannon (1948). It is a technical paper, but its introduction can be read without mathematics and explains where everything comes from.
“Entropy: how much information there is and how a model learns by measuring it” by Ramón Invarato at jarroba.com, licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0. Registered at Safe Creative.

You may also like
Is my improvement real? When a difference is more than chance
Your change raises the metric: real improvement or chance? A/B testing, the p-value and the mistake of peeking at results too early. AI course, 1.4.
A sample is not the whole truth: minimal statistics
Why the number from a sample is not the true one, how much it changes, and why pure chance can look like a trend. AI course, 1.3.






Leave a comment