2

I have the following code of Optuna to do the hyperparameter tunning for a Xgboost classifier.

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)

It keeps giving me the following error:

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'

Thanks so much

Yifan Lyu
  • 43
  • 1
  • 6

4 Answers4

1

The problem is that you are calling suggest_int on the class Trial as if it were a class/static method. suggest_int is a regular method and should be called on an object, in this case trial. Changing Trial.suggest_int to trial.suggest_int should get rid of the error.

hvy
  • 201
  • 1
  • 5
  • I get the error `module optuna.trial has no attribute suggest_int` on optuna v2.10 – PJ_ Aug 11 '22 at 13:39
0

What about below. I just changed the params after objective and changed Trial to trial.

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
            }
J R
  • 436
  • 3
  • 7
0

"n_estimators" : trial.suggest_int("n_estimators", 0, 1000, 20) where

  • 0 is the starting range,
  • 1000 is the ending range, and
  • 20 is the step difference
tuomastik
  • 4,559
  • 5
  • 36
  • 48
0

You have mix several things with Optuna. The use of a dictionary is not necessary in our case, since you have created an objective function.

Your function should be like this:

def objective(trial): 
    
    model = XGBClassifier(
        n_estimators=trial.suggest_int("n_estimators", 0, 1000),
        max_depth=trial.suggest_int('max_depth', 2, 25)
    )
...

And, you have also to change the call of the method like this:

study = optuna.create_study(direction='maximize',sampler=TPESampler())
study.optimize(objective, n_trials=50)

And it should work!

In Optuna you need to implement a dictionary when you use classes from the integration module which integrate Optuna with external machine learning frameworks.

And finally, your error is due of your implementation, I don't know why exactly, but the first argument you pass to suggest_int is in fact self. So you should use the code like in my example.

Adrien Riaux
  • 266
  • 9