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

  • 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]])
  • 15
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值