From 2316f8afad6e89581ee6cc450b4b6cc16772b338 Mon Sep 17 00:00:00 2001 From: fis Date: Sat, 10 Oct 2020 16:24:50 +0800 Subject: [PATCH] Reviewers' comments. --- demo/guide-python/callbacks.py | 2 +- doc/python/callbacks.rst | 8 ++++---- python-package/xgboost/training.py | 6 ++++-- tests/python/test_callback.py | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/demo/guide-python/callbacks.py b/demo/guide-python/callbacks.py index 969d97fd8a0f..4e5d8b6bac00 100644 --- a/demo/guide-python/callbacks.py +++ b/demo/guide-python/callbacks.py @@ -97,7 +97,7 @@ def check(as_pickle): # Check point to a temporary directory for demo with tempfile.TemporaryDirectory() as tmpdir: # Use callback class from xgboost.callback - # Feel free to subclass/customize it to suite your need. + # Feel free to subclass/customize it to suit your need. check_point = xgb.callback.TrainingCheckPoint(directory=tmpdir, rounds=rounds, name='model') diff --git a/doc/python/callbacks.rst b/doc/python/callbacks.rst index 1c46e4a01f1d..009b4d742fe5 100644 --- a/doc/python/callbacks.rst +++ b/doc/python/callbacks.rst @@ -4,7 +4,7 @@ Callback Functions This document gives a basic walkthrough of callback function used in XGBoost Python package. In XGBoost 1.3, a new callback interface is designed for Python package, which -provides the flexiablity of designing various extension for training. Also, XGBoost has a +provides the flexiblity of designing various extension for training. Also, XGBoost has a number of pre-defined callbacks for supporting early stopping, checkpoints etc. ####################### @@ -30,11 +30,11 @@ this callback function directly into XGBoost: r[gt] = 1 - label[gt] le = predt <= 0.5 r[le] = label[le] - return 'PyError', np.sum(r) + return 'CustomErr', np.sum(r) # Specify which dataset and which metric should be used for early stopping. early_stop = xgb.callback.EarlyStopping(rounds=early_stopping_rounds, - metric_name='PyError', + metric_name='CustomErr', data_name='Train') booster = xgb.train( @@ -48,7 +48,7 @@ this callback function directly into XGBoost: verbose_eval=False) dump = booster.get_dump(dump_format='json') - assert len(early_stop.stopping_history['Valid']['PyError']) == len(dump) + assert len(early_stop.stopping_history['Valid']['CustomErr']) == len(dump) ########################## Defining your own callback diff --git a/python-package/xgboost/training.py b/python-package/xgboost/training.py index 7bf5268b31c1..c9aee40082c2 100644 --- a/python-package/xgboost/training.py +++ b/python-package/xgboost/training.py @@ -12,6 +12,8 @@ def _configure_deprecated_callbacks( verbose_eval, early_stopping_rounds, maximize, start_iteration, num_boost_round, feval, evals_result, callbacks, show_stdv, cvfolds): + link = 'https://xgboost.readthedocs.io/en/latest/python/callbacks.html' + raise DeprecationWarning(f'Old style callback is deprecated. See: {link}') # Most of legacy advanced options becomes callbacks if early_stopping_rounds is not None: callbacks.append(callback.early_stop(early_stopping_rounds, @@ -85,7 +87,7 @@ def _train_internal(params, dtrain, is_new_callback = _is_new_callback(callbacks) if is_new_callback: assert all(isinstance(c, callback.TrainingCallback) - for c in callbacks), "You can't mix two styles of callbacks." + for c in callbacks), "You can't mix new and old callback styles." if verbose_eval: callbacks.append(callback.EvaluationMonitor()) if early_stopping_rounds: @@ -478,7 +480,7 @@ def cv(params, dtrain, num_boost_round=10, nfold=3, stratified=False, folds=None is_new_callback = _is_new_callback(callbacks) if is_new_callback: assert all(isinstance(c, callback.TrainingCallback) - for c in callbacks), "You can't mix two styles of callbacks." + for c in callbacks), "You can't mix new and old callback styles." if isinstance(verbose_eval, bool) and verbose_eval: callbacks.append(callback.EvaluationMonitor(show_stdv=show_stdv)) if early_stopping_rounds: diff --git a/tests/python/test_callback.py b/tests/python/test_callback.py index dec719b8c8bf..09d90f7e04c1 100644 --- a/tests/python/test_callback.py +++ b/tests/python/test_callback.py @@ -74,7 +74,7 @@ def test_early_stopping_customize(self): D_valid = xgb.DMatrix(self.X_valid, self.y_valid) early_stopping_rounds = 5 early_stop = xgb.callback.EarlyStopping(rounds=early_stopping_rounds, - metric_name='PyError', + metric_name='CustomErr', data_name='Train') # Specify which dataset and which metric should be used for early stopping. booster = xgb.train( @@ -88,7 +88,7 @@ def test_early_stopping_customize(self): verbose_eval=False) dump = booster.get_dump(dump_format='json') assert len(dump) - booster.best_iteration == early_stopping_rounds + 1 - assert len(early_stop.stopping_history['Train']['PyError']) == len(dump) + assert len(early_stop.stopping_history['Train']['CustomErr']) == len(dump) def test_early_stopping_skl(self): from sklearn.datasets import load_breast_cancer