Python Pytorch range() 方法
PyTorch 是 Facebook 开发的开源机器学习库。它用于深度神经网络和自然语言处理目的。
函数torch.range()
返回大小为的一维张量
从开始到结束的值与步骤步骤。步长是张量中两个值之间的差距。
此函数已弃用,取而代之的是 torch.arange()。
Syntax: torch.range(start=0, end, step=1, out=None)
Parameters:
start: the starting value for the set of points. Default: 0.
end: the ending value for the set of points
step: the gap between each pair of adjacent points. Default: 1.
out(Tensor, optional): the output tensor
Return type: A tensor
代码#1:
# Importing the PyTorch library
import torch
# Applying the range function and
# storing the resulting tensor in 't'
a = torch.range(1, 6)
print("a = ", a)
b = torch.range(1, 5, 0.5)
print("b = ", b)
输出:
a = tensor([1., 2., 3., 4., 5., 6.])
b = tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000, 5.0000])