seed¶
- delu.random.seed(base_seed: int | None, /, *, one_cuda_seed: bool = False) int [source]¶
Set diverse global random seeds in
random
,numpy
andtorch
.Note
For all libraries, different deterministically computed (based on the
base_seed
argument) seeds are set to ensure that different libraries and (by default) devices generate diverse random numbers.Usage
>>> import random >>> import numpy as np >>> >>> def f(): ... return ( ... random.randint(0, 10 ** 9), ... np.random.rand(10).tolist(), ... torch.randn(20).tolist(), ... ) ... >>> # Numbers sampled under the same random seed are equal. >>> delu.random.seed(0) >>> a = f() >>> delu.random.seed(0) >>> b = f() >>> a == b True
Pass
None
to set a truly random seed generated by the OS:>>> # Save the generated `seed` for future reproducibility: >>> seed = delu.random.seed(None) >>> a = f() >>> # Reproduce the results: >>> delu.random.seed(seed) >>> b = f() >>> a == b True
- Parameters:
base_seed – an integer from
[0, 2**64)
used to compute diverse seeds for all libraries. IfNone
, then an unpredictable seed generated by OS is used and returned.one_cuda_seed – if
True
, then the same seed will be set for all CUDA devices, otherwise, different seeds will be set for all CUDA devices.
- Returns:
the provided
base_seed
or the generated one ifbase_seed=None
.