Contents
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).
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
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.
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,
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):
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.