Gradient boosting

Gradient boosting computed live, stage by stage: each small regression tree fits the residuals, scaled by the learning rate. Matches scikit-learn at every stage, in 1D and as a 3D surface.

Updated

  • Boosting as gradient descent in function space
  • Residuals as negative gradients
  • Learning rate and number of stages
  • Tree depth and interactions
  • Overfitting with too many stages
m = 0m = 1m = 2m = 3m = 5m = 8m = 13m = 21m = 34m = 60F ← F + ν·h(x)
Preparing the lab…

Intuition

Correct a little at a time

Start by guessing every point with one constant (the mean): the error is large. Look at where the guess is too high and where too low — those are the residuals. Sketch the shape of the residuals roughly with a small tree of a few leaves, and add a small fraction of it to the prediction. Compute the new residuals, grow another tree… Every tree is weak, but hundreds of them added together can approximate a complicated curve.

The three small labs below really boost stage by stage. Grey points are the training data (the true function plus noise 0.3), the dashed line is the true function, the thick orange line is the current prediction FmF_m and the faint orange line the previous stage's Fm−1F_{m-1}. Defaults: learning rate 0.15, tree depth 2 (at most 4 leaves).

Stage 1Preparing…
Stage 30Preparing…
Stage 400Preparing…

Stage 1: one tree can only cut the flat line into a few pieces, and only 15% of it is added, so the curve barely moves; training MSE falls from 2.29 to 1.75. Stage 30: the prediction already hugs the true wave, with test MSE 0.117. Stage 400: training MSE is down to 0.0011 and the curve bends to chase every noisy point, while test MSE has risen to 0.168 — it has overfitted.

Math

The algorithm

Training data {(xi,yi)}i=1n\{(x_i, y_i)\}_{i=1}^n, squared loss L=12∑i(yi−F(xi))2L = \frac12\sum_i (y_i - F(x_i))^2:

  1. F0(x)=yˉF_0(x) = \bar y (the constant that minimises squared loss);
  2. for m=1,…,Mm = 1, \dots, M:
    • residuals ri=yi−Fm−1(xi)r_i = y_i - F_{m-1}(x_i);
    • fit a CART regression tree (splitting on mean squared error, maximum depth DD) to {(xi,ri)}\{(x_i, r_i)\}, giving hmh_m, whose leaf values are the means of the residuals falling into each leaf;
    • Fm(x)=Fm−1(x)+ν hm(x)F_m(x) = F_{m-1}(x) + \nu\, h_m(x).

Why "gradient" boosting

Treat the predictions F(xi)F(x_i) at the training points as parameters. The gradient of the loss with respect to each is

∂L∂F(xi)=−(yi−F(xi))=−ri\frac{\partial L}{\partial F(x_i)} = -(y_i - F(x_i)) = -r_i

The residual is the negative gradient. Ordinary gradient descent steps along −∇L-\nabla L; gradient boosting does the same, except the "direction" is approximated by a tree so that it also gives values at unseen xx. With a different loss (log loss, Huber), fit that loss's negative gradient instead.

Residuals at stage 1Preparing…
Stage 10Preparing…
Stage 60Preparing…

Blue points are the residuals before this stage, and the black line is the tree fitted to them. At first the residuals are the whole wave; by stage 60 almost nothing but noise is left, and the tree starts fitting the noise — that is where overfitting comes from.

Learning rate and number of stages

Look at training and test MSE after every stage. The noise has standard deviation 0.3, so test MSE cannot go much below 0.09. Within 400 stages:

Learning rate νBest test MSEAt stage
10.14717
0.30.11320
0.15 (default)0.11635
0.050.115190
0.010.123400 (still falling)

The smaller the learning rate, the more stages it takes to reach the best point, and the more slowly the error rises afterwards. ν=1\nu = 1 means no shrinkage: each tree swallows the residuals whole, memorises the noise quickly, and its best is only 0.147. In practice ν\nu is set fairly small (0.01–0.1) and a validation set decides when to stop (early stopping).

Tree depth

Depth sets how complicated a shape each tree can express. Default data, learning rate 0.15:

  • depth 1 (stumps, one cut per tree): best test MSE 0.112, but only at stage 254;
  • depth 2: 0.116;
  • depth 6: by stage 120 training MSE is close to 0 and test MSE 0.182, and its best is only 0.156.

A tree of depth DD has at most 2D2^D leaves. Each tree should be a "weak learner", leaving the complexity to the sum of stages.

How it is computed, with real numbers

Once the lab above has loaded, this panel works through the same formulas with its current numbers.

Experiment

Follow the lesson

Course

Lesson 1: residuals, learning rate and overfitting

5 steps · each one: set up and run → look → answer

  1. 1. The first tree

    Default wave data, one stage. Look at the residuals and the update in "How it is computed" below.

    Why does the curve move so little?
  2. 2. The residuals shrink

    Go to stage 30 and open "Residuals and tree".

    Why do the residuals look more and more like random noise?
  3. 3. Too far

    Run all 120 stages and look at the MSE curves on the right.

    Training MSE keeps falling, while test MSE is lowest around stage 35 and then rises. Why?
  4. 4. No shrinkage

    Set the learning rate to 1.

    Training MSE quickly reaches 0, but test MSE is 0.183, worse than the default's 0.126. Why?
  5. 5. Trees that are too deep

    Learning rate back at 0.15, tree depth 6.

    Why are deeper trees worse (test 0.182)?

Try these settings

On the step data at stage 120, stumps give test MSE 0.208 and depth 2 gives 0.232: the target is itself a few constant pieces, so cutting once per tree fits it well. With learning rate 0.01, test MSE after 120 stages is still 0.533 — not overfitting, just not finished learning.

Exercises

Exercises

Exercise 1: early stopping

After the default 120 stages test MSE is 0.126. Without changing the learning rate or tree depth, finish with test MSE at most 0.12.

Hint

Cut the number of stages to 60 (0.118) or 35 (0.116).

  • Test MSE ≤ 0.120

Exercise 2: learn slowly

After 400 stages at the default learning rate, test MSE is 0.168. Change only the learning rate so that test MSE after 400 stages is at most 0.13.

Hint

Learning rate 0.05: 0.126; 0.01: 0.123.

  • Stage ≥ 400
  • Test MSE ≤ 0.130

Quiz

With squared loss, what does each tree in gradient boosting fit?
If you lower the learning rate from 0.15 to 0.05, to get a similar result you usually need…
What is the main difference between gradient boosting and a random forest?

Further reading

Frequently asked questions

Why is it called “gradient” boosting?

Treat the predictions at the training points as parameters: the negative gradient of the loss with respect to them is what each tree fits. With squared loss the negative gradient is exactly the residual, so "fitting residuals" is gradient descent in function space.

How should I choose the learning rate (shrinkage)?

A smaller learning rate needs more stages but usually generalises better. A common choice is 0.01–0.1, with early stopping on a validation set to pick the number of stages. On this page learning rate 1 reaches a best test MSE of 0.147, while 0.3 to 0.05 all land at 0.113–0.116.

Can gradient boosting overfit?

Yes. Too many stages, trees that are too deep or a learning rate that is too large all let the model fit noise. With this page's defaults test MSE is lowest at stage 35 and has risen to 0.168 by stage 400. Early stopping, subsampling (stochastic gradient boosting), limits on the number of leaves and regularisation keep it in check.

How are XGBoost and LightGBM related to gradient boosting?

They are all gradient-boosted decision trees. XGBoost uses second derivatives and regularised leaf weights, LightGBM speeds things up with histograms and leaf-wise growth, and CatBoost specialises in categorical features. The core loop is the same as on this page: fit the negative gradient, multiply by the learning rate, add.

Can gradient boosting do classification?

Yes. Switch to log loss: F is the log-odds, each tree fits y − σ(F), and leaf values are computed with a Newton step. For multi-class problems each stage grows one tree per class.

Further reading