Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom loss functions #2522

Closed
jeremyhermann opened this issue Jul 17, 2017 · 4 comments
Closed

custom loss functions #2522

jeremyhermann opened this issue Jul 17, 2017 · 4 comments

Comments

@jeremyhermann
Copy link

We are assessing the viability of moving a number of our modeling projects to xgboost and wanted to understand the feasibility of supporting several new loss functions, using the existing plugin system and/or making modifications to xgboost source code.

Do you think we can get these to work?

If these require changes to xgboost source, we'd be interested in contributing back.

@hcho3
Copy link
Collaborator

hcho3 commented Aug 5, 2017

@jeremyhermann Currently, the Python binding supports custom loss function. The format will look like

def custom_loss(preds, dtrain): 
    ... do some gory computation ...
    return grad, hess

# at training time
bst = xgboost.train(params, dtrain, 10, eval_list, obj=custom_loss)

where grad is the list of first-order gradients and hess is the list of second-order gradients. If your loss function is twice-differentiable, great!

Things get tricky when your loss function is not twice differentiable. For now, you should look at #1825. Basically, what we need is to find g and h such that

[objective] <= [some constant] + g dy + 0.5 h dy^2       (1)

For twice-differentiable objectives, g and h are trivially found by taking first- and second-order gradients. (See slide 14 of this presentation.) But really any g and h that satisfy (1) will do. So if one can find suitable g and h for the huber loss, for instance, the huber loss can be used in XGBoost.

For next week or two, I plan to do some self-study on gradient boosting. I will get back to you if I have better idea about supporting non-smooth functions.

@adamwlev
Copy link

adamwlev commented Aug 6, 2017

It would be really nice if huber loss was supporting natively in xgboost. Implementing it in python works but it significantly slows the learning process.

@pommedeterresautee
Copy link
Member

@adamwlev you may use a pseudo huber loss https://en.wikipedia.org/wiki/Huber_loss which is differentiable.

@adamwlev
Copy link

adamwlev commented Sep 4, 2017

@pommedeterresautee Thanks, I am actually using this and I like it. The only thing is that training is 10-15% slower since the objective is implemented in python-numpy not native to xgboost. Unfortunately, I am intimated by the code base of xgboost/don't have the to invest in learning enough to be able to make this contribution.

@tqchen tqchen closed this as completed Jul 4, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Oct 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants