关闭Optuna Training中的Warning



我完全意识到我可能会因为错过一些明显的东西而感到尴尬,但这让我难倒了。我正在使用Optuna调优LGBM模型,我的笔记本上充斥着警告消息,我该如何抑制它们留下错误(理想情况下是试验结果)?下列代码

import optuna
import sklearn
optuna.logging.set_verbosity(optuna.logging.ERROR)
import warnings
warnings.filterwarnings('ignore')
def objective(trial):    
list_bins = [25, 50, 75, 100, 125, 150, 175, 200, 225, 250,500,750,1000]   
param = {
'lambda_l1': trial.suggest_loguniform('lambda_l1', 1e-8, 10.0),
'lambda_l2': trial.suggest_loguniform('lambda_l2', 1e-8, 10.0),
'colsample_bytree': trial.suggest_categorical('colsample_bytree', [0.3,0.4,0.5,0.6,0.7,0.8,0.9, 1.0]),
'subsample': trial.suggest_categorical('subsample', [0.4,0.5,0.6,0.7,0.8,1.0]),
'learning_rate': trial.suggest_categorical('learning_rate', [0.006,0.008,0.01,0.014,0.017,0.02,0.05]),
'max_depth': trial.suggest_categorical('max_depth', [10,20,50,100]),
'num_leaves' : trial.suggest_int('num_leaves', 2, 1000),
'feature_fraction': trial.suggest_uniform('feature_fraction', 0.1, 1.0),
'bagging_fraction': trial.suggest_uniform('bagging_fraction', 0.1, 1.0),
'bagging_freq': trial.suggest_int('bagging_freq', 1, 15),
'min_child_samples': trial.suggest_int('min_child_samples', 1, 300),
'cat_smooth' : trial.suggest_int('cat_smooth', 1, 256),
'cat_l2' : trial.suggest_int('cat_smooth', 1, 256),
'max_bin': trial.suggest_categorical('max_bin', list_bins)
}

model = LGBMRegressor(**param,objective='regression',metric= 'rmse',boosting_type='gbdt',verbose=-1,random_state=42,n_estimators=20000,cat_feature= [x for x in range(len(cat_features))])


model.fit(X_train, y_train,eval_set=[(X_test,y_test)], early_stopping_rounds=200,verbose=False)

preds = model.predict(X_test)

rmse = mean_squared_error(y_test, preds,squared=False)

return rmse

study = optuna.create_study(direction="minimize")
study.optimize(objective, n_trials=300)
print("Number of finished trials: {}".format(len(study.trials)))
print("Best trial:")
trial = study.best_trial
print("  Value: {}".format(trial.value))
print("  Params: ")
for key, value in trial.params.items():
print("    {}: {}".format(key, value))

我要最小化的是这个

[LightGBM] [Warning] feature_fraction is set=0.7134336417771784, colsample_bytree=0.4 will be ignored. Current value: feature_fraction=0.7134336417771784
[LightGBM] [Warning] lambda_l1 is set=0.0001621506831365743, reg_alpha=0.0 will be ignored. Current value: lambda_l1=0.0001621506831365743
[LightGBM] [Warning] bagging_fraction is set=0.8231149550002105, subsample=0.5 will be ignored. Current value: bagging_fraction=0.8231149550002105
[LightGBM] [Warning] bagging_freq is set=4, subsample_freq=0 will be ignored. Current value: bagging_freq=4
[LightGBM] [Warning] lambda_l2 is set=0.00010964883369301453, reg_lambda=0.0 will be ignored. Current value: lambda_l2=0.00010964883369301453
[LightGBM] [Warning] feature_fraction is set=0.3726043373358532, colsample_bytree=0.3 will be ignored. Current value: feature_fraction=0.3726043373358532
[LightGBM] [Warning] lambda_l1 is set=1.4643061619613147, reg_alpha=0.0 will be ignored. Current value: lambda_l1=1.4643061619613147

我知道这是一个迟到的回应,但我最近有一个类似的问题使用Optuna与XGBoost,我能够关闭警告与simplefilter这样:

from warnings import simplefilter
simplefilter("ignore", category=RuntimeWarning)

我看到你已经使用警告模块忽略,我还没有这样做,但simplefilter为我工作。

你应该通过' verbose ': -1"在字典'params'中,稍后将传递给lightbam .train()。此外,还需要将'verbose_eval=False'传递给lightgbam .train()。

:

params=
{... 
'verbosity': -1
} 
gbm = lgbm.train(
params, 
...
verbose_eval=False, 
...)

Optuna基本上是在告诉您,您已经传递了参数的别名,因此默认的参数名称和值被忽略。如果将参数名称设置为默认名称,则不会收到这些警告。例如,将feature_fraction替换为colsample_bytree将lambda_l1替换为reg_alpha,依此类推。

请注意,您也传递了重复的参数。

  1. 子样本和baging_fraction
  2. colsample_bytree和feature_fraction等

如果你阅读这里链接的文档:https://optuna.readthedocs.io/en/stable/faq.html#how-to-suppress-log-messages-of-optuna

您可以看到,在创建研究之前实现这行代码解决了问题:

optuna.logging.set_verbosity(optuna.logging.WARNING)

相关内容

最新更新