MoonSay /Tutorials/Machine Learning Interview #15
Contents
  1. 01Introduction
  2. 02From a line to a probability
  3. 03Maximum likelihood
  4. 04The push
  5. 05One bowl
  6. 06Interview follow-ups
  7. 07Common misconceptions
  8. 08Interview questions and model answers
  9. 09Summary
  10. References
Logistic Regression/Chapter 04 of 09

04The push

The next reason to prefer cross-entropy is how hard each loss pushes the weights when the model is wrong. Gradient descent (Episode 9) moves the weights against the gradient, so the gradient is the push.

4.1The gradient of cross-entropy

Proposition 2 (The gradient is prediction minus truth, Murphy, 2022, eq. 10.21). For one example, \partial J_i/\partial z_i=p_i-y_i. For all of them,

\nabla_wJ=\sum_{i=1}^{n}(p_i-y_i)\,x_i=X^\top(p-y). \tag{7}
Try itStep through the derivation: the gradient is prediction minus truth

Watch the orange factor: the sigmoid's slope appears in the chain rule and cancels the denominator exactly.

Write the loss of one example as a function of p, J_i=-[y\log p+(1-y)\log(1-p)], and differentiate:

\frac{\partial J_i}{\partial p}=-\frac{y}{p}+\frac{1-y}{1-p}=\frac{-y(1-p)+(1-y)p}{p(1-p)}=\frac{p-y}{p(1-p)} . \tag{8}

By the chain rule and (2), \partial p/\partial z=p(1-p), which cancels the denominator:

\frac{\partial J_i}{\partial z}=\frac{p-y}{p(1-p)}\cdot p(1-p)=p-y . \tag{9}

Finally z_i=w^\top x_i, so \partial z_i/\partial w=x_i; summing over the examples gives (7).

The result is remarkably simple. Each example pushes its score by its error, prediction minus truth, a number between -1 and 1, and the gradient adds up these errors weighted by the inputs. It has exactly the form of the least-squares gradient of Episode 14, \nabla_w\frac12\|y-Xw\|^2=X^\top(Xw-y), with the prediction Xw replaced by p. The logarithm in the likelihood undoes the exponential in the sigmoid, so the loss becomes flat only when the answer is already right (Goodfellow et al., 2016, §6.2.2.3).

4.2The gradient of squared error

Squared error wraps the sigmoid inside the square. For one example, J_i^{\mathrm{SE}}=(p-y)^2, and the chain rule gives

\frac{\partial J_i^{\mathrm{SE}}}{\partial z}=2(p-y)\cdot\frac{\partial p}{\partial z}=2(p-y)\,p(1-p). \tag{10}

The error p-y is still there, but now multiplied by 2p(1-p), twice the sigmoid’s slope. Divide the two pushes:

\frac{|\partial J_i/\partial z|}{|\partial J_i^{\mathrm{SE}}/\partial z|}=\frac{1}{2p(1-p)}\;\ge\;2 , \tag{11}

and the ratio grows without limit as p goes to 0 or 1. For a malignant tumor (y=1) that the model calls almost surely benign (p\to0), the cross-entropy push tends to its maximum, 1, while the squared-error push tends to 0 (Figure 3a). The extra factor switches the learning off exactly when the model is confidently wrong, the moment it most needs to learn.

(a) The size of the push on the score of a malignant tumor, for cross-entropy, |p-y|, and squared error, 2|p-y|\,p(1-p); dots: the small malignant tumor of Example 2. (b) Accuracy during gradient descent from the same confidently wrong start, with the same step size, for the two losses (standardized radius; start (w_0,w_1)=(0,-6), which calls large tumors benign; step size 1 on the average loss). Computed for this tutorial, with the same data and settings as the companion video.
Figure 3. (a) The size of the push on the score of a malignant tumor, for cross-entropy, |p-y|, and squared error, 2|p-y|\,p(1-p); dots: the small malignant tumor of Example 2. (b) Accuracy during gradient descent from the same confidently wrong start, with the same step size, for the two losses (standardized radius; start (w_0,w_1)=(0,-6), which calls large tumors benign; step size 1 on the average loss). Computed for this tutorial, with the same data and settings as the companion video.
Try itHow hard does each loss push?

A malignant tumor (y = 1) to which the model gives probability p. The bars are the size of the push on its score, equations (7) and (10). It starts at the small malignant tumor of Example 2.

Worked example 3 (Which pushes harder?). For the small malignant tumor, p=0.02 and y=1. Cross-entropy pushes its score with p-y=-0.98. Squared error pushes with 2(p-y)\,p(1-p)=2(-0.98)(0.02)(0.98)=-0.037, about 26 times weaker. Now train both models from the same start, one that is confidently wrong about most tumors (it calls the large ones benign), with the same step size (Figure 3b). Cross-entropy passes 85\% accuracy after 15 steps of gradient descent; squared error needs 489.

Most of those 489 steps are spent at 62.7\%, the accuracy of calling every tumor benign. Why does squared error stall there for so long?