Sub topic 2
How gradient boosted trees work
This chapter assumes you know what a decision tree is, can split data into training and validation sets, and are familiar with metrics such as RMSE and log loss. It covers the idea that LightGBM and CatBoost both build on.
The core idea
Gradient boosting builds a model as a sum of many small decision trees. Each tree is trained to correct the mistakes of the trees before it.
The prediction after m trees is:
F_m(x) = F_{m-1}(x) + learning_rate * tree_m(x)
The model starts from a constant, such as the mean of the labels for regression. Each new tree then adds a small correction.
What each tree fits
A tree does not fit the raw labels. It fits the negative gradient of the loss function, taken with respect to the current predictions. This value is often called a pseudo-residual.
For squared error loss, the negative gradient is exactly y - F(x), which is the ordinary residual. For log loss, the negative gradient is y - p, where p is the predicted probability. Because the method only needs a gradient, you can use any differentiable loss.
The training loop is:
- Start with a constant prediction.
- Compute the negative gradient of the loss for every row.
- Fit a shallow tree to those gradients.
- Add the tree to the model, scaled by the learning rate.
- Repeat for a set number of rounds.
A minimal example
This code implements the loop for squared error, using scikit-learn trees. It makes the mechanism visible.
import numpy as np
from sklearn.tree import DecisionTreeRegressor
rng = np.random.default_rng(0)
X = rng.uniform(-3, 3, size=(500, 1))
y = np.sin(X[:, 0]) + rng.normal(0, 0.2, 500)
learning_rate = 0.1
pred = np.full_like(y, y.mean())
trees = []
for m in range(100):
residual = y - pred
tree = DecisionTreeRegressor(max_depth=3).fit(X, residual)
pred += learning_rate * tree.predict(X)
trees.append(tree)
if m % 25 == 0:
print(m, np.sqrt(np.mean((y - pred) ** 2)))The training RMSE falls with each round. Libraries such as LightGBM and CatBoost do the same thing, but they add faster split finding, regularization and many other refinements.
Why shallow trees and a small learning rate
Each tree is weak on its own. Depth is kept small so that a single tree cannot memorize the data. The learning rate shrinks every tree's contribution. A smaller rate needs more trees, but it usually generalizes better.
Boosting reduces bias by adding trees. It can also overfit if you add too many. Training error keeps falling, while validation error eventually rises. This is why you need a validation set and early stopping, which later chapters cover.
Common errors
- Judging the model on training data. Training loss almost always decreases with more trees. Only validation loss tells you when to stop.
- Using a large learning rate with a fixed number of trees. The model may overshoot or overfit. Lower the rate and raise the tree count instead.
- Treating trees as independent. Unlike a random forest, boosted trees are built in sequence. Each one depends on all earlier ones, so training is harder to parallelize across trees.
- Expecting extrapolation. Trees predict constants within regions. They cannot continue a trend beyond the range of the training data.
Exercise
In gradient boosting with squared error loss, what does each new tree fit, and what does the learning rate do?
Show answer
Each new tree fits the residuals y - F(x) of the current ensemble, which are the negative gradients of the squared error. The learning rate scales how much of the new tree's prediction is added, so each tree makes only a small correction. A smaller rate usually needs more trees but generalizes better.