clinicadl.utils.seed.seed_everything

clinicadl.utils.seed.seed_everything(seed: int | None = None, deterministic: bool = False) None[source]

To control reproducibility.

It will seed pseudo-random number generators in: PyTorch, Numpy and Python’s random module. The seed can be accessed via the environment variable "CLINICADL_GLOBAL_SEED".

Besides, if deterministic=True, PyTorch’s operations will be configured in deterministic mode, to the extent possible. In this case, an environment variable "CLINICADL_DETERMINISTIC" will also be created.

Warning

deterministic=True

  • does not guarantee fully reproducible results; it only ensures determinism within PyTorch’s current limitations;

  • comes with a cost in computing performances. It is advised to use this parameter only for your final experiments.

Parameters:
  • seed (Optional[int], default=None) – The seed to use. If None, a random seed will be generated.

  • deterministic (bool, default=False) – Whether to configure PyTorch’s operations in deterministic mode.

Examples

from clinicadl.utils.seed import seed_everything
import torch
import numpy as np
import random
>>> seed_everything(0)
>>> torch.randn(1), np.random.randn(), random.randint(0, 100)
(tensor([1.5410]), 1.764052345967664, 49)
>>> seed_everything(0)
>>> torch.randn(1), np.random.randn(), random.randint(0, 100)
(tensor([1.5410]), 1.764052345967664, 49)