torch_ecg.utils.worker_init_fn#
- torch_ecg.utils.worker_init_fn(worker_id: int) None[source]#
worker_init_fnfortorch.utils.data.DataLoaderwhich reseedsDEFAULTS.RNGin each worker process.PyTorch already reseeds the global
random,torchandnumpy.randomstates in each worker (withbase_seed + worker_id, wherebase_seedderives from the generator of the data loader), but it knows nothing aboutnumpy.random.Generatorinstances held by the user. Viafork(the default start method on Linux), each worker inherits a copy of the parent’sDEFAULTS.RNG, so all workers would produce identical random streams, e.g. for augmentations driven byDEFAULTS.RNG.This function derives a distinct, reproducible seed from the worker-specific torch seed (
torch.initial_seed()) and reseedsDEFAULTS.RNG(and theRNG_sample/RNG_randintpartials) accordingly.Usage
from torch.utils.data import DataLoader from torch_ecg.utils import seed_everything, worker_init_fn seed_everything(42) dl = DataLoader( ds, batch_size=32, num_workers=4, worker_init_fn=worker_init_fn, )
- Parameters:
worker_id (int) – Id of the worker process, passed by
torch.utils.data.DataLoader.