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 05 of 09

05One bowl

Chapter 4 looked at the push at one point. To see why squared error stalls, look at the whole landscape: the loss over both weights, the intercept and the slope (Figure 4).

The two losses over the intercept and the slope (standardized radius; darker is lower; cross-entropy on a log scale). Lines: the paths of gradient descent of Figure 3b, with a dot every 25 steps, from the same start (white circle) to the end of 600 steps (diamond). Star: the minimum of cross-entropy, found by Newton’s method. Computed for this tutorial, with the same data and settings as the companion video.
Figure 4. The two losses over the intercept and the slope (standardized radius; darker is lower; cross-entropy on a log scale). Lines: the paths of gradient descent of Figure 3b, with a dot every 25 steps, from the same start (white circle) to the end of 600 steps (diamond). Star: the minimum of cross-entropy, found by Newton’s method. Computed for this tutorial, with the same data and settings as the companion video.
Try itThe race on the two landscapes

The landscapes of Figure 4 (darker is lower) and the accuracy curves of Figure 3b, played together. Both runs start at the same confidently wrong weights and take steps of the same size. Press Play, or drag the step.

5.1Cross-entropy is convex

A function is convex if its graph lies below every chord: a single bowl, with no false bottoms. For a twice-differentiable function it is enough that the Hessian, the matrix of second derivatives, is positive semidefinite everywhere.

Theorem 1 (Cross-entropy is convex, Murphy, 2022, eq. 10.23–10.25). The Hessian of the logistic loss (5) is

H=\nabla_w^2J=\sum_{i=1}^{n}p_i(1-p_i)\,x_ix_i^\top=X^\top SX,\qquad S=\mathrm{diag}\bigl(p_1(1-p_1),\dots,p_n(1-p_n)\bigr), \tag{12}

and it is positive semidefinite for every w, so J is convex. If the columns of X are linearly independent, H is positive definite and J is strictly convex.

Try itStep through the proof: cross-entropy is one bowl

Differentiate the gradient once more, then test the curvature in every direction v.

Differentiate the gradient (7) once more. Only p_i depends on w, and by (2) and the chain rule \nabla_wp_i=p_i(1-p_i)\,x_i; so the derivative of (p_i-y_i)x_i is x_i\,p_i(1-p_i)\,x_i^\top, which gives (12). For any vector v,

v^\top Hv=\sum_{i=1}^{n}p_i(1-p_i)\,(x_i^\top v)^2\;\ge\;0 , \tag{13}

because every p_i(1-p_i) is positive and every square is non-negative. The sum is zero only if x_i^\top v=0 for all i, that is Xv=0, which for linearly independent columns forces v=0.

So the cross-entropy surface is one bowl (Figure 4a): every downhill path leads to the same bottom, and gradient descent with a suitable step size reaches it from anywhere. The Hessian also makes Newton’s method practical, w\leftarrow w-(X^\top SX)^{-1}X^\top(p-y), which on our data reaches the minimum in 8 steps; each step is a weighted least-squares fit, hence the name iteratively reweighted least squares (Murphy, 2022, §10.2.6).

5.2Squared error on a sigmoid is not

For squared error, take one malignant tumor and its loss f(z)=(\sigma(z)-1)^2 as a function of its score. By (10), f'(z)=-2\sigma(1-\sigma)^2. Differentiate once more with (2):

\begin{aligned}f''(z)&=-2\sigma(1-\sigma)\Bigl[(1-\sigma)^2-2\sigma(1-\sigma)\Bigr]\\&=-2\sigma(1-\sigma)^2(1-3\sigma),\end{aligned} \tag{14}

which is negative whenever \sigma<\frac13. Where the model calls a malignant tumor benign, the loss of that tumor bends the wrong way: it is concave, not convex. Add up many such tumors and the total surface can bend the wrong way too. At our confidently wrong start, the Hessian of squared error has a negative eigenvalue (-0.019), so the surface is not convex there.

What it looks like is Figure 4b. Where the model calls every tumor benign, all the p_i are close to 0, every factor p_i(1-p_i) in (10) is close to 0, and the surface goes flat: a plateau, high above the minimum but with almost no slope. Gradient descent crawls across it for hundreds of steps, calling every tumor benign, before it finds the slope down. With more inputs it is worse: for a sigmoid with squared error, the number of local minima can grow exponentially with the number of inputs (Auer et al., 1996), so gradient descent can stop in a bad one.

Pause and think. Squared error with a linear model, \sum_i(y_i-w^\top x_i)^2, is convex (Episode 14). Why does putting a sigmoid inside break convexity, while putting it inside cross-entropy does not?

Show the answer

A convex function of a linear function is convex, but a convex function of a nonlinear function need not be. The square is convex in p, but p=\sigma(z) is curved, and (14) shows the composition bends the wrong way when \sigma<\frac13. With cross-entropy the logarithm undoes the exponential: as a function of the score, the loss of a malignant tumor is \log(1+e^{-z}), whose second derivative \sigma(1-\sigma) is always positive.

That completes the answer: cross-entropy is the likelihood, it pushes hardest on confident mistakes, and it is one bowl. Squared error on a sigmoid loses all three. Now, three follow-ups interviewers love.