Shortcuts

seed

Functions

isolate_rng

A context manager that resets the global random state on exit to what it was before entering.

pl_worker_init_function

rtype:

None

reset_seed

rtype:

None

seed_everything

rtype:

Any

Utilities to help with reproducibility of models.

pytorch_lightning.utilities.seed.isolate_rng()[source]

A context manager that resets the global random state on exit to what it was before entering.

It supports isolating the states for PyTorch, Numpy, and Python built-in random number generators.

Return type:

Generator[None, None, None]

Example

>>> import torch
>>> torch.manual_seed(1)  
<torch._C.Generator object at ...>
>>> with isolate_rng():
...     [torch.rand(1) for _ in range(3)]
[tensor([0.7576]), tensor([0.2793]), tensor([0.4031])]
>>> torch.rand(1)
tensor([0.7576])