Skip to content

Commit

Permalink
Fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Dec 17, 2020
1 parent 8be2cd8 commit ec50033
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion python-package/xgboost/callback.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit ec50033

Please sign in to comment.