我有下面的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:ProgramDataAnaconda3envsJaneStreetlibsite-packagesoptuna_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'
非常感谢
问题是在类Trial
上调用suggest_int
,就好像它是一个类/静态方法一样。suggest_int
是一个常规方法,应该在对象上调用,在本例中为trial
。将Trial.suggest_int
更改为trial.suggest_int
应该可以消除该错误。
下面是怎么回事。我只是在目标后更改了参数,并将审判改为审判。
def objective(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
}
"n_estimators" : trial.suggest_int("n_estimators", 0, 1000, 20)
,其中
- 0是起始范围
- 1000是结束范围,并且
- 20是阶差
您已经使用Optuna混合了一些东西。在我们的情况下,不需要使用字典,因为您已经创建了objective
函数。
你的功能应该是这样的:
def objective(trial):
model = XGBClassifier(
n_estimators=trial.suggest_int("n_estimators", 0, 1000),
max_depth=trial.suggest_int('max_depth', 2, 25)
)
...
而且,你还必须像这样更改method
的调用:
study = optuna.create_study(direction='maximize',sampler=TPESampler())
study.optimize(objective, n_trials=50)
它应该起作用!
在Optuna中,当您使用integration
模块中的类时,您需要实现一个字典,该模块将Optuna与外部机器学习框架集成在一起。
最后,您的错误是由于您的实现造成的,我不知道确切的原因,但您传递给suggest_int
的第一个参数实际上是self
。所以你应该像我的例子一样使用代码。