目录
第二个参数输入列表,则按照列表和翻转规则,依次翻转列表内的维度
方法描述
torch.flip()
函数是PyTorch中用于翻转张量的函数。它可以用于在指定维度上对张量进行翻转操作
torch.flip(input,dim):第一个参数是tensor输入,第二个参数是输入的第几维度,按照维度对输入进行翻转, 反转后shape不变
参数
-
input
:输入张量,可以是任意形状的张量。 -
dims
:一个整数或整数列表,表示要翻转的维度。
函数签名
torch.flip(input, dims) → Tensor
第二个参数输入单维度,翻转列表内的维度
import torch
x = torch.arange(120).view(2, 3, 4,5)
print('x=\n',x)
a = torch.flip(x, [0])
b = torch.flip(x, [1])
c = torch.flip(x, [2])
d = torch.flip(x, [3])
print('a=\n',a)
print('b=\n',b)
print('c=\n',c)
print('d=\n',d)
原张量显示
x=
tensor([[[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[ 10, 11, 12, 13, 14],
[ 15, 16, 17, 18, 19]],[[ 20, 21, 22, 23, 24],
[ 25, 26, 27, 28, 29],
[ 30, 31, 32, 33, 34],
[ 35, 36, 37, 38, 39]],[[ 40, 41, 42, 43, 44],
[ 45, 46, 47, 48, 49],
[ 50, 51, 52, 53, 54],
[ 55, 56, 57, 58, 59]]],
[[[ 60, 61, 62, 63, 64],
[ 65, 66, 67, 68, 69],
[ 70, 71, 72, 73, 74],
[ 75, 76, 77, 78, 79]],[[ 80, 81, 82, 83, 84],
[ 85, 86, 87, 88, 89],
[ 90, 91, 92, 93, 94],
[ 95, 96, 97, 9