Generator
PyTorch provides methods to create random number generation (RNG) as part of your PyTorch program. torch.generator is the primary API to use for standard in the clear application where there is no application of privacy preserving techniques such as differential privacy. To create a cryptographically secure RNG, please use torchcsprng which can be found in the repo here.
-
class
torch.
Generator
(device='cpu') → Generator Creates and returns a generator object which manages the state of the algorithm that produces pseudo random numbers. Used as a keyword argument in many In-place random sampling functions.
- Parameters
device (
torch.device
, optional) – the desired device for the generator.- Returns
An torch.Generator object.
- Return type
Example:
>>> g_cpu = torch.Generator() >>> g_cuda = torch.Generator(device='cuda')
-
device
Generator.device -> device
Gets the current device of the generator.
Example:
>>> g_cpu = torch.Generator() >>> g_cpu.device device(type='cpu')
-
get_state
() → Tensor Returns the Generator state as a
torch.ByteTensor
.- Returns
A
torch.ByteTensor
which contains all the necessary bits to restore a Generator to a specific point in time.- Return type
Example:
>>> g_cpu = torch.Generator() >>> g_cpu.get_state()
-
initial_seed
() → int Returns the initial seed for generating random numbers.
Example:
>>> g_cpu = torch.Generator() >>> g_cpu.initial_seed() 2147483647
-
manual_seed
(seed) → Generator Sets the seed for generating random numbers. Returns a torch.Generator object. It is recommended to set a large seed, i.e. a number that has a good balance of 0 and 1 bits. Avoid having many 0 bits in the seed.
Example:
>>> g_cpu = torch.Generator() >>> g_cpu.manual_seed(2147483647)