torchnlp.random package

The torchnlp.random package introduces modules for finer grain control of random state.

class torchnlp.random.RandomGeneratorState(random, torch, numpy, torch_cuda)[source]
numpy

Alias for field number 2

random

Alias for field number 0

torch

Alias for field number 1

torch_cuda

Alias for field number 3

torchnlp.random.fork_rng(seed=None, cuda=False)[source]

Forks the torch, numpy and random random generators, so that when you return, the random generators are reset to the state that they were previously in.

Parameters:
  • seed (int or None, optional) – If defined this sets the seed values for the random generator fork. This is a convenience parameter.
  • cuda (bool, optional) – If True saves the cuda seed also. Getting and setting the random generator state can be quite slow if you have a lot of GPUs.
torchnlp.random.fork_rng_wrap(function=None, **kwargs)[source]

Decorator alias for fork_rng.

torchnlp.random.get_random_generator_state(cuda: bool = False) → torchnlp.random.RandomGeneratorState[source]

Get the torch, numpy and random random generator state.

Parameters:cuda (bool, optional) – If True saves the cuda seed also. Note that getting and setting the random generator state for CUDA can be quite slow if you have a lot of GPUs.
Returns:RandomGeneratorState
torchnlp.random.set_random_generator_state(state: torchnlp.random.RandomGeneratorState)[source]

Set the torch, numpy and random random generator state.

Parameters:state (RandomGeneratorState) –
torchnlp.random.set_seed(seed, cuda=False)[source]

Set seed values for random generators.

Parameters:
  • seed (int) – Value used as a seed.
  • cuda (bool, optional) – If True sets the cuda seed also.