Section 1
Self-information
How to use this pageCommit to a guess before the demo answers it. Click any colored symbol to read what it is. Each challenge is solved by moving a control, and it is worth 8 XP.
probability of an outcome, the data a second distribution, the model entropy, the main object and surprise and divergence, in nats
A weather forecast that says “80% chance of rain” hardly surprises you when it rains. A forecast of “1% chance of snow in July” is news if the snow falls. Information theory turns that feeling into a number for each outcome .
The number has three rules. Likely events carry little information, and a certain event carries none. Rarer events carry more. Independent events add their information.
Predict first. A common event has probability 0.5. A rare event has probability 0.1. Which one carries more self-information?
The book defines the self-information of an outcome as Equation 3.48:
Click any coloured symbol to see what it means.
Here is the probability of that outcome, and log is the natural logarithm. The unit is the nat. One nat is the information gained from an event of probability 1/e, about 0.37. Other books use base 2 and call the unit a bit. One bit is 1 / ln 2, about 1.44 nats, so the unit is only a rescaling.
Surprise as a function of probability
event 1event 2both together, probability
Set both sliders to 1 and nothing is learned. Set one to 0.5 and that event carries 1 bit, the content of one fair coin flip, or 0.69 nats. The dot for the pair sits at the product of the two probabilities, far to the left. Its height equals the two heights added together.
Challenge +8 XPIn the Nats tab, set the two probabilities so that the information of the pair is 3 nats, within 0.05.
Going deeper: why the information of two events adds up
Take two independent events, so that their joint probability is the product of the two probabilities. Then:
Click any coloured symbol to see what it means.
This derivation is added here and is not printed in the book. It is the reason the logarithm appears: it turns a product of probabilities into a sum of information.
The book gives an example: a tossed coin that has come up heads twice should convey twice as much information as heads once. Two independent fair coins make this concrete. Each head of a fair coin carries 0.69 nats.
The book also contrasts the sun rising, which is almost certain and carries almost no information, with a solar eclipse, which is rare and carries much more.
Predict first. Two independent fair coins both land heads. Compared with one head, how much self-information does the pair carry?
Self-information describes one outcome, and only its probability matters. The next section averages it over all outcomes.
Section 2
Shannon entropy
A distribution produces many outcomes, each with its own surprise. The Shannon entropy averages that surprise over the distribution. Equation 3.49 writes it as the expected self-information of one draw:
Click any coloured symbol to see what it means.
The symbol is the expected value over draws from , where each outcome is weighted by how often it occurs. The same number is written .
Predict first. Measured in bits, what is the entropy of a fair coin, where ?
The book gives two readings:
- Uncertainty. Nearly deterministic distributions have low entropy, and distributions close to uniform have high entropy.
- Code length. The entropy is a lower bound on the average number of bits needed to encode symbols drawn from , in base 2. Other bases change the unit.
The coin: Figure 3.5
Take a binary variable that equals 1 with probability . Figure 3.5 plots its entropy against , using the formula in its caption. That formula is not a numbered equation:
Click any coloured symbol to see what it means.
Figure 3.5, interactive
entropy your coinmaximum at
At and the coin is certain and the entropy is 0. It peaks at , where both outcomes are equally likely: ln 2 nats, which is exactly 1 bit. The curve is steepest near the ends.
Challenge +8 XPMake the entropy 0.60 nats, within 0.01, with below one half. Use the Nats tab to read it directly.
Going deeper: where the maximum sits
Differentiate the caption’s formula. This derivation is added here and is not printed in the book:
Click any coloured symbol to see what it means.
The derivative is zero exactly where , positive below it and negative above it, so the entropy peaks there.
Entropy as area, and as code length
With more outcomes, draw each one as a rectangle. Its width is and its height is in bits, so its area is its share of the entropy. The orange line marks the length of a Huffman code word for that outcome. Huffman coding is not in the book. It is added here as a concrete code, so that you can check the code-length reading.
Five outcomes: area is entropy
width , height in bits: area = length of the Huffman code word
Challenge +8 XPFind weights whose average code length matches the entropy, with a gap under 0.005 bits. Hint: try probabilities that are powers of one half.
The average code length never falls below the entropy and stays within one bit above it. That bound is standard coding theory, not printed in the book, and the readout checks it on your weights. A weight of 0 means the outcome never occurs, so it adds nothing.
Section 3
Continuous variables and differential entropy
For a continuous variable the book uses the same definition by analogy, with the density in place of the probability. The result is the differential entropy. The book does not print the integral below. It is the standard form of the analogy, written here:
Click any coloured symbol to see what it means.
Predict first. A uniform density of width 1 has height 1 everywhere. What is its differential entropy?
The book warns that some properties of the discrete case are lost. An event with unit density still has zero information, although it is not certain. This remark is added here and is not in the book: a density can be larger than 1, so the information of an outcome, minus the logarithm of its density, can be negative.
Differential entropy of two densities
uniform density, height normal density
Set the width to 1 and the entropy is 0. Narrow the uniform density below 1 and its entropy goes negative. That does not mean less than no uncertainty. The number depends on the units of : rescaling by a factor shifts the differential entropy by . This remark is added here and is not in the book.
Challenge +8 XPMake the differential entropy of the uniform density negative.
Going deeper: the two closed forms
These closed forms are not printed in the book. They follow from the integral above, and the readout checks the normal one by numerical integration.
Click any coloured symbol to see what it means.
On an interval of width the density is , so every outcome has information , and the average is the same.
Click any coloured symbol to see what it means.
For a normal density the average of is , which turns the integral into one half plus one half of the logarithm of 2π .
Among all densities with a given variance, the normal has the largest differential entropy. This is a standard fact, added here for context.
Predict first. A uniform density and a normal density have the same variance. Which has the larger differential entropy?
Section 4
The KL divergence
Two distributions and over the same outcomes need a number that says how different they are. The book uses the Kullback-Leibler (KL) divergence, Equation 3.50:
Click any coloured symbol to see what it means.
The average is over , as the subscript says. Each outcome contributes the log of the ratio , weighted by how often produces it. In the second form it is the average of two self-informations: minus .
Predict first. Starting settings: has weights (0.4, 0.3, 0.2, 0.1) and is uniform. Which divergence is larger?
Two distributions, two divergences
terms of terms of
The book points out three properties. The divergence is never negative, which the book calls its most notable property. It is zero if and only if the distributions agree, for discrete variables, or equal almost everywhere for continuous ones. And it is not symmetric, so for some pairs. A true distance must be symmetric, so the divergence is not one.
Challenge +8 XPDrag the outcome 4 slider down to zero while still gives outcome 4 some weight. Read which divergence becomes infinite.
When says an outcome is impossible but still produces it, the code for has no word for it. The cost is unbounded and is infinite. The reverse direction stays finite, because outcomes that never produces drop out of its average. That is the asymmetry in its starkest form.
Going deeper: why the divergence is never negative
This proof is added here and is not printed in the book. It uses Jensen’s inequality for the concave logarithm, and the sum runs over the outcomes where :
Click any coloured symbol to see what it means.
So the divergence is at least 0. Equality holds only when wherever is positive.
Section 5
The direction of the divergence
Because the divergence is asymmetric, using or as an objective gives different answers. Figure 3.6 shows it. The target has two separated peaks. We approximate it with one Gaussian , with center and width , by minimizing one divergence or the other.
Predict first. For the two-peak target, where does the single Gaussian sit when it minimizes ?
- Minimizing keeps the approximation where the target is high. With two peaks it blurs them together.
- Minimizing keeps the approximation where the target is small. With well separated peaks it picks one peak, and it could equally pick the other.
Figure 3.6, interactive
, two equal peaks of width 0.7, one Gaussianyour choice on the mapbest fits found by search
Press the first button. The best Gaussian for sits between the peaks and is wide enough to cover both. Then press the second and third buttons. The best Gaussian for sits on one peak and ignores the other, and both peaks give the same value. Lower the distance until the peaks merge, and the two answers coincide. The book adds a caveat: when the two peaks are not separated by a strong low-probability region, the reverse divergence can still blur them.
Challenge +8 XPSwitch to the reverse map and drag the marker until is within 0.01 nats of its best value.
Going deeper: where the forward best fit comes from
This derivation is added here and is not printed in the book. Minimizing over a Gaussian is the same as maximizing the average of log over the target . That gives the mean and the variance of the target. For this target the mean is 0 and the variance is 0.49 + . With the width is 2.596, which the forward search finds.
Section 6
Cross-entropy and the 0 log 0 convention
The cross-entropy is the average cost of coding draws from with a code built for . Equation 3.51 defines it:
Click any coloured symbol to see what it means.
The book also states that it equals the entropy of plus the divergence. The identity is stated in the text and is not numbered:
Click any coloured symbol to see what it means.
The cost of the best code, the entropy , is a floor. The cross-entropy is that floor plus the extra paid for using the wrong code, which is the divergence.
Predict first. Q is chosen to minimize the cross-entropy , not the divergence . Is the best different?
Cross-entropy of two coins
cross-entropy as variesentropy , the flooryour ; the gap to the floor is the divergence
Challenge +8 XPMove until the orange gap disappears: must match to within 0.005.
The curve touches the floor exactly where equals , and everywhere else it lies above. The gap is . Near the ends the curve rises steeply: a model that gives almost no probability to an outcome that happens often pays a large cost.
Going deeper: where the identity comes from
This derivation is added here and is not printed in the book. Write the cross-entropy and split the logarithm of the ratio:
Click any coloured symbol to see what it means.
The first part is the divergence and the second is the entropy. The entropy does not involve , so the best is the same for both.
The convention for 0 log 0
Expressions of the form 0 log 0 appear when an outcome has probability 0. The book treats them as the limit as , and that limit is 0.
Click any coloured symbol to see what it means.
The limit of x log x
x ln xyour x
Slide down. The product shrinks to 0 even though log goes to minus infinity, because shrinks faster. The convention applies only to the weight in front of the logarithm, so it does not rescue a divergence where is 0 and is positive.
What to take away
- Self-information measures surprise. It is 0 for certain events, grows for rare ones, and adds for independent ones. In nats it uses the natural logarithm; in bits, base 2.
- Shannon entropy is the average surprise. It is 0 for a deterministic distribution, largest for a uniform one, and a lower bound on the average code length.
- For continuous variables the same definition gives the differential entropy, which can be negative and depends on units.
- The KL divergence is the extra information paid for using a code built for on draws from . It is never negative, zero only when the distributions agree (almost everywhere), and not symmetric.
- The direction matters. makes cover the target and may blur its modes, while lets pick a single mode.
- The cross-entropy equals plus . The two differ by a term that does not involve , so minimizing either over gives the same answer.
- By convention the term 0 log 0 equals 0, the limit of as .
Final quizCheck yourself
Questions that mix the whole module. Each one is scored, and the score feeds your mastery of this module.
Source: Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, MIT Press, 2016, Section 3.13 (pages 73–76), free to read at deeplearningbook.org. Where the book prints equation numbers, the numbers here follow the book. The order of ideas follows the book. The explanations and all demos were written for this page.