评价此页

torch.random#

创建于: 2019 年 8 月 7 日 | 最后更新于: 2025 年 6 月 18 日

torch.random.fork_rng(devices=None, enabled=True, _caller='fork_rng', _devices_kw='devices', device_type='cuda')[源码]#

Forked RNG,这样当你返回时,RNG 会重置为之前的状态。

参数
  • devices (可迭代的设备 ID) – for which to fork the RNG. CPU RNG state is always forked. By default, fork_rng() operates on all devices, but will emit a warning if your machine has a lot of devices, since this function will run very slowly in that case. If you explicitly specify devices, this warning will be suppressed

  • enabled (bool) – if False, the RNG is not forked. This is a convenience argument for easily disabling the context manager without having to delete it and unindent your Python code under it.

  • device_type (str) – device type str, default is cuda. As for supported device, see details in accelerator

返回类型

生成器

torch.random.get_rng_state()[源码]#

将随机数生成器状态作为torch.ByteTensor返回。

注意

返回的状态仅用于 CPU 上的默认生成器。

另请参阅: torch.random.fork_rng()

返回类型

张量

torch.random.initial_seed()[源码]#

返回生成随机数的初始种子,作为Python long类型。

注意

返回的种子仅用于 CPU 上的默认生成器。

返回类型

int

torch.random.manual_seed(seed)[源码]#

设置在所有设备上生成随机数的种子。返回一个 torch.Generator 对象。

参数

seed (int) – The desired seed. Value must be within the inclusive range [-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Otherwise, a RuntimeError is raised. Negative inputs are remapped to positive values with the formula 0xffff_ffff_ffff_ffff + seed.

返回类型

生成器

torch.random.seed()[源码]#

将用于在所有设备上生成随机数的种子设置为非确定性随机数。返回一个用于播种 RNG 的 64 位数字。

返回类型

int

torch.random.set_rng_state(new_state)[源码]#

设置随机数生成器状态。

注意

此函数仅适用于 CPU。对于 CUDA,请使用 torch.manual_seed(),它适用于 CPU 和 CUDA。

参数

new_state (torch.ByteTensor) – 所需的状态