0

我有以下 Optuna 代码来为 Xgboost 分类器进行超参数调整。

import optuna 
from optuna import Trial, visualization
from optuna.samplers import TPESampler
from xgboost import XGBClassifier

def objective(trial: Trial,X_train,y_train,X_test,y_test):
    
    param = {
            "n_estimators" : Trial.suggest_int("n_estimators", 0, 1000),
            'max_depth':Trial.suggest_int('max_depth', 2, 25),
            'reg_alpha':Trial.suggest_int('reg_alpha', 0, 5),
            'reg_lambda':Trial.suggest_int('reg_lambda', 0, 5),
            'min_child_weight':Trial.suggest_int('min_child_weight', 0, 5),
            'gamma':Trial.suggest_int('gamma', 0, 5),
            'learning_rate':Trial.suggest_loguniform('learning_rate',0.005,0.5),
            'colsample_bytree':Trial.suggest_discrete_uniform('colsample_bytree',0.1,1,0.01),
            'nthread' : -1
            }
    
    model = XGBClassifier(**param)

    model.fit(X_train,y_train)

    return cross_val_score(model,X_test,y_test).mean()

study = optuna.create_study(direction='maximize',sampler=TPESampler())
study.optimize(lambda trial : objective(trial,X_train,y_train,X_test,y_test),n_trials= 50)

它不断给我以下错误:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\JaneStreet\lib\site-packages\optuna\_optimize.py", line 217, in _run_trial
    value_or_values = func(trial)
  File "<ipython-input-74-c1454daaa53e>", line 2, in <lambda>
    study.optimize(lambda trial : objective(trial,X_train,y_train,X_test,y_test),n_trials= 50)
  File "<ipython-input-73-4438e1db47ef>", line 4, in objective
    "n_estimators" : Trial.suggest_int("n_estimators", 0, 1000),
TypeError: suggest_int() missing 1 required positional argument: 'high'

非常感谢

4

1 回答 1

0

问题是您正在调用suggest_int该类Trial,就好像它是一个类/静态方法一样。suggest_int是一个常规方法,应该在对象上调用,在这种情况下是trial. 更改Trial.suggest_inttrial.suggest_int应该摆脱错误。

于 2021-04-22T00:38:06.910 回答