torch.flip

torch.flip(input, dims) Tensor

沿dims中指定的轴反转n维张量的顺序。

注意

torch.flip 会复制 input 的数据。这与 NumPy 的 np.flip 不同,np.flip 返回的是一个视图,并且操作时间是常量级别。由于复制张量的数据比查看该数据需要更多的计算资源,因此预计torch.flipnp.flip 更慢。

参数
  • input (Tensor) – 需要输入的张量。

  • dims (一个列表或元组) – 指定需要翻转的轴

示例:

>>> x = torch.arange(8).view(2, 2, 2)
>>> x
tensor([[[ 0,  1],
         [ 2,  3]],

        [[ 4,  5],
         [ 6,  7]]])
>>> torch.flip(x, [0, 1])
tensor([[[ 6,  7],
         [ 4,  5]],

        [[ 2,  3],
         [ 0,  1]]])
本页目录