python-3.xxgboost

XGboost custom objective. Gradient & hessian problem


I am trying to create my 'objective' = original 'objective': 'reg:squarederror'.

The model results on my 'objective' and on the original 'objective': 'reg:squarederror' are very different.

Why? Where did I go wrong?

def my_squarederror(y_pred, y_true):
    grad = 2*(y_pred - y_true)
    hess = 0*y_true + 2
    return grad, hess

XGB_default_mse = xgboost.XGBRegressor().set_params(**{'objective': 'reg:squarederror'}) # original
XGB_my_mse = xgboost.XGBRegressor().set_params(**{'objective': my_squrederror}) # my

Solution

  • def my_squarederror(y_pred, y_true):
        grad = 2*(y_pred - y_true)
        hess = 0*y_true + 2
        return -grad, hess