gpt4 book ai didi

python - Optuna 在大量试验中建议相同的参数值(浪费时间和预算的重复试验)

转载 作者:行者123 更新时间:2023-12-04 15:13:11 24 4
gpt4 key购买 nike

由于某种原因,Optuna TPESampler 和 RandomSampler 对任何参数尝试相同的建议整数值(也可能是浮点数和对数)不止一次。我找不到一种方法来阻止它再次建议相同的值。在 100 次试验中,相当多的试验只是重复。在 100 次试验中,唯一建议值计数最终约为 80-90。如果我包含更多的调整参数,比如说 3,我什至会看到所有 3 个参数在 100 次试验中多次获得相同的值。
就像这样。 75 for min_data_in_leaf 使用了 3 次:
[I 2020-11-14 14:44:05,320] 试验 8 完成值:45910.54012028659 和参数:{'min_data_in_leaf': 75}。最好的是试用 4,其值为:45805.19030897498。
[I 2020-11-14 14:44:07,876] 试验 9 完成值:45910.54012028659 和参数:{'min_data_in_leaf': 75}。最好的是试用 4,其值为:45805.19030897498。
[I 2020-11-14 14:44:10,447] 试验 10 完成,值:45831.75933279074 和参数:{'min_data_in_leaf': 43}。最好的是试用 4,其值为:45805.19030897498。
[I 2020-11-14 14:44:13,502] Trial 11 完成,值:46125.39810101329 和参数:{'min_data_in_leaf': 4}。最好的是试用 4,其值为:45805.19030897498。
[I 2020-11-14 14:44:16,547] Trial 12 完成,值:45910.54012028659 和参数:{'min_data_in_leaf': 75}。最好的是试用 4,其值为:45805.19030897498。
示例代码如下:

def lgb_optuna(trial):

rmse = []

params = {
"seed": 42,
"objective": "regression",
"metric": "rmse",
"verbosity": -1,
"boosting": "gbdt",
"num_iterations": 1000,
'min_data_in_leaf': trial.suggest_int('min_data_in_leaf', 1, 100)
}

cv = StratifiedKFold(n_splits=5, random_state=42, shuffle=False)
for train_index, test_index in cv.split(tfd_train, tfd_train[:,-1]):
X_train, X_test = tfd_train[train_index], tfd_train[test_index]
y_train = X_train[:,-2].copy()
y_test = X_test[:,-2].copy()

dtrain = lgb.Dataset(X_train[:,:-2], label=y_train)
dtest = lgb.Dataset(X_test[:,:-2], label=y_test)

booster_gbm = lgb.train(params, dtrain, valid_sets=dtest, verbose_eval=False)

y_predictions = booster_gbm.predict(X_test[:,:-2])
final_mse = mean_squared_error(y_test, y_predictions)
final_rmse = np.sqrt(final_mse)
rmse.append(final_rmse)

return np.mean(rmse)

study = optuna.create_study(sampler=TPESampler(seed=42), direction='minimize')
study.optimize(lgb_optuna, n_trials=100)

最佳答案

问题是您在此行中指定的采样器:

study = optuna.create_study(sampler=TPESampler(seed=42), direction='minimize')
TPESampler不是一个统一的采样器。这是一个不同的采样器,它试图从有希望的值范围中进行采样。查看详情 herehere .这就是您看到大量重复的原因。对于优化器,它们是有希望的值,然后它们可能会以不同的组合进行进一步探索。
要进行真正的均匀采样,您应该将采样器更改为:
sampler=RandomSampler(seed)
这并不能保证不会有重复,但值会更均匀地分布
如果你想确保你只搜索不同的组合,你应该使用 GridSampler .正如文档中所述:

the trials suggest all combinations of parameters in the given searchspace during the study.


doc here

关于python - Optuna 在大量试验中建议相同的参数值(浪费时间和预算的重复试验),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64836142/

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