torch.linspace#
- torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor #
创建一个大小为
steps
的一维张量,其值从start
到end
均匀分布,包含端点。也就是说,这些值是:从 PyTorch 1.11 开始,linspace 需要 steps 参数。使用 steps=100 可恢复之前的行为。
- 参数
- 关键字参数
out (Tensor, optional) – 输出张量。
dtype (torch.dtype, 可选) – 用于执行计算的数据类型。默认值:如果为 None,则当
start
和end
都是实数时,使用全局默认 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.])