Contents
02Surprise
2.1What a measure of surprise must do
How surprised should you be when an outcome of probability p happens? Three requirements pin the answer down (Goodfellow et al., 2016, §3.13):
- a certain outcome (p=1) carries no surprise;
- the less likely the outcome, the larger the surprise;
- the surprise of two independent outcomes is the sum of their surprises.
Independent probabilities multiply, p_1p_2, while surprises must add. The function that turns products into sums is the logarithm, so the surprise, or information content, of an outcome x is
2.2Counted in coin flips
One bit is the surprise of one fair coin flip: \log_2 2=1. Eight heads in a row have probability 1/2^8=1/256 and surprise 8 bits. In general, an outcome with surprise k bits is as unlikely as k heads in a row. Figure 1 shows the curve: each halving of the probability adds one bit.
Worked example 1 (Tomorrow’s weather in July). Over 1991–2020, Miami International Airport recorded rain (at least 0.01 inch) on 17.3 of July’s 31 days on average, and San Jose International Airport, the nearest station to Cupertino, California, that counts rain days, on 0.1 (NOAA National Centers for Environmental Information, 2021). So the chance of rain tomorrow is 17.3/31=0.558 in Miami and 0.1/31=0.0032, about one day in 310, in Cupertino. The surprises are listed in Table 1. In Cupertino, “rain tomorrow” is big news; in Miami, the news is a dry day. Two rainy days in a row in Cupertino carry 8.28+8.28=16.6 bits: surprises add.
| outcome | probability p | surprise \log_2(1/p) | in coin flips |
|---|---|---|---|
| Cupertino, dry | 0.9968 | 0.005 bits | almost none |
| Cupertino, rain | 0.0032 | 8.28 bits | a bit more than 8 heads in a row |
| Miami, rain | 0.558 | 0.84 bits | less than one |
| Miami, dry | 0.442 | 1.18 bits | about one |
2.3Bits, nats and digits: the base sets the unit
The base of the logarithm is the number of answers per question, and it only sets the unit. Base 2 counts yes-or-no answers (bits). Base 10 counts decimal digits: a lock with three dials, each 0 to 9, has 1000 settings, and \log_{10}1000=3 digits. Base e counts nats: one nat is the surprise of an event with probability 1/e\approx0.37 (Goodfellow et al., 2016, §3.13). Changing the base multiplies every surprise by the same constant,
like converting inches to centimeters. Rain in Cupertino is 8.28 bits =5.74 nats =2.49 decimal digits.
Why machine learning uses base e. Shannon already noted that base e is convenient in analytical work with derivatives and integrals, since \frac{d}{dx}\ln x=\frac1x has no stray constant. In machine learning there is a stronger reason: the probabilities models output are e raised to a power. The sigmoid is \sigma(z)=1/(1+e^{-z}), the softmax is p_k=e^{z_k}/\sum_je^{z_j}, and the Gaussian (bell curve) density is proportional to e^{-(x-\mu)^2/(2\sigma^2)}. The natural log undoes that power, so the loss becomes simple:
The first gives the softmax gradient p-y; the second is squared error. Goodfellow et al. (2016, §6.2.2) stress that the log in the loss undoes the exp of the output unit, and that without it learning stalls when the exponent becomes very negative. With \log_2 every formula would carry an extra factor 1/\ln2. So textbooks of machine learning use the natural log and nats (Goodfellow et al., 2016, §3.13), while coding and compression use bits. Everything in this tutorial holds in either unit; we use bits for the weather story because a coin flip is easy to picture.
Pause and think. A fair die shows a six. How many bits of surprise is that? And two sixes in a row?
Show the answer
\log_26=2.585 bits for one six. The rolls are independent, so the surprises add: 5.17 bits, the same as \log_2 36.