gpt4 book ai didi

python - 生成 optuna 网格的函数提供了 sklearn 管道

转载 作者:行者123 更新时间:2023-12-05 05:52:37 25 4
gpt4 key购买 nike

我将 sklearn 与 optuna 一起用于 HPO。我想创建一个自定义函数,它将 sklearn 管道作为输入并返回特定于 optuna 的网格。返回 sklearn 特定的参数网格(即字典)似乎更直接(duh);这是我到目前为止所得到的:

def grid_from_estimator(estimator, type = 'sklearn'):

estimator_name = estimator.named_steps['estimator'].__class__.__name__

if type == 'sklearn':
if estimator_name=='LogisticRegression':
params = {
'estimator__penalty': ['l1','elasticnet'],
'estimator__C': np.logspace(-4, 4, 20)
}
elif estimator_name=='LGBMClassifier':
params = {
'estimator__n_estimators': np.arange(100, 1000, 200),
'estimator__boosting_type':['gbdt','dart'],
'estimator__max_depth': np.arange(6, 12),
'estimator__num_leaves': np.arange(30, 150,5),
'estimator__learning_rate': [1e-2/2 , 1e-2, 1e-1/2, 1e-1, 0.5, 1],
'estimator__min_child_samples': np.arange(20, 100, 5),
'estimator__subsample': np.arange(0.65, 1, 0.05),
'estimator__colsample_bytree': np.arange(0.4, 0.75, 0.05),
'estimator__reg_alpha': [0, 1e-1, 1, 2, 5, 7, 10, 50, 100],
'estimator__reg_lambda': [0, 1e-1, 1, 5, 10, 20, 50, 100],
'estimator__iterations': np.arange(100, 800, 100),
'estimator__objective': 'binary'
}
elif type == 'optuna':
if estimator_name == 'LogisticRegression':
params = {
'estimator__penalty': trial.suggest_categorical('penalty', ['l1', 'elasticnet']),
'estimator__C': trial.suggest.suggest_loguniform('c', -4, 4)
}
elif estimator_name == 'LGBMClassifier':
params = {
'estimator__n_estimators': trial.suggest_int('n_estimators', 100, 1000),
'estimator__boosting_type': trial.suggest_categorical('boosting_type', ['gbdt', 'dart']),
'estimator__max_depth': trial.suggest_int('max_depth', 6, 12),
'estimator__num_leaves': trial.suggest_int('num_leaves', 30, 150, 5),
'estimator__learning_rate': trial.suggest_float('learning_rate', 1e-4, 1),
'estimator__min_child_samples': trial.suggest_int('min_child_samples', 20, 100),
'estimator__subsample': trial.suggest_float('subsample', 0.5, 1),
'estimator__colsample_bytree': trial.suggest_float('colsample_bytree', 0.4, 0.75),
'estimator__reg_alpha': trial.suggest_float('reg_alpha', 1e-2, 10),
'estimator__reg_lambda': trial.suggest_float('reg_lambda', 1e-2, 10)
}

return params

trial.suggest_...”部分不断“提示”并返回错误;虽然我明白其中的原因,但我看不出任何解决办法。这可能吗?有任何想法吗?感谢您的支持!

最佳答案

我认为,这方面的一些事情应该可行,

def grid_from_estimator(estimator, trial, type = 'sklearn'):
pass

def your_objective_function(trial):
params = grid_from_estimator('LogisticRegression', trial, 'optuna')
#Rest of the code here.


def tune_model():
study = optuna.create_study()
study.optimize(your_objective_function, n_trials=20)

tune_model()

关于python - 生成 optuna 网格的函数提供了 sklearn 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70088279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com