评价此页

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, optional) – 输出张量。

  • dtype (torch.dtype, 可选) – 执行计算的数据类型。默认值:如果为 None,则当 startend 都为实数时,使用全局默认数据类型(参见 torch.get_default_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.])