评价此页

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 都是实数时,使用全局默认 dtype(参见 torch.get_default_dtype()),当其中一个为复数时,使用相应的复数 dtype。

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

  • device (torch.device, 可选) – 返回张量的所需设备。默认:如果为 None,则使用当前设备作为默认张量类型(参见 torch.set_default_device())。对于 CPU 张量类型,device 将是 CPU;对于 CUDA 张量类型,device 将是当前的 CUDA 设备。

  • requires_grad (bool, optional) – 如果 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.])