Skip to content

HistGradientBoosting: Implement custom loss function like LightGBM permits it #15841

Closed

Description

@flamby

Hi,

I've good results w/ HistGradientBoostingClassifier (an estimator deserving to be known by all), and I rely a lot on decision threshold.

LightGBM permits to use a custom loss function as demonstrated here and this macro-soft F1 loss function could permit me to not care about decision threshold, and improve results.

So before asking: newbie here ;-).

Is it something achievable and relevant for HistGradientBoosting?
As HGB is inspired by LightGBM, I would say yes. But I'd like your thought on that.

Thanks!

Activity

  1. NicolasHug commented on Dec 9, 2019

    @NicolasHug
    Member

    I think this is something we could consider supporting, at least in a private API. A loss class is simply a bunch of methods and a bunch of attributes.

    We would need to wait for #14830 to be merged since it slightly changes the API

    WDYT @ogrisel @adrinjalali ?

  2. ogrisel commented on Dec 9, 2019

    @ogrisel
    Member

    +1 as a private API (no API stability guaranteed from one version of scikit-learn to the next).

  3. adrinjalali commented on Dec 14, 2019

    @adrinjalali
    Member

    I don't mind if it's public. It's still experimental, we can still change the API in the future.

  4. NicolasHug commented on Dec 14, 2019

    @NicolasHug
    Member

    Let's keep it private for now. Even if the estimators are experimental, it's still not great to make something available and then remove it

  5. NicolasHug commented on Mar 30, 2020

    @NicolasHug
    Member

    @gbolmier might be an easy one?

  6. gbolmier commented on Mar 30, 2020

    @gbolmier
    Contributor

    With pleasure @NicolasHug!

  7. NicolasHug commented on Mar 30, 2020

    @NicolasHug
    Member

    Cool!

    Basically the idea is to allow the loss parameter to be a BaseLoss object. But we keep everything private: this should not be documented or anything.

  8. added a commit that references this issue on Apr 12, 2020
    e4cc10e
  9. NicolasHug commented on Apr 15, 2020

    @NicolasHug
    Member

    @flamby we have implemented support for this and this should be out in the next version. Please keep in mind however that this is part of a private API so no backward compatibility is guaranteed:

    • we might not support this anymore at some point
    • the current loss API is subject to change, which means that you might have to adjust your own the custom losses in the future
  10. Sandy4321 commented on Jun 27, 2021

    @Sandy4321

    people really need it
    https://stackoverflow.com/questions/54267745/implementing-custom-loss-function-in-scikit-learn

    It's tricky, but you can do it...

    1. Open up your classifier. Let's use an RFC for example: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html

    2. click [source]

    3. See how it's inheriting from ForestClassifier? Right there in the class definition. Click that word to jump to it's parent definition.

    4. See how this new object is inheriting from ClassifierMixin? Click that.

    5. See how the bottom of that ClassifierMixin class says this?

    from .metrics import accuracy_score
    return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
    That's your model being trained on accuracy. You need to inject at this point if you want to train your model to be a "recall model" or a "precision model" or whatever model. This accuracy metric is baked into SKlearn. Some day, a better man than I will make this a parameter which models accept, however in the mean time, you gotta go into your sklearn installation, and tweak this accuracy_score to be whatever you want.

  11. NicolasHug commented on Jun 28, 2021

    @NicolasHug
    Member

    Hi @Sandy4321 , a few things:

    Please avoid posting the same post in 3 different places in the repo. You also posted in #16908 (comment) and in #17659 (comment). If you have a question or something cool to show, please open a new issue or a new discussion

    is it possible to add custom loss function to classification tree as well?

    You mean regular DecisionTreeClassifier here. They don't use loss functions, they just have a splitting criterion. It's not possible to define a custom splitting criterion for those trees: #17436

    This has absolutely nothing to do with the loss of gradient boosting estimators, which is what this issue was about.

    Also, the instructions you copy pasted from stack overflow are completely wrong (the question of OP itself is underspecified). The fact that accuracy score is used in ClassifierMixins is just because by default models will be scored with accuracy, or evaluated with that, but never trained with accuracy.
    Also you don't need to go deep in the skleran code to change the accuracy scorer. Everywhere where it's relevant, sklearn tools like GridSearchCV will accept a "scoring" parameter which will allow to replace accuracy_score with an arbitrary callable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions