-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[python] make
early_stopping
callback pickleable (#5012)
* Turn `early_stopping` into a Callable class * Fix * Lint * Remove print * Fix order * Revert "Lint" This reverts commit 7ca8b55. * Apply suggestion from code review * Nit * Lint * Move callable class outside the func for pickling * Move _pickle and _unpickle to tests utils * Add early stopping callback picklability test * Nit * Fix * Lint * Improve type hint * Lint * Lint * Add cloudpickle to test_windows * Update tests/python_package_test/test_engine.py * Fix * Apply suggestions from code review
- Loading branch information
Showing
5 changed files
with
184 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# coding: utf-8 | ||
import pytest | ||
|
||
import lightgbm as lgb | ||
|
||
from .utils import pickle_obj, unpickle_obj | ||
|
||
|
||
@pytest.mark.parametrize('serializer', ["pickle", "joblib", "cloudpickle"]) | ||
def test_early_stopping_callback_is_picklable(serializer, tmp_path): | ||
callback = lgb.early_stopping(stopping_rounds=5) | ||
tmp_file = tmp_path / "early_stopping.pkl" | ||
pickle_obj( | ||
obj=callback, | ||
filepath=tmp_file, | ||
serializer=serializer | ||
) | ||
callback_from_disk = unpickle_obj( | ||
filepath=tmp_file, | ||
serializer=serializer | ||
) | ||
assert callback.stopping_rounds == callback_from_disk.stopping_rounds |
Oops, something went wrong.