optuna: The random seed fixing doesn't work in a proper way when n_jobs > 1

Expected behavior

Thank you for checking this report.

I found a bug in fixing random seed when the n_jobs of optuna is more than 2.

Of course, there was no bugs with it bug if the n_job = 1 . As it always.

I attached the results both for n_jobs (1 and 2) in Error message column below to show what happended.

Thank you in advance to fix this bug.

Environment

  • Optuna version: 3.1.0
  • Python version: 3.9.15 (anaconda3-2022.05)
  • OS: Linux
  • (Optional) Other libraries and their versions:

Error messages, stack traces, or logs

# First Time for n_jobs = 1
[I 2023-02-09 16:05:35,704] A new study created in memory with name: no-name-24071d83-4971-48c1-8cb5-2da534fafc58
[I 2023-02-09 16:05:35,706] Trial 0 finished with value: 6.296072711533571 and parameters: {'x': -2.50919762305275}. Best is trial 0 with value: 6.296072711533571.
[I 2023-02-09 16:05:35,707] Trial 1 finished with value: 81.25735440102869 and parameters: {'x': 9.014286128198322}. Best is trial 0 with value: 6.296072711533571.
[I 2023-02-09 16:05:35,708] Trial 2 finished with value: 21.52847561487744 and parameters: {'x': 4.639878836228101}. Best is trial 0 with value: 6.296072711533571.
[I 2023-02-09 16:05:35,708] Trial 3 finished with value: 3.8933986016227684 and parameters: {'x': 1.973169683940732}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:05:35,709] Trial 4 finished with value: 47.329270289227914 and parameters: {'x': -6.87962719115127}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:05:35,710] Trial 5 finished with value: 47.33590801548771 and parameters: {'x': -6.880109593275947}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:05:35,711] Trial 6 finished with value: 78.11603753372253 and parameters: {'x': -8.83832775663601}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:05:35,712] Trial 7 finished with value: 53.63398789383462 and parameters: {'x': 7.323522915498703}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:05:35,713] Trial 8 finished with value: 4.0896982399317 and parameters: {'x': 2.0223002348641756}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:05:35,713] Trial 9 finished with value: 17.317679052276553 and parameters: {'x': 4.161451555920909}. Best is trial 3 with value: 3.8933986016227684.

# Second Time for n_jobs = 1
[I 2023-02-09 16:00:20,454] A new study created in memory with name: no-name-6411c615-210f-42ac-bf2d-0e6283644376
[I 2023-02-09 16:00:20,457] Trial 0 finished with value: 6.296072711533571 and parameters: {'x': -2.50919762305275}. Best is trial 0 with value: 6.296072711533571.
[I 2023-02-09 16:00:20,458] Trial 1 finished with value: 81.25735440102869 and parameters: {'x': 9.014286128198322}. Best is trial 0 with value: 6.296072711533571.
[I 2023-02-09 16:00:20,460] Trial 2 finished with value: 21.52847561487744 and parameters: {'x': 4.639878836228101}. Best is trial 0 with value: 6.296072711533571.
[I 2023-02-09 16:00:20,461] Trial 3 finished with value: 3.8933986016227684 and parameters: {'x': 1.973169683940732}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:00:20,462] Trial 4 finished with value: 47.329270289227914 and parameters: {'x': -6.87962719115127}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:00:20,463] Trial 5 finished with value: 47.33590801548771 and parameters: {'x': -6.880109593275947}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:00:20,463] Trial 6 finished with value: 78.11603753372253 and parameters: {'x': -8.83832775663601}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:00:20,464] Trial 7 finished with value: 53.63398789383462 and parameters: {'x': 7.323522915498703}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:00:20,465] Trial 8 finished with value: 4.0896982399317 and parameters: {'x': 2.0223002348641756}. Best is trial 3 with value: 3.8933986016227684.
[I 2023-02-09 16:00:20,466] Trial 9 finished with value: 17.317679052276553 and parameters: {'x': 4.161451555920909}. Best is trial 3 with value: 3.8933986016227684.


# But when the n_jobs = 2
# First Time for n_jobs = 2
[I 2023-02-09 16:07:22,587] A new study created in memory with name: no-name-8c551aac-f45d-4d1f-b7d3-b8d069314041
[I 2023-02-09 16:07:22,590] Trial 0 finished with value: 0.010853259034042183 and parameters: {'x': 0.10417897596944492}. Best is trial 0 with value: 0.010853259034042183.
[I 2023-02-09 16:07:22,592] Trial 1 finished with value: 50.942833667262015 and parameters: {'x': -7.137424862459991}. Best is trial 0 with value: 0.010853259034042183.
[I 2023-02-09 16:07:22,594] Trial 2 finished with value: 3.598699001758317 and parameters: {'x': 1.8970237219809132}. Best is trial 0 with value: 0.010853259034042183.
[I 2023-02-09 16:07:22,596] Trial 3 finished with value: 71.26651946913249 and parameters: {'x': 8.441949980255302}. Best is trial 0 with value: 0.010853259034042183.
[I 2023-02-09 16:07:22,598] Trial 4 finished with value: 7.833086514727638 and parameters: {'x': 2.7987651767748645}. Best is trial 0 with value: 0.010853259034042183.
[I 2023-02-09 16:07:22,599] Trial 5 finished with value: 78.01911118530657 and parameters: {'x': -8.832842757872834}. Best is trial 0 with value: 0.010853259034042183.
[I 2023-02-09 16:07:22,601] Trial 6 finished with value: 0.7034046942656792 and parameters: {'x': -0.8386922524178217}. Best is trial 0 with value: 0.010853259034042183.
[I 2023-02-09 16:07:22,603] Trial 7 finished with value: 29.554591150539665 and parameters: {'x': 5.436413445511633}. Best is trial 0 with value: 0.010853259034042183.
[I 2023-02-09 16:07:22,604] Trial 8 finished with value: 66.23378953500544 and parameters: {'x': -8.13841443617892}. Best is trial 0 with value: 0.010853259034042183.
[I 2023-02-09 16:07:22,607] Trial 9 finished with value: 1.8212410293730235 and parameters: {'x': -1.3495336340280755}. Best is trial 0 with value: 0.010853259034042183.

# Second Time for n_jobs = 2
[I 2023-02-09 16:07:23,369] A new study created in memory with name: no-name-38cd0947-7e25-45ab-b570-79ab74b1186c
[I 2023-02-09 16:07:23,373] Trial 0 finished with value: 0.0004341242080175366 and parameters: {'x': 0.020835647530555335}. Best is trial 0 with value: 0.0004341242080175366.
[I 2023-02-09 16:07:23,376] Trial 1 finished with value: 61.3001487111207 and parameters: {'x': 7.829441149349083}. Best is trial 0 with value: 0.0004341242080175366.
[I 2023-02-09 16:07:23,378] Trial 2 finished with value: 65.33010984277928 and parameters: {'x': -8.082704364430217}. Best is trial 0 with value: 0.0004341242080175366.
[I 2023-02-09 16:07:23,381] Trial 3 finished with value: 9.902102174116182 and parameters: {'x': -3.1467605841748085}. Best is trial 0 with value: 0.0004341242080175366.
[I 2023-02-09 16:07:23,384] Trial 4 finished with value: 34.163470837582715 and parameters: {'x': 5.844952594981649}. Best is trial 0 with value: 0.0004341242080175366.
[I 2023-02-09 16:07:23,387] Trial 5 finished with value: 72.37110229029717 and parameters: {'x': -8.50712068154068}. Best is trial 0 with value: 0.0004341242080175366.
[I 2023-02-09 16:07:23,392] Trial 6 finished with value: 55.76749798366058 and parameters: {'x': -7.4677639212592}. Best is trial 0 with value: 0.0004341242080175366.
[I 2023-02-09 16:07:23,395] Trial 7 finished with value: 0.48307421480677265 and parameters: {'x': -0.6950354054339769}. Best is trial 0 with value: 0.0004341242080175366.
[I 2023-02-09 16:07:23,398] Trial 8 finished with value: 22.40928268619518 and parameters: {'x': -4.733844387619346}. Best is trial 0 with value: 0.0004341242080175366.
[I 2023-02-09 16:07:23,400] Trial 9 finished with value: 83.41232895534615 and parameters: {'x': 9.133035035263259}. Best is trial 0 with value: 0.0004341242080175366.

Steps to reproduce

  1. I used an example code in the official tutorial of optuna
  2. just changing the number of n_jobs is enough to reproduce

Here is the example code.

Python code

import optuna


def objective(trial):
    x = trial.suggest_float("x", -10, 10)
    return x**2


sampler = optuna.samplers.TPESampler(seed = 42)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=10, n_jobs=1)

Additional context (optional)

No response

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

@idssplab Your problem is not related to this issue because n_jobs is not specified, which means n_jobs=1. For your question, please use https://github.com/optuna/optuna/discussions.

Anyway, I suppose reproducibility pages written in PyTorch lightning or PyTorch would be helpful for your problem.

@nzw0301 Got it! Thanks!

Until 10 trials are completed, TPESampler lets RandomSampler handle the sampling. Thus, up to the first 10+α trials, sampling is independent of previous trials. After that, sampling depends on the history, so the randomness of the execution time affects the result.