我目前正在使用 optuna,我注意到当我使用 n_jobs = -1 时,TPESampler 不会为不同的研究采样完全相同的参数,即使
optuna.samplers.TPESampler(seed = 10)
内部的种子是固定的。看来,结合 optuna 的多重处理并获得确定性结果是不兼容的。我确实需要我的结果是可重现的,但我还需要 n_jobs = -1,否则代码将花费很多时间。我完全知道使用 n_jobs = 1 将消除这个问题,但正如我所说,我不想使用它,因为我的时间有限。
你们有人能解决这个问题吗?
代码:
study1 = optuna.create_study(direction="maximize", sampler=optuna.samplers.TPESampler(seed = 10))
study1.optimize(objective, n_trials=5, n_jobs = -1)
study2 = optuna.create_study(direction="maximize", sampler=optuna.samplers.TPESampler(seed = 10))
study2.optimize(objective, n_trials=5, n_jobs = -1)
非常感谢 stackoverflow 社区:)!
当
n_jobs != 1
时,optuna 在内部重置每个线程的采样器种子:https://github.com/optuna/optuna/blob/13dcf9074223942855a4e61516fd5d32629627d2/optuna/study/_optimize.py#L136-L137。一种解决方法可能是批量优化,如 https://optuna.readthedocs.io/en/stable/tutorial/20_recipes/009_ask_and_tell.html#batch-optimization 中所述。为了重现性,我们需要按每批次 study.tell
的顺序调用 trial.number
。