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

[backport] Fix for save_best. #6523

Merged
merged 1 commit into from Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion python-package/xgboost/callback.py
Expand Up @@ -456,6 +456,7 @@ def __init__(self, learning_rates):

def after_iteration(self, model, epoch, evals_log):
model.set_param('learning_rate', self.learning_rates(epoch))
return False


# pylint: disable=too-many-instance-attributes
Expand Down Expand Up @@ -565,7 +566,7 @@ def after_iteration(self, model: Booster, epoch, evals_log):
def after_training(self, model: Booster):
try:
if self.save_best:
model = model[: int(model.attr('best_iteration'))]
model = model[: int(model.attr('best_iteration')) + 1]
except XGBoostError as e:
raise XGBoostError('`save_best` is not applicable to current booster') from e
return model
Expand Down Expand Up @@ -677,6 +678,7 @@ def after_iteration(self, model, epoch, evals_log):
else:
model.save_model(path)
self._epoch += 1
return False


class LegacyCallbacks:
Expand Down
2 changes: 1 addition & 1 deletion tests/python/test_callback.py
Expand Up @@ -148,7 +148,7 @@ def test_early_stopping_save_best_model(self):
eval_metric=tm.eval_error_metric, callbacks=[early_stop])
booster = cls.get_booster()
dump = booster.get_dump(dump_format='json')
assert len(dump) == booster.best_iteration
assert len(dump) == booster.best_iteration + 1

early_stop = xgb.callback.EarlyStopping(rounds=early_stopping_rounds,
save_best=True)
Expand Down