PyTorch中torch.linspace的详细用法

tech2026-08-10  1

线性间距向量

torch.linspace(start, end, steps=100, out=None) → Tensor

返回一个1维张量,包含在区间start和end上均匀间隔的step个点。 输出张量的长度由steps决定。

参数: start (float) - 区间的起始点 end (float) - 区间的终点 steps (int) - 在start和end间生成的样本数 out (Tensor, optional) - 结果张量

例子

#生成0到10的4个数构成的等差数列 a = torch.linspace(0,10,steps=4) print(a) #生成0到10的5个数构成的等差数列 b = torch.linspace(0,10,steps=5) print(b) tensor([ 0.0000, 3.3333, 6.6667, 10.0000]) tensor([ 0.0000, 2.5000, 5.0000, 7.5000, 10.0000])
最新回复(0)