optuna / optuna-examples

Examples for https://github.com/optuna/optuna
MIT License
634 stars 172 forks source link

optuna.integration.XGBoostPruningCallback is not xgboost TrainingCallback #203

Closed anhailing closed 9 months ago

anhailing commented 9 months ago

Code downloaded at https://github.com/optuna/optuna-examples/blob/main/xgboost/xgboost_integration.py errored out during execution.

Environment

Error messages, stack traces, or logs

[Running] C:\Users\hahy\Miniconda3\envs\ml\python.exe -u "c:\Users\hahy\examples\xgboost_integration.py"
[I 2023-09-15 17:24:25,730] A new study created in memory with name: no-name-d56dde96-9338-4236-ad38-c39f799c7fdb
[W 2023-09-15 17:24:25,776] Trial 0 failed with parameters: {'booster': 'gbtree', 'lambda': 0.07152539971374029, 'alpha': 0.5186515246328354, 'max_depth': 5, 'eta': 2.503213997637747e-05, 'gamma': 0.001738417857798512, 'grow_policy': 'depthwise'} because of the following error: TypeError('callback must be an instance of `TrainingCallback`.').
Traceback (most recent call last):
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\optuna\study\_optimize.py", line 200, in _run_trial
    value_or_values = func(trial)
                      ^^^^^^^^^^^
  File "c:\Users\hahy\examples\xgboost_integration.py", line 52, in objective
    bst = xgb.train(param, dtrain, evals=[(dvalid, "validation")], callbacks=[pruning_callback])
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\xgboost\core.py", line 729, in inner_f
    return func(**kwargs)
           ^^^^^^^^^^^^^^
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\xgboost\training.py", line 167, in train
    cb_container = CallbackContainer(
                   ^^^^^^^^^^^^^^^^^^
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\xgboost\callback.py", line 139, in __init__
    raise TypeError("callback must be an instance of `TrainingCallback`.")
TypeError: callback must be an instance of `TrainingCallback`.
[W 2023-09-15 17:24:25,780] Trial 0 failed with value None.
Traceback (most recent call last):
  File "c:\Users\hahy\examples\xgboost_integration.py", line 63, in <module>
    study.optimize(objective, n_trials=100)
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\optuna\study\study.py", line 442, in optimize
    _optimize(
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\optuna\study\_optimize.py", line 66, in _optimize
    _optimize_sequential(
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\optuna\study\_optimize.py", line 163, in _optimize_sequential
    frozen_trial = _run_trial(study, func, catch)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\optuna\study\_optimize.py", line 251, in _run_trial
    raise func_err
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\optuna\study\_optimize.py", line 200, in _run_trial
    value_or_values = func(trial)
                      ^^^^^^^^^^^
  File "c:\Users\hahy\examples\xgboost_integration.py", line 52, in objective
    bst = xgb.train(param, dtrain, evals=[(dvalid, "validation")], callbacks=[pruning_callback])
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\xgboost\core.py", line 729, in inner_f
    return func(**kwargs)
           ^^^^^^^^^^^^^^
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\xgboost\training.py", line 167, in train
    cb_container = CallbackContainer(
                   ^^^^^^^^^^^^^^^^^^
  File "C:\Users\hahy\Miniconda3\envs\ml\Lib\site-packages\xgboost\callback.py", line 139, in __init__
    raise TypeError("callback must be an instance of `TrainingCallback`.")
TypeError: callback must be an instance of `TrainingCallback`.

Steps to reproduce

  1. code downloaded at https://github.com/optuna/optuna-examples/blob/main/xgboost/xgboost_integration.py

Reproducible examples (optional)

Additional context (optional)

johnchoiniere commented 9 months ago

I'm experiencing the same error, and update somewhere seems to have broken the xgb integration

toshihikoyanase commented 9 months ago

I guess https://github.com/optuna/optuna/pull/4921 resolved this issue, and it will be included in the next release (v3.4).