• 文档 >
  • torch >
  • 生成器
快捷键

生成器

class torch.Generator(device='cpu')

创建并返回一个生成器对象,该对象管理产生伪随机数的算法状态。在许多就地随机采样函数中用作关键字参数。

参数:

device ( torch.device , 可选) – 生成器期望的设备。

返回值:

一个 torch.Generator 对象。

返回类型:

生成器

示例:

>>> g_cpu = torch.Generator()
>>> g_cuda = torch.Generator(device='cuda')
clone_state() → torch.Generator

复制生成器的当前状态并返回一个新的生成器,该生成器指向复制的状态。此方法有助于保留生成器的特定状态,以便稍后恢复。

返回值:

指向新复制的状态的生成器。

返回类型:

torch.Generator

示例

>>> g_cuda = torch.Generator(device='cuda')
>>> cloned_state = g_cuda.clone_state()
设备

Generator.device -> 设备

获取生成器的当前设备。

示例:

>>> g_cpu = torch.Generator()
>>> g_cpu.device
device(type='cpu')
get_state() → 张量 ¶

返回生成器的状态作为 torch.ByteTensor

返回值:

包含所有必要位以将生成器恢复到特定时间点的 torch.ByteTensor

返回类型:

张量

示例:

>>> g_cpu = torch.Generator()
>>> g_cpu.get_state()
graphsafe_get_state() → torch.Generator

以安全的方式检索生成器的当前状态,以便进行图捕获。此方法对于确保生成器的状态可以被捕获在 CUDA 图中至关重要。

返回值:

生成器指向当前生成器的状态

返回类型:

torch.Generator

示例

>>> g_cuda = torch.Generator(device='cuda')
>>> current_state = g_cuda.graphsafe_get_state()
graphsafe_set_state(state) None

以安全的方式将生成器的状态设置为指定的状态,以便在图捕获中使用。此方法对于确保生成器的状态可以被 CUDA 图捕获至关重要。

参数:

状态(torch.Generator)- 指向生成器新状态的 Generator,通常从 graphsafe_get_state 获取。

示例

>>> g_cuda = torch.Generator(device='cuda')
>>> g_cuda_other = torch.Generator(device='cuda')
>>> current_state = g_cuda_other.graphsafe_get_state()
>>> g_cuda.graphsafe_set_state(current_state)
initial_seed() → int

返回生成随机数的初始种子。

示例:

>>> g_cpu = torch.Generator()
>>> g_cpu.initial_seed()
2147483647
manual_seed(seed) → Generator

设置生成随机数的种子。返回一个 torch.Generator 对象。任何 32 位整数都是有效的种子。

参数:

seed(整数)- 所需的种子。值必须在[-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]的范围内。否则,将引发 RuntimeError。负数输入将使用公式 0xffff_ffff_ffff_ffff + 种子映射为正数。

返回值:

torch.Generator 对象。

返回类型:

生成器

示例:

>>> g_cpu = torch.Generator()
>>> g_cpu.manual_seed(2147483647)
seed() → int

从 std::random_device 或当前时间获取一个非确定性随机数,并使用它来初始化一个 Generator。

示例:

>>> g_cpu = torch.Generator()
>>> g_cpu.seed()
1516516984916
set_state(new_state) → void

设置生成器状态。

参数:

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

示例:

>>> g_cpu = torch.Generator()
>>> g_cpu_other = torch.Generator()
>>> g_cpu.set_state(g_cpu_other.get_state())

© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源