noether.core.utils.seed

Attributes

Functions

set_seed(seed)

Set the seed for random number generation for Python random, numpy,

seed_worker(worker_id)

DataLoader worker_init_fn that re-seeds Python random and numpy

Module Contents

noether.core.utils.seed.logger
noether.core.utils.seed.set_seed(seed)

Set the seed for random number generation for Python random, numpy, torch and torch.cuda, if available.

Parameters:

seed (int) – Seed value.

Return type:

None

noether.core.utils.seed.seed_worker(worker_id)

DataLoader worker_init_fn that re-seeds Python random and numpy inside each worker.

PyTorch already derives a per-worker torch seed from the DataLoader’s generator (or base_seed) combined with worker_id, so torch.randperm, torch.randn, etc. inside workers are deterministic as long as the DataLoader receives a seeded generator. However, random and numpy.random are forked from the main process unseeded per-worker, which makes any code using them in a worker non-deterministic across runs. This function pulls the torch worker seed and uses it to reseed random and numpy so the whole worker is deterministic.

Parameters:

worker_id (int) – Worker id passed by the DataLoader. Unused; kept to match the worker_init_fn signature.

Return type:

None