---
title: Entropy: how much information there is and how a model learns by measuring it
url: https://jarroba.com/en/entropy/
author: Ramón Invarato
date: 2026-12-02
modified: 2026-12-02
language: en
license: CC BY-NC-SA 3.0 (https://creativecommons.org/licenses/by-nc-sa/3.0/) — attribution: "Entropy: how much information there is and how a model learns by measuring it", Ramón Invarato, jarroba.com
categories: Artificial intelligence
tags: ai, AI course, Artificial Intelligence, cross-entropy, entropy, information theory, KL divergence
series: Level 1 — Uncertainty: the raw material of AI (part 5 of 5)
---

# 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.

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.

[![Four bags of eight numbered, shuffled balls. All with a 1: 0 yes-or-no questions to guess the number, 0 bits. Four with a 1 and four with a 2: 1 question (“Is it 1?”), 1 bit. Two of each, from 1 to 4: 2 questions (“Is it greater than 2?”, “Is it 1?”), 2 bits. From 1 to 8: 3 questions (“Is it greater than 4?”, “Is it greater than 6?”, “Is it 5?”), 3 bits](https://jarroba.com/wp-content/uploads/2026/12/bolas-y-preguntas-www.jarroba.com_.svg)](https://jarroba.com/wp-content/uploads/2026/12/bolas-y-preguntas-www.jarroba.com_.svg)

## Try this first

Open **[the Entropy / KL tab of the Probability for AI tool](https://jarroba.com/tools/en/prob-ai?tab=kl)**. 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](https://jarroba.com/tools/en/prob-ai?tab=kl&klp=0.2,0.2,0.2,0.2,0.2)**. 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](https://jarroba.com/tools/en/prob-ai?tab=kl&klp=1,0,0,0,0)**: 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.

[![The Entropy / KL tab with the five equally likely (H(P) = 1.609 nats, the maximum) and with all the probability on A (H(P) = 0)](https://jarroba.com/wp-content/uploads/2026/12/entropia-maxima-y-cero-www.jarroba.com_-1024x157.png)](https://jarroba.com/wp-content/uploads/2026/12/entropia-maxima-y-cero-www.jarroba.com_-scaled.png)

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.

[![The weather in three places, with one bar for each kind of day. A desert, always sunny: H(P) = 0. Your city, with 40% sun, 25% clouds, 20% rain, 10% storm and 5% snow: H(P) = 1.415 nats. An unpredictable city, with all five at 20%: H(P) = 1.609 nats](https://jarroba.com/wp-content/uploads/2026/12/tiempo-tres-sitios-www.jarroba.com_.svg)](https://jarroba.com/wp-content/uploads/2026/12/tiempo-tres-sitios-www.jarroba.com_.svg)

## 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](https://en.wikipedia.org/wiki/Entropy_%28information_theory%29)). 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](https://jarroba.com/tools/en/prob-ai?tab=kl&klp=0.2,0.2,0.2,0.2,0.2), any result is equally surprising and the entropy is at its maximum. With [all of P on A](https://jarroba.com/tools/en/prob-ai?tab=kl&klp=1,0,0,0,0), 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 $p$ is $-\log p$, and the entropy is the average of those surprises, weighted by the probability of each result.

$$
H(P) = -\sum_i p_i \log p_i
$$

[![Drawing of two dice: the fair one, with all six faces at 1/6, has the maximum entropy, 1.792 nats (2.585 bits); the loaded one, which always lands on 6, has entropy 0](https://jarroba.com/wp-content/uploads/2026/12/dado-justo-y-trucado-www.jarroba.com_.svg)](https://jarroba.com/wp-content/uploads/2026/12/dado-justo-y-trucado-www.jarroba.com_.svg)

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](https://people.math.harvard.edu/~ctm/home/text/others/shannon/entropy/entropy.pdf)). 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](https://en.wikipedia.org/wiki/Entropy_%28information_theory%29)). 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](https://en.wikipedia.org/wiki/Shannon%27s_source_coding_theorem)). 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.

[![Bits per character needed, at a minimum, to store four texts without losing anything, against the 8 that each uncompressed character takes: a single repeated letter, 0; two equally frequent letters, 1; a normal text, 4.148; 27 random symbols, 4.75. Below, “the cat sleeps in the sun” squeezed below the limit comes back as “the c_t sl__ps in th_ s_n”](https://jarroba.com/wp-content/uploads/2026/12/compresion-y-perdida-www.jarroba.com_-1.svg)](https://jarroba.com/wp-content/uploads/2026/12/compresion-y-perdida-www.jarroba.com_-1.svg)

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.

[![P in blue and Q in orange for the five results; below, the sum H(P) + KL(P‖Q) = cross-entropy: 1.415 + 0.194 = 1.609 nats](https://jarroba.com/wp-content/uploads/2026/12/entropia-p-y-q-www.jarroba.com_-1024x270.png)](https://jarroba.com/wp-content/uploads/2026/12/entropia-p-y-q-www.jarroba.com_.png)

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.

[![Four moments of a training run, with P in blue and Q in orange. At the start, Q gives 20% to each result: KL = 0.194 and cross-entropy = 1.609. Halfway through training: KL = 0.055 and 1.470. Almost trained: KL = 0.011 and 1.426. With Q = P: KL = 0 and cross-entropy = 1.415, the entropy of P](https://jarroba.com/wp-content/uploads/2026/12/entrenar-acercar-q-www.jarroba.com_.svg)](https://jarroba.com/wp-content/uploads/2026/12/entrenar-acercar-q-www.jarroba.com_.svg)

## 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?](https://jarroba.com/en/what-is-a-model/)).

## Try it yourself

Test yourself

In **[the Entropy / KL tab of Probability for AI](https://jarroba.com/tools/en/prob-ai?tab=kl)**, 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](https://jarroba.com/tools/en/prob-ai?tab=kl&klp=0.5,0.5,0,0,0)). Before looking, what should H(P) be?

It is ___ bit, which in nats is ___.

**Hint:** 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?

- **A.** Nothing serious: E only comes up 5% of the time.
- **B.** The KL shoots up without limit: every time E comes up, the model is infinitely surprised.
- **C.** 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](https://jarroba.com/tools/en/prob-ai?tab=kl&sample=normal)**. 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](https://jarroba.com/tools/en/prob-ai?tab=kl&sample=repeated)**.

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.

**Hint:** 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.

[![The box How much information does a text carry? with the normal text: 4.148 bits of entropy per character and 5.631 bits really compressed](https://jarroba.com/wp-content/uploads/2026/12/texto-entropia-y-comprimido-www.jarroba.com_-1024x292.png)](https://jarroba.com/wp-content/uploads/2026/12/texto-entropia-y-comprimido-www.jarroba.com_.png)

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](https://en.wikipedia.org/wiki/Entropy_%28information_theory%29): the definition, the one-bit coin and the compression limit.
- [A Mathematical Theory of Communication](https://people.math.harvard.edu/~ctm/home/text/others/shannon/entropy/entropy.pdf), by Claude Shannon (1948). It is a technical paper, but its introduction can be read without mathematics and explains where everything comes from.

## Series: Level 1 — Uncertainty: the raw material of AI

1. [The unlikely happens too: chance and distributions](https://jarroba.com/en/chance-and-distributions.md)
2. [Bayes without formulas: why a positive does not mean what it seems](https://jarroba.com/en/bayes-without-formulas.md)
3. [A sample is not the whole truth: minimal statistics](https://jarroba.com/en/minimal-statistics.md)
4. [Is my improvement real? When a difference is more than chance](https://jarroba.com/en/is-my-improvement-real.md)
5. [Entropy: how much information there is and how a model learns by measuring it](https://jarroba.com/en/entropy.md)

