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

[python-package] mark EarlyStopException as part of public API #6095

Merged
merged 5 commits into from
Sep 13, 2023

Conversation

jameslamb
Copy link
Collaborator

@jameslamb jameslamb commented Sep 13, 2023

Doing some research for another contribution, I found that several large projects rely on importing lightgbm.callback.EarlyStopException, and using it to trigger early stopping in LightGBM.

This PR proposes formally marking that class (and by extension, that mechanism of triggering early stopping) as a part of the public API, to prevent it from being removed or altered in a non-backward-compatible way in the future.

@@ -32,5 +32,5 @@
'train', 'cv',
'LGBMModel', 'LGBMRegressor', 'LGBMClassifier', 'LGBMRanker',
'DaskLGBMRegressor', 'DaskLGBMClassifier', 'DaskLGBMRanker',
'log_evaluation', 'record_evaluation', 'reset_parameter', 'early_stopping',
'log_evaluation', 'record_evaluation', 'reset_parameter', 'early_stopping', 'EarlyStopException',
Copy link
Collaborator Author

@jameslamb jameslamb Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows for imports like

from lightgbm import EarlyStopException

or

import lightgbm as lgb

def _early_stop(env):
    raise lgb.EarlyStopException(
        best_iteration=6,
        best_score=[("some_validation_set", "some_metric", 0.708, True)]
     )

Every other part of the public API can be imported like this, and I think such imports should be encouraged over those that require knowing the internal sub-module structure like from lightgbm.callback import EarlyStopException

@jameslamb
Copy link
Collaborator Author

Thanks @jmoralez !

If you haven't seen it, take a look at those FLAML + autogluon links (and the wandb one in #6096). It was pretty interesting for me to see how other popular projects are using the callback system here to provide supported LightGBM integrations.

@jameslamb jameslamb merged commit 0b3d9da into master Sep 13, 2023
41 checks passed
@jameslamb jameslamb deleted the python/early-stop-exception branch September 13, 2023 18:47
Copy link

This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants