mindspore.nn.Generator
- class mindspore.nn.Generator[source]
A generator that manages the state of random numbers and provides seed and offset for random functions. When the seed and offset are fixed, the random function generates the same random sequence.
- Inputs:
step (int) - Set the step size for offset update.
- Outputs:
Tuple consisting of the seed and offset of generator.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore as ms >>> from mindspore.nn import Generator >>> import numpy as np >>> np.random.seed(10) >>> ms.set_context(mode=1) >>> generator = Generator() >>> print(generator.get_state()) (Tensor(shape=[], dtype=Int32, value= 0), Tensor(shape=[], dtype=Int32, value= 0)) >>> print(generator(12)) (0, 0) >>> print(generator.get_state()) (Tensor(shape=[], dtype=Int32, value= 0), Tensor(shape=[], dtype=Int32, value= 12)) >>> generator.manual_seed(20) >>> print(generator.get_state()) (Tensor(shape=[], dtype=Int32, value= 20), Tensor(shape=[], dtype=Int32, value= 0)) >>> print(generator.seed()) 1165313289 >>> print(generator.initial_seed()) 1165313289
- construct(step)[source]
Update the value of offset, and return the seed and the previous offset.
- Parameters
step (int) – Update offset by step.
- Returns
Seed and offset before update.
- get_state()[source]
Get the generator state.
- Returns
Tuple consisting of the seed and offset of generator.
- initial_seed()[source]
Return the initial seed of generator.
- Returns
The initial seed of generator.
- manual_seed(seed)[source]
Sets the generator seed.
- Parameters
seed (int) – Sets the generator seed.
- Returns
The generator self.