Contents
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,
Write the loss of one example as a function of p, J_i=-[y\log p+(1-y)\log(1-p)], and differentiate:
By the chain rule and (2), \partial p/\partial z=p(1-p), which cancels the denominator:
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
The error p-y is still there, but now multiplied by 2p(1-p), twice the sigmoid’s slope. Divide the two pushes:
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.
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?