Advanced Usage of Custom Objectives
Contents
Overview
XGBoost allows optimizing custom user-defined functions based on gradients and Hessians provided by the user for the desired objective function.
In order for a custom objective to work as intended:
The function to optimize must be smooth and twice differentiable.
The function must be additive with respect to rows / observations, such as a likelihood function with i.i.d. assumptions.
The range of the scores for the function must be unbounded (i.e. it should not work exclusively with positive numbers, for example).
The function must be convex. Note that, if the Hessian has negative values, they will be clipped, which will likely result in a model that does not fit the function well.
For multi-output objectives, there should not be dependencies between different targets (i.e. Hessian should be diagonal for each row).
Some of these limitations can nevertheless be worked around by foregoing the true Hessian of the function, using something else instead such as an approximation with better properties - convergence might be slower when not using the true Hessian of a function, but many theoretical guarantees should still hold and result in usable models. For example, XGBoost’s internal implementation of multionomial logistic regression uses an upper bound on the Hessian with diagonal structure instead of the true Hessian which is a full square matrix for each row in the data.
This tutorial provides some suggestions for use-cases that do not perfectly fit the criteria outlined above, by showing how to solve a Dirichlet regression parameterized by concentrations.
A Dirichlet regression model poses certain challenges for XGBoost:
Concentration parameters must be positive. An easy way to achieve this is by applying an ‘exp’ transform on raw unbounded values, but in such case the objective becomes non-convex. Furthermore, note that this function is not in the exponential family, unlike typical distributions used for GLM models.