其他分享
首页 > 其他分享> > Pytorch数组反转(数组倒序)函数flip的使用

Pytorch数组反转(数组倒序)函数flip的使用

作者:互联网

TORCH.FLIP函数

torch.flip(input, dims) → Tensor
Reverse the order of a n-D tensor along given axis in dims.
对n维张量的指定维度进行反转(倒序)


NOTE

torch.flip makes a copy of input’s data. This is different from NumPy’s np.flip, which returns a view in constant time. Since copying a tensor’s data is more work than viewing that data, torch.flip is expected to be slower than np.flip.
注意torch.flip是反序地复制一份新的数据,这一点与NumPy不同,NumPy是返回一个view,因而使用torch.flip耗时更久。


Parameters


Example:

>>> x = torch.arange(10).view(2, 5)
>>> x
tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
>>> torch.flip(x, dims=[0])	# 对第0维进行反转
tensor([[5, 6, 7, 8, 9],
        [0, 1, 2, 3, 4]])
>>> torch.flip(x, dims=[1])	# 对第1维进行反转
tensor([[4, 3, 2, 1, 0],
        [9, 8, 7, 6, 5]])
>>> torch.flip(x, dims=[0, 1])	# 对第0、1维进行反转
tensor([[9, 8, 7, 6, 5],
        [4, 3, 2, 1, 0]])

>>> x.flip(dims=[0, 1])	# 对第0、1维进行反转,与上一句效果相同
tensor([[9, 8, 7, 6, 5],
        [4, 3, 2, 1, 0]])

标签:tensor,反转,torch,flip,dims,数组,input,倒序
来源: https://blog.csdn.net/Ocean_waver/article/details/113814671