快捷方式

torch.linspace

torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor

创建一个大小为 steps 的一维张量,其值在 startend 之间均匀间隔(包含端点)。也就是说,这些值是

(start,start+endstartsteps1,,start+(steps2)endstartsteps1,end)(\text{start}, \text{start} + \frac{\text{end} - \text{start}}{\text{steps} - 1}, \ldots, \text{start} + (\text{steps} - 2) * \frac{\text{end} - \text{start}}{\text{steps} - 1}, \text{end})

从 PyTorch 1.11 开始,linspace 需要 steps 参数。使用 steps=100 可以恢复之前的行为。

参数
  • start (floatTensor) – 点集的起始值。如果是 Tensor,它必须是 0 维张量。

  • end (floatTensor) – 点集的结束值。如果是 Tensor,它必须是 0 维张量。

  • steps (int) – 构建的张量的大小。

关键字参数
  • out (Tensor, 可选) – 输出张量。

  • dtype (torch.dtype, 可选) – 执行计算的数据类型。默认值:如果为 None,当 startend 都是实数时,使用全局默认 dtype (参阅 torch.get_default_dtype());当其中任一为复数时,使用相应的复数 dtype。

  • layout (torch.layout, 可选) – 返回张量的期望布局。默认值:torch.strided

  • device (torch.device, 可选) – 返回张量的期望设备。默认值:如果为 None,则使用默认张量类型的当前设备 (参阅 torch.set_default_device())。device 将是 CPU 张量类型的 CPU,以及 CUDA 张量类型的当前 CUDA 设备。

  • requires_grad (bool, 可选) – 如果 autograd 应该记录返回张量上的操作。默认值:False

示例

>>> torch.linspace(3, 10, steps=5)
tensor([  3.0000,   4.7500,   6.5000,   8.2500,  10.0000])
>>> torch.linspace(-10, 10, steps=5)
tensor([-10.,  -5.,   0.,   5.,  10.])
>>> torch.linspace(start=-10, end=10, steps=5)
tensor([-10.,  -5.,   0.,   5.,  10.])
>>> torch.linspace(start=-10, end=10, steps=1)
tensor([-10.])

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取针对初学者和高级开发者的深入教程

查看教程

资源

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

查看资源