optuna / optuna-examples

Examples for https://github.com/optuna/optuna
MIT License
674 stars 176 forks source link

optuna-examples/xgboost /xgboost_integration.py error #222

Closed pangjac closed 11 months ago

pangjac commented 11 months ago

Expected behavior

Environment

[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'>.

nzw0301 commented 11 months ago

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: https://github.com/optuna/optuna/pull/4921.