简体   繁体   English

如何使用 optuna 训练 LGBMClassifier

[英]How to train LGBMClassifier using optuna

I am trying to use lgbm with optuna for a classification task.我正在尝试将lgbmoptuna用于分类任务。
Here is my model.这是我的模型。

from optuna.integration import LightGBMPruningCallback
import optuna.integration.lightgbm as lgbm
import optuna

def objective(trial, X_train, y_train, X_test, y_test):
    param_grid = {
        # "device_type": trial.suggest_categorical("device_type", ['gpu']),
        "n_estimators": trial.suggest_categorical("n_estimators", [10000]),
        "learning_rate": trial.suggest_float("learning_rate", 0.01, 0.3, log=True),
        "num_leaves": trial.suggest_int("num_leaves", 20, 3000, step=20),
        "max_depth": trial.suggest_int("max_depth", 3, 12), 
        "min_data_in_leaf": trial.suggest_int("min_data_in_leaf", 100, 10000, step=1000),
        "lambda_l1": trial.suggest_int("lambda_l1", 0, 100, step=5),
        "min_gain_to_split": trial.suggest_float("min_gain_to_split", 0, 15),
        "bagging_fraction": trial.suggest_float(
            "bagging_fraction", 0.2, 0.95, step=0.1
        ),
        "bagging_freq": trial.suggest_categorical("bagging_freq", [1]),
        "feature_fraction": trial.suggest_float(
            "feature_fraction", 0.2, 0.95, step=0.1
        ),
        "max_features": trial.suggest_categorical(
            "max_features", choices=["auto", "sqrt", "log2"]
        ),
        "n_jobs": -1,
        "random_state": 1121218,
    }
    
    model = lgbm.LGBMClassifier(objective="multiclass", **param_grid)
    model.fit(
        X_train,
        y_train,
        eval_set=[(X_test, y_test)],
        eval_metric="multi_logloss",
        early_stopping_rounds=5,
        callbacks=[
            LightGBMPruningCallback(trial, "multi_logloss")
        ],  # Add a pruning callback
    )
    preds = model.predict_proba(X_test)
    return preds, model

I then call the model然后我调用模型

%%time
study = optuna.create_study(direction="maximize", study_name="LGBM Classifier")

func = lambda trial: objective(trial, X_train, y_train, X_test, y_test)

preds, model = study.optimize(func, n_trials=100) 

But I get the following error:但我收到以下错误:

RuntimeError: scikit-learn estimators should always specify their parameters in the signature of their __init__ (no varargs). 
<class 'optuna.integration._lightgbm_tuner.sklearn.LGBMClassifier'> with constructor (self, *args:Any, **kwargs:Any) -> None doesn't  follow this convention.

The understand the error, but I'm not sure what the correct way is to do what I want to do.理解错误,但我不确定正确的方法是做我想做的事。

To solve this issue, you have to specify your hyper-parameters without using variable length arguments.要解决此问题,您必须在不使用可变长度参数的情况下指定超参数。

Could you try to replace this code :您能否尝试替换此代码:

model = lgbm.LGBMClassifier(objective="multiclass", **param_grid)

With :和 :

model = lgbm.LGBMClassifier(objective="multiclass", n_estimators=param_grid["n_estimators"], learning_rate=param_grid["learning_rate"], max_depth=param_grid["max_depth"], min_data_in_leaf=param_grid["min_data_in_leaf"],lambda_l1=param_grid["lambda_l1"],min_gain_to_split=param_grid["min_gain_to_split"],bagging_fraction=param_grid["bagging_fraction"],bagging_freq=param_grid["bagging_freq"],feature_fraction=param_grid["feature_fraction"],max_features=param_grid["max_features"],n_jobs=param_grid["n_jobs"],random_state=param_grid["random_state"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用optuna搜索一组正态分布参数? - How to search a set of normally distributed parameters using optuna? 如何使用optuna或hyperopt调整条件目标函数 - How to tune conditional objective function using optuna or hyperopt 使用 optuna LightGBMTunerCV 作为使用 optuna 进一步搜索的起点 - Using optuna LightGBMTunerCV as starting point for further search with optuna 如何针对 Optuna 中的多个指标进行优化 - How to optimize for multiple metrics in Optuna 我们如何在不使用助推器 object 的情况下使用 optuna 优化 XGBoost 超参数? - How do we optimize XGBoost hyperparameters using optuna without using the booster object? 如何使用 optuna 试验在 sklearn MLPRegressor 中设置 hidden_​​layer_sizes - How to set hidden_layer_sizes in sklearn MLPRegressor using optuna trial 在LGBMClassifier中使用PermutationImportance会导致ValueError:未知标签类型:&#39;continuous&#39; - Using PermutationImportance with LGBMClassifier causes ValueError: Unknown label type: 'continuous' AttributeError: 'tuple' object 在使用 LGBMClassifier 包装器时没有属性 'encode' - AttributeError: 'tuple' object has no attribute 'encode' when using LGBMClassifier wrapper RandomizedSearchCV 中的 LGBMClassifier:best_params_ 使用 eval_metric 还是评分? - LGBMClassifier in RandomizedSearchCV: best_params_ using eval_metric or scoring? 使用 Pytorch 和 Optuna 进行 DNN 的结果重现性 - Results reproducibility using Pytorch and Optuna for DNN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM