mindspore.nn.Generator

查看源文件
class mindspore.nn.Generator[源代码]

管理随机数状态的生成器,为随机函数提供seed和offset。当seed和offset固定时,随机函数产生相同的随机序列。

输入:
  • step (int) - 偏移量更新的步长。

输出:

包含生成器的种子和偏移量的tuple。

支持平台:

Ascend GPU CPU

样例:

>>> 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)[源代码]

更新偏移量的值,并返回种子和先前的偏移量。

参数:
  • step (int) - 偏移量更新的步长。

返回:

种子和更新前的偏移量。

get_state()[源代码]

获取生成器状态。

返回:

包含生成器的种子和偏移量的tuple。

initial_seed()[源代码]

返回生成器的初始种子。

返回:

生成器的初始化种子。

manual_seed(seed)[源代码]

设置生成器种子。

参数:
  • seed (int) - 生成器的种子。

返回:

生成器自身。

seed()[源代码]

生成可作为生成器种子的随机种子。

返回:

Tensor类型,随机生成的种子。

set_state(seed, offset=None)[源代码]

设置生成器状态。

参数:
  • seed (int) - 生成器的种子。

  • offset (int,可选) - 生成器的偏移量,默认值是:None ,取 0