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

[WIP] Add chosen metric argument to clarify early stopping behaviour #6424

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add chosen_metric_early_stopping parameter to create early stop…
…ping callback through parameters
  • Loading branch information
sami-ka committed Apr 20, 2024
commit dbdc17c43de32431acb5e91b18d347d51b6d2694
16 changes: 15 additions & 1 deletion python-package/lightgbm/engine.py
Original file line number Diff line number Diff line change
@@ -191,7 +191,13 @@ def train(
if params["early_stopping_round"] is None:
params.pop("early_stopping_round")
first_metric_only = params.get("first_metric_only", False)

chosen_metric_early_stopping = params.get("chosen_metric_early_stopping", None)
# Test if both parameters are used
if (first_metric_only + (chosen_metric_early_stopping is not None)) == 2:
error_message = """
Only one of first_metric_only and chosen_metric_early_stopping parameters should be used"""
raise ValueError(error_message)

predictor: Optional[_InnerPredictor] = None
if isinstance(init_model, (str, Path)):
predictor = _InnerPredictor.from_model_file(model_file=init_model, pred_parameter=params)
@@ -241,6 +247,7 @@ def train(
callback.early_stopping(
stopping_rounds=params["early_stopping_round"], # type: ignore[arg-type]
first_metric_only=first_metric_only,
chosen_metric=chosen_metric_early_stopping,
verbose=_choose_param_value(
main_param_name="verbosity",
params=params,
@@ -716,6 +723,12 @@ def cv(
if params["early_stopping_round"] is None:
params.pop("early_stopping_round")
first_metric_only = params.get("first_metric_only", False)
chosen_metric_early_stopping = params.get("chosen_metric_early_stopping", None)
# Test if both parameters are used
if (first_metric_only + (chosen_metric_early_stopping is not None)) == 2:
error_message = """
Only one of first_metric_only and chosen_metric_early_stopping parameters should be used"""
raise ValueError(error_message)

if isinstance(init_model, (str, Path)):
predictor = _InnerPredictor.from_model_file(
@@ -765,6 +778,7 @@ def cv(
callback.early_stopping(
stopping_rounds=params["early_stopping_round"], # type: ignore[arg-type]
first_metric_only=first_metric_only,
chosen_metric=chosen_metric_early_stopping,
verbose=_choose_param_value(
main_param_name="verbosity",
params=params,