Python Pytorch 排列()方法
PyTorch 是 Facebook 开发的开源机器学习库。它用于深度神经网络和自然语言处理目的。
函数torch.arrange() 返回大小为的一维张量
来自区间的值从一开始就采取共同的差异步骤。
Syntax: torch.arrange(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:
Python3
# Importing the PyTorch library
import torch
# Applying the arrange function and
# storing the resulting tensor in 't'
a = torch.arrange(3)
print("a = ", a)
b = torch.arrange(1, 6)
print("b = ", b)
c = torch.arrange(1, 5, 0.5)
print("c = ", c)
输出:
a = tensor([0, 1, 2])
b = tensor([1, 2, 3, 4, 5])
c = tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000])
请注意,与 end 比较时,非整数步长会出现浮点舍入误差;为避免不一致,我们建议在这种情况下在末尾添加一个小 epsilon。