Add tolerance to early stopping. (#6942)

This commit is contained in:
Jiaming Yuan
2021-05-14 00:19:51 +08:00
committed by GitHub
parent 894e9bc5d4
commit d245bc891e
2 changed files with 65 additions and 10 deletions

View File

@@ -126,6 +126,27 @@ class TestCallbacks:
assert len(dump) - booster.best_iteration == early_stopping_rounds + 1
assert len(early_stop.stopping_history['Train']['CustomErr']) == len(dump)
# test tolerance, early stop won't occur with high tolerance.
tol = 10
rounds = 100
early_stop = xgb.callback.EarlyStopping(
rounds=early_stopping_rounds,
metric_name='CustomErr',
data_name='Train',
abs_tol=tol
)
booster = xgb.train(
{'objective': 'binary:logistic',
'eval_metric': ['error', 'rmse'],
'tree_method': 'hist'}, D_train,
evals=[(D_train, 'Train'), (D_valid, 'Valid')],
feval=tm.eval_error_metric,
num_boost_round=rounds,
callbacks=[early_stop],
verbose_eval=False)
# 0 based index
assert booster.best_iteration == rounds - 1
def test_early_stopping_skl(self):
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)