Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tianlh
LightGBM-DCU
Commits
0b3d9da2
Unverified
Commit
0b3d9da2
authored
Sep 13, 2023
by
James Lamb
Committed by
GitHub
Sep 13, 2023
Browse files
[python-package] mark EarlyStopException as part of public API (#6095)
parent
1a6e6ff9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
3 deletions
+36
-3
python-package/lightgbm/__init__.py
python-package/lightgbm/__init__.py
+2
-2
python-package/lightgbm/callback.py
python-package/lightgbm/callback.py
+7
-1
tests/python_package_test/test_engine.py
tests/python_package_test/test_engine.py
+27
-0
No files found.
python-package/lightgbm/__init__.py
View file @
0b3d9da2
...
...
@@ -6,7 +6,7 @@ Contributors: https://github.com/microsoft/LightGBM/graphs/contributors.
from
pathlib
import
Path
from
.basic
import
Booster
,
Dataset
,
Sequence
,
register_logger
from
.callback
import
early_stopping
,
log_evaluation
,
record_evaluation
,
reset_parameter
from
.callback
import
EarlyStopException
,
early_stopping
,
log_evaluation
,
record_evaluation
,
reset_parameter
from
.engine
import
CVBooster
,
cv
,
train
try
:
...
...
@@ -32,5 +32,5 @@ __all__ = ['Dataset', 'Booster', 'CVBooster', 'Sequence',
'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'
,
'plot_importance'
,
'plot_split_value_histogram'
,
'plot_metric'
,
'plot_tree'
,
'create_tree_digraph'
]
python-package/lightgbm/callback.py
View file @
0b3d9da2
...
...
@@ -12,6 +12,7 @@ if TYPE_CHECKING:
from
.engine
import
CVBooster
__all__
=
[
'EarlyStopException'
,
'early_stopping'
,
'log_evaluation'
,
'record_evaluation'
,
...
...
@@ -30,7 +31,11 @@ _ListOfEvalResultTuples = Union[
class
EarlyStopException
(
Exception
):
"""Exception of early stopping."""
"""Exception of early stopping.
Raise this from a callback passed in via keyword argument ``callbacks``
in ``cv()`` or ``train()`` to trigger early stopping.
"""
def
__init__
(
self
,
best_iteration
:
int
,
best_score
:
_ListOfEvalResultTuples
)
->
None
:
"""Create early stopping exception.
...
...
@@ -39,6 +44,7 @@ class EarlyStopException(Exception):
----------
best_iteration : int
The best iteration stopped.
0-based... pass ``best_iteration=2`` to indicate that the third iteration was the best one.
best_score : list of (eval_name, metric_name, eval_result, is_higher_better) tuple or (eval_name, metric_name, eval_result, is_higher_better, stdv) tuple
Scores for each metric, on each validation set, as of the best iteration.
"""
...
...
tests/python_package_test/test_engine.py
View file @
0b3d9da2
...
...
@@ -1092,6 +1092,33 @@ def test_early_stopping_min_delta(first_only, single_metric, greater_is_better):
assert
np
.
greater_equal
(
last_score
,
best_score
-
min_delta
).
any
()
def
test_early_stopping_can_be_triggered_via_custom_callback
():
X
,
y
=
make_synthetic_regression
()
def
_early_stop_after_seventh_iteration
(
env
):
if
env
.
iteration
==
6
:
exc
=
lgb
.
EarlyStopException
(
best_iteration
=
6
,
best_score
=
[(
"some_validation_set"
,
"some_metric"
,
0.708
,
True
)]
)
raise
exc
bst
=
lgb
.
train
(
params
=
{
"objective"
:
"regression"
,
"verbose"
:
-
1
,
"num_leaves"
:
2
},
train_set
=
lgb
.
Dataset
(
X
,
label
=
y
),
num_boost_round
=
23
,
callbacks
=
[
_early_stop_after_seventh_iteration
]
)
assert
bst
.
num_trees
()
==
7
assert
bst
.
best_score
[
"some_validation_set"
][
"some_metric"
]
==
0.708
assert
bst
.
best_iteration
==
7
assert
bst
.
current_iteration
()
==
7
def
test_continue_train
():
X
,
y
=
make_synthetic_regression
()
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.1
,
random_state
=
42
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment