I am trying yo use optuna and cross_validate from sklearn also using the callback function from optuna. My code is below.
It seems that the callback does not work here and I do not know why … I don’t see the intermediate value when printing it.
The code works but it semms that the code does not call the callback function of Optuna, hence it does not prune any trial. Do you know why ?
# define hy
params = {
'n_estimators' : trial.suggest_categorical('n_estimators', [20, 50, 100, 200]), # Number of boosting rounds i.e. number of trees
'max_depth': trial.suggest_categorical('max_depth', [14, 18, 22, 26, 30, 34, 38, 42, 46, 50]), # max depth of the trees
'learning_rate': trial.suggest_categorical('learning_rate', [0.01, 0.1, 0.3]), # learning rateof the boosting
'gamma': trial.suggest_categorical('gamma',[0, 0.25, 0.5, 1]), # pruning parameter (Minimum loss reduction required to make a further partition on a leaf node of the tree)
'reg_lambda' : trial.suggest_float('reg_lambda', 0, 1, step = 0.1), # penalty : used when the similarity score is computed
'subsample': trial.suggest_float('subsample',0.1,1, step = 0.1), # for each tree, we are using a random subset of the data (% of rows)
'colsample_bytree': trial.suggest_float('colsample_bytree',0.1,1, step = 0.1), # for each tree, we sample using a sample of the columns (% of columns used)
'min_child_weight' : 0, # also named cover --> minimum number of residuals in a leaf
'eval_metric': 'pre', # metric to monitor for early stopping
}
pruning_callback = optuna.integration.XGBoostPruningCallback(trial, "test-pre")
xgb_model = xgb.XGBClassifier(**params, random_state=10, n_jobs = -1, callback = [pruning_callback]) # XGBRegressor if regression
scores = cross_validate(xgb_model, X_train, y_train, verbose=1,
n_jobs=-1, scoring="precision", cv=4,)
return scores["test_score"].mean()
Thank for your support.