Sub topic 3
Loss Functions and Their Gradients
This chapter assumes you know Python and NumPy, the basics of supervised learning, and what a derivative is at an intuitive level. It explains what a loss function is, why boosting needs its gradient, and how the gradient and hessian look for the two losses you will use most.
Why boosting needs a loss
A loss function scores one prediction against its true label. A lower loss means a better prediction. Training minimizes the total loss over the data.
Gradient boosting works with any loss that is differentiable. For each row, it asks how the loss changes if the current prediction moves a little. That slope is the gradient. The next tree is fit to push predictions in the direction that lowers the loss.
Squared error
For regression, the common loss is squared error. For a label y and a prediction p, it is:
L = 1/2 (y - p)^2
The gradient with respect to p is p - y. The negative gradient is y - p, which is the residual. This is why the first chapter's code can fit each tree to y - pred. The residual is the negative gradient for squared error.
The second derivative, the hessian, is 1 for every row. The curvature of squared error is constant.
Log loss
For binary classification, the model outputs a raw score f. A sigmoid turns the score into a probability p:
p = 1 / (1 + exp(-f))
Log loss is L = -(y log p + (1 - y) log(1 - p)). Its gradient with respect to the raw score f is p - y. Its hessian is p(1 - p).
Here the negative gradient is not a plain residual in label space. It is the gap between the label and the predicted probability. The hessian is small when the model is confident, either near 0 or near 1. It is largest at p = 0.5.
Computing them in code
The following function returns the gradient and hessian for both losses. You can check the log loss formulas against a numerical derivative.
import numpy as np
def squared_error_grad_hess(y, pred):
grad = pred - y
hess = np.ones_like(pred)
return grad, hess
def logloss_grad_hess(y, raw_score):
p = 1.0 / (1.0 + np.exp(-raw_score))
grad = p - y
hess = p * (1.0 - p)
return grad, hess
def logloss(y, raw_score):
p = 1.0 / (1.0 + np.exp(-raw_score))
return -(y * np.log(p) + (1 - y) * np.log(1 - p))
y = np.array([1.0, 0.0, 1.0])
f = np.array([0.5, -1.0, 2.0])
grad, hess = logloss_grad_hess(y, f)
eps = 1e-6
numeric = (logloss(y, f + eps) - logloss(y, f - eps)) / (2 * eps)
print(np.allclose(grad, numeric)) # True
print(hess)Common errors
- Taking the gradient with respect to the probability. Boosting adds trees to the raw score, so the gradient must be taken with respect to f, not p.
- Mixing up signs. Trees fit the negative gradient. XGBoost works with the gradient itself and handles the sign inside its split scoring.
- Assuming the hessian is always 1. That holds only for squared error. For log loss it changes per row, and the next chapters use it.
Exercise
A binary model gives a raw score of 0 for a row whose label is 1. What are the log loss gradient and hessian for that row?
Show answer
The sigmoid of 0 is p = 0.5. The gradient is p - y = 0.5 - 1 = -0.5. The hessian is p(1 - p) = 0.25. The negative gradient is positive, so the next tree pushes the raw score up.