optuna/optuna-examples

optuna-examples/xgboost /xgboost_integration.py error

pangjac opened this issue · 1 comments

Expected behavior

Environment

  • Optuna version: 3.2.0
  • Python version:3.8.11
  • OS:
  • NAME="Amazon Linux"
    VERSION="2"
    ID="amzn"
    ID_LIKE="centos rhel fedora"
    VERSION_ID="2"
    PRETTY_NAME="Amazon Linux 2"
    ANSI_COLOR="0;33"
    CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
    HOME_URL="https://amazonlinux.com/"
  • (Optional) Other libraries and their versions:
    XGBoost version: 2.0.0

Error messages, stack traces, or logs

[W 2023-10-25 21:10:08,038] Trial 0 failed with parameters: {'booster': 'gbtree', 'lambda': 4.14282963601177e-07, 'alpha': 0.0001541271370025986, 'max_depth': 6, 'eta': 9.821782417351608e-05, 'gamma': 6.805794394788842e-07, 'grow_policy': 'depthwise'} because of the following error: TypeError('callback must be an instance of `TrainingCallback`.').
Traceback (most recent call last):
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/optuna/study/_optimize.py", line 200, in _run_trial
    value_or_values = func(trial)
  File "xgboost_integration.py", line 42, in objective
    bst = xgb.train(param, dtrain, evals=[(dvalid, "validation")], callbacks=[pruning_callback])
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/xgboost/core.py", line 729, in inner_f
    return func(**kwargs)
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/xgboost/training.py", line 167, in train
    cb_container = CallbackContainer(
  File "/opt/omniai/software/Miniconda/lib/python3.8/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-10-25 21:10:08,048] Trial 0 failed with value None.
Traceback (most recent call last):
  File "xgboost_integration.py", line 53, in <module>
    study.optimize(objective, n_trials=100)
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/optuna/study/study.py", line 443, in optimize
    _optimize(
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/optuna/study/_optimize.py", line 66, in _optimize
    _optimize_sequential(
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/optuna/study/_optimize.py", line 163, in _optimize_sequential
    frozen_trial = _run_trial(study, func, catch)
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/optuna/study/_optimize.py", line 251, in _run_trial
    raise func_err
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/optuna/study/_optimize.py", line 200, in _run_trial
    value_or_values = func(trial)
  File "xgboost_integration.py", line 42, in objective
    bst = xgb.train(param, dtrain, evals=[(dvalid, "validation")], callbacks=[pruning_callback])
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/xgboost/core.py", line 729, in inner_f
    return func(**kwargs)
  File "/opt/omniai/software/Miniconda/lib/python3.8/site-packages/xgboost/training.py", line 167, in train
    cb_container = CallbackContainer(
  File "/opt/omniai/software/Miniconda/lib/python3.8/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. Terminal cd <your_optuna-example_folder>/optuna-examples/xgboost/
  2. In terminal python xgboost_integration.py

Reproducible examples (optional)

# python code

Additional context (optional)

The object pruning_callback is not derived TrainingCallback. if using inspect and print(inspect.getmro(type(pruning_callback))), you will get :
<class 'optuna.integration.xgboost.XGBoostPruningCallback'>, <class 'object'>.

Please feel free to reopen this issue if the problem exists with the latest stable Optuna (3.4.0), which includes a patch for this: optuna/optuna#4921.