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]])
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐