簡體   English   中英

LightGBM模型的Optuna超參數優化

[英]Optuna hyperparameter optimization of LightGBM model

我正在使用 Optuna 來調整 LightGBM 模型的超參數。 我建議了一些要優化的超參數的值(使用trail.suggest_int / trial.suggest_float / trial.suggest_loguniform )。

還有一些超參數我設置了一個固定值。 例如我設置feature_fraction = 1 但是,當我運行我的代碼時,我看到 Optuna 正在嘗試測試不同的feature_fraction值。 這不是我想要的,因為它需要太多時間。 我不明白為什么。

代碼:

def objective(trial):   
    param = {
            'objective': 'binary',
            'boosting': 'gbdt', 
            'metric': 'auc', 
            'max_depth':4,
            'seed': 100,
            'feature_fraction': 1,
            'feature_fraction_seed': 100,
            'is_unbalance': True,
            'is_provide_training_metric': True, 
            'extra_trees': True,
            'force_col_wise': True, 
            'numleaves': trial.suggest_int('num_leaves', 10, 30),
            'learning_rate': trial.suggest_float('learning_rate', 1e-4, 1e-2), 
            'min_data_in_leaf': trial.suggest_int('min_data_in_leaf', 10, 80),
            'min_sum_hessianin_leaf':trial.suggest_loguniform('min_sum_hessian_in_leaf', 1e-8, 10.0),
            'verbose': trial.suggest_int('verbose', 1, 5), 
            'max_bin': trial.suggest_int('max_bin', 80, 300),
            'lambda_l1': trial.suggest_loguniform('lambda_l1', 1e-5, 1e-1), 
            'path_smooth':trial.suggest_uniform('path_smooth', 0.4, 1.5)}

    threshold = 0.5

    gbm=lgb.train(param,
                  train_set=1gbtrain,
                  valid_sets=[1gb_val,lgb_train],
                  valid_names = ['eval', 'train'], 
                  verbose_eval=2,
                  early_stopping_rounds=5, 
                  num_boost_round=10)

     y_pred=gbm.predict(X_test)
     pred_labels =(y_pred > threshold).astype(int)
     accuracy=sklearn.metrics.accuracy_score(y_test, pred_labels) 
     return accuracy

study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=5, show_progress_bar = True, gc_after_trial=True)
print('Number of finished trials:', len(study.trials)) 
print('Best trial:', study.best_trial.params)

輸出: 日志顯示它正在嘗試測試不同的 feature_fraction

雖然我不知道 optuna 為feature_fraction嘗試不同值的原因,但您可以嘗試設置默認值,例如

'feature_fraction': trial.suggest_int('feature_fraction', 1, 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM