espnet2.iterators.sequence_iter_factory.worker_init_fn
Less than 1 minute
espnet2.iterators.sequence_iter_factory.worker_init_fn
espnet2.iterators.sequence_iter_factory.worker_init_fn(worker_id, base_seed=0)
Set random seed for each worker in DataLoader.
This function initializes the random seed for each worker process used by PyTorch’s DataLoader. It ensures that each worker has a unique seed, which is calculated based on the base seed and the worker ID. This is particularly useful for ensuring reproducibility in data loading across different runs of the training process.
- Parameters:
- worker_id (int) – The ID of the worker. Each worker will have a unique ID, starting from 0.
- base_seed (int , optional) – The base seed used for generating the random seed for each worker. Default is 0.
Examples
>>> worker_init_fn(0, base_seed=42) # Initializes the seed for worker 0
>>> worker_init_fn(1, base_seed=42) # Initializes the seed for worker 1
NOTE
This function is typically passed to the DataLoader as the worker_init_fn argument.