MoonSay /Tutorials/Machine Learning Interview #8
Contents
  1. 01Introduction
  2. 02Surprise
  3. 03Entropy
  4. 04Cross-entropy
  5. 05KL divergence
  6. 06Information gain and mutual information
  7. 07Why training minimizes cross-entropy
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References

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):

  1. a certain outcome (p=1) carries no surprise;
  2. the less likely the outcome, the larger the surprise;
  3. 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

I(x)=\log_2\frac{1}{p(x)}\quad\text{bits}. \tag{1}

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.

outcomeprobability psurprise \log_2(1/p)in coin flips
Cupertino, dry0.99680.005 bitsalmost none
Cupertino, rain0.00328.28 bitsa bit more than 8 heads in a row
Miami, rain0.5580.84 bitsless than one
Miami, dry0.4421.18 bitsabout one
Table 1. The surprise of tomorrow’s weather in July (Example 1). Data: NOAA climate normals 1991–2020; computed for this tutorial.
Surprise \log_2(1/p) against the probability of the outcome (log scale). Gold dots: k heads in a row, probability 2^{-k}, surprise k bits. Navy dots: the four weather outcomes of Example 1. Data: NOAA climate normals; computed for this tutorial.
Figure 1. Surprise \log_2(1/p) against the probability of the outcome (log scale). Gold dots: k heads in a row, probability 2^{-k}, surprise k bits. Navy dots: the four weather outcomes of Example 1. Data: NOAA climate normals; computed for this tutorial.
Try itHow surprising is a one-in-N chance?

Pick how rare the outcome is. Its surprise \log_2(1/p), equation (1), is drawn as one bar measured with three rulers: bits, nats and decimal digits. The base only sets the unit (Section 2.3).

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,

\log_2\frac1p=\frac{\ln(1/p)}{\ln2},\qquad 1\ \text{nat}=\log_2e=1.443\ \text{bits},

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:

-\ln\frac{e^{z_c}}{\sum_je^{z_j}}=-z_c+\ln\sum_je^{z_j},\qquad -\ln\mathcal N(x\mid\mu,\sigma^2)=\frac{(x-\mu)^2}{2\sigma^2}+\tfrac12\ln(2\pi\sigma^2).

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.