delu.improve_reproducibility#

delu.improve_reproducibility(base_seed, one_cuda_seed=False)[source]#

Set seeds in random, numpy and torch and make some cuDNN operations deterministic.

Do everything possible to improve reproducibility for code that relies on global random number generators from the aforementioned modules. See also the note below.

Sets:

  1. seeds in random, numpy.random, torch, torch.cuda

  2. torch.backends.cudnn.benchmark to False

  3. torch.backends.cudnn.deterministic to True

Parameters
Returns

if base_seed is set to None, the generated base seed is

returned; otherwise, base_seed is returned as is

Return type

base_seed

Note

If you don’t want to choose the base seed, but still want to have a chance to reproduce things, you can use the following pattern:

print('Seed:', delu.improve_reproducibility(None))

Note

100% reproducibility is not always possible in PyTorch. See this page for details.

Examples

assert delu.improve_reproducibility(0) == 0
seed = delu.improve_reproducibility(None)