Week 09 · Boosting and early stopping
Pick the number of boosting rounds without opening the test set.
← Week 8 · Subject contents · Week 10 →
Pick the number of boosting rounds without opening the test set.
Work through the problem
Under squared loss, the negative gradient with respect to each prediction is its residual y−f(x). Gradient boosting fits a small tree to those residuals and updates f ← f+ηh. If the current prediction is 4, the residual fit is 3, and η = 0.1, the update gives 4.3.
Extra rounds may fit noise. Track validation loss as trees accumulate and choose the round with minimum validation loss. Once chosen, evaluate on the untouched test set. Looking at the test curve first would turn that set into another validation set.
Validation sets the number of boosting rounds.
Run the Python lab
import numpy as np
from sklearn.ensemble import GradientBoostingRegressor
rng = np.random.default_rng(17)
x = rng.uniform(-2,2,(240,1))
y = np.sin(3*x[:,0])+rng.normal(0,.24,240)
model = GradientBoostingRegressor(n_estimators=120,max_depth=2,random_state=5).fit(x[:100],y[:100])
def curve(a,b):
return [np.mean((out-y[a:b])**2) for out in model.staged_predict(x[a:b])]
train, validation = curve(0,100), curve(100,170)
best = int(np.argmin(validation))
test_pred = list(model.staged_predict(x[170:]))[best]
print("Selected round:", best+1)
print("Validation and test MSE:", round(validation[best],3),round(np.mean((test_pred-y[170:])**2),3))
assert validation[best] <= validation[-1]
Output from the lab
Selected round: 61
Validation and test MSE: 0.088 0.07
Practice
- Why is the chosen validation error optimistic?
Check your reasoning
- It is the smallest among noisy validation estimates and also informed the choice.