Negative gradients and residual updates
Quick answer
Gradient boosting fits a new learner to the direction that decreases a chosen loss. For half squared error, that direction is the residual: target minus current prediction. For other losses, it is generally a different quantity. Examples are static and locally tested with Python 3.13.5, scikit-learn 1.8.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
One squared-loss stage
y = [2,6]f = [4,4]r = [a-b for a,b in zip(y,f)]new = [a+0.5*b for a,b in zip(f,r)]print(r,new)print(sum((a-b)**2 for a,b in zip(y,new))/len(y))Observed local output:
[-2, 2] [3.0, 5.0]1.0The perfect residual correction leaves half the error; squared error becomes one quarter.
How it works
Let L(y,F)=0.5(y-F)^2. Its derivative with respect to the prediction F is F-y. The negative derivative is y-F, so the next regression tree learns residuals. Start with the training mean, compute residuals, fit a learner to them, then update F <- F + eta*h. For squared error a leaf correction is the mean residual within that leaf (for unweighted rows). With y=[2,6], F=[4,4], the residuals are [-2,2]. A perfect residual learner with eta=0.5 yields [3,5], reducing mean squared error from 4 to 1. The factor 0.5 in the definition of the loss avoids an extra factor of two in the gradient. For binary logistic loss with raw score F and p=sigmoid(F), the negative gradient is y-p for labels 0/1. This is a residual in probability space, but the stage update is to raw scores, not directly to p. Leaf step sizes can require additional optimization depending on loss; do not assume every leaf is simply a residual mean. For differentiable losses the negative gradient gives a local direction of improvement; it does not guarantee improvement for arbitrary finite step sizes or validation data.
Check the logistic negative gradient
from math import log, expdef loss(s): return log(1+exp(-s)) # y=1, small scores onlyh=0.00001print(round(-(loss(h)-loss(-h))/(2*h),6))print(1-0.5)Observed local output:
0.50.5At raw score zero, probability is 0.5 and the negative derivative agrees with y-p. Use stable log-loss for extreme scores.
Common mistakes
Do not fit to the original target each round and call it gradient boosting. Do not reverse residual sign. Do not add a probability residual directly to the predicted probability for a log-loss classifier.
Practice and next steps
For y=[4,10], initial F=[7,7] and eta=1/3, calculate residuals, updated predictions and mean squared error assuming a perfect residual learner. Complete the knowledge check and staged exercise. Next: Learning rate and subsampling.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Negative gradients and residual updates — knowledge check
Apply negative gradients and residual updates to new examples.
For half squared loss, y=8 and F=5. Negative derivative?
For a perfect residual learner with eta=0.5, how much residual remains?
For binary logistic loss with y=0 and p=0.75, negative gradient?
Where is the binary logistic stage update applied?
Why can validation loss rise while training loss falls?