zero.improve_reproducibility

zero.improve_reproducibility(seed)[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

seed (Optional[int]) – the seed for all mentioned libraries. Must be less than 2 ** 32 - 1. If None, a high-quality seed is generated instead.

Returns

if seed is set to None, the generated seed is returned;

otherwise, seed is returned as is

Return type

seed

Note

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

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

Note

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

Examples

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