当前位置: 首页 > 工具软件 > Flip View > 使用案例 >

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

龙繁
2023-12-01

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

  • input (Tensor) – the input tensor.
  • dims (a list or tuple) – axis to flip on

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]])
 类似资料: