目录
第二个参数输入列表,则按照列表和翻转规则,依次翻转列表内的维度
方法描述
torch.flip()
函数是PyTorch中用于翻转张量的函数。它可以用于在指定维度上对张量进行翻转操作
torch.flip(input,dim):第一个参数是tensor输入,第二个参数是输入的第几维度,按照维度对输入进行翻转, 反转后shape不变
参数
-
input
:输入张量,可以是任意形状的张量。 -
dims
:一个整数或整数列表,表示要翻转的维度。
函数签名
torch.flip(input, dims) → Tensor
第二个参数输入单维度,翻转列表内的维度
python
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, 98, 99]],
[[100, 101, 102, 103, 104],
[105, 106, 107, 108, 109],
[110, 111, 112, 113, 114],
[115, 116, 117, 118, 119]]]])
按照0维翻转结果(在三个方括号"[[[ ]]]"内的内容为一体 在四括号内"[[[[ ]]]]"进行倒叙排列)
a=
tensor([[[[ 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, 98, 99]],
[[100, 101, 102, 103, 104],
[105, 106, 107, 108, 109],
[110, 111, 112, 113, 114],
[115, 116, 117, 118, 119]]],
[[[ 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]]]])
按照1维翻转结果(在两个方括号"[[ ]]"内的内容为一体 在三括号"[[[ ]]]"内进行倒叙排列)
b=
tensor([[[[ 40, 41, 42, 43, 44],
[ 45, 46, 47, 48, 49],
[ 50, 51, 52, 53, 54],
[ 55, 56, 57, 58, 59]],
[[ 20, 21, 22, 23, 24],
[ 25, 26, 27, 28, 29],
[ 30, 31, 32, 33, 34],
[ 35, 36, 37, 38, 39]],
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[ 10, 11, 12, 13, 14],
[ 15, 16, 17, 18, 19]]],
[[[100, 101, 102, 103, 104],
[105, 106, 107, 108, 109],
[110, 111, 112, 113, 114],
[115, 116, 117, 118, 119]],
[[ 80, 81, 82, 83, 84],
[ 85, 86, 87, 88, 89],
[ 90, 91, 92, 93, 94],
[ 95, 96, 97, 98, 99]],
[[ 60, 61, 62, 63, 64],
[ 65, 66, 67, 68, 69],
[ 70, 71, 72, 73, 74],
[ 75, 76, 77, 78, 79]]]])
按照2维翻转结果(在一个方括号"[ ]"内的内容为一体 在双括号内"[[ ]]"进行倒叙排列)
c=
tensor([[[[ 15, 16, 17, 18, 19],
[ 10, 11, 12, 13, 14],
[ 5, 6, 7, 8, 9],
[ 0, 1, 2, 3, 4]],
[[ 35, 36, 37, 38, 39],
[ 30, 31, 32, 33, 34],
[ 25, 26, 27, 28, 29],
[ 20, 21, 22, 23, 24]],
[[ 55, 56, 57, 58, 59],
[ 50, 51, 52, 53, 54],
[ 45, 46, 47, 48, 49],
[ 40, 41, 42, 43, 44]]],
[[[ 75, 76, 77, 78, 79],
[ 70, 71, 72, 73, 74],
[ 65, 66, 67, 68, 69],
[ 60, 61, 62, 63, 64]],
[[ 95, 96, 97, 98, 99],
[ 90, 91, 92, 93, 94],
[ 85, 86, 87, 88, 89],
[ 80, 81, 82, 83, 84]],
[[115, 116, 117, 118, 119],
[110, 111, 112, 113, 114],
[105, 106, 107, 108, 109],
[100, 101, 102, 103, 104]]]])
按照3维翻转结果(在"[ ]"每个数字单独为一体 在单括号内"[ ]"进行倒叙排列)
d=
tensor([[[[ 4, 3, 2, 1, 0],
[ 9, 8, 7, 6, 5],
[ 14, 13, 12, 11, 10],
[ 19, 18, 17, 16, 15]],
[[ 24, 23, 22, 21, 20],
[ 29, 28, 27, 26, 25],
[ 34, 33, 32, 31, 30],
[ 39, 38, 37, 36, 35]],
[[ 44, 43, 42, 41, 40],
[ 49, 48, 47, 46, 45],
[ 54, 53, 52, 51, 50],
[ 59, 58, 57, 56, 55]]],
[[[ 64, 63, 62, 61, 60],
[ 69, 68, 67, 66, 65],
[ 74, 73, 72, 71, 70],
[ 79, 78, 77, 76, 75]],
[[ 84, 83, 82, 81, 80],
[ 89, 88, 87, 86, 85],
[ 94, 93, 92, 91, 90],
[ 99, 98, 97, 96, 95]],
[[104, 103, 102, 101, 100],
[109, 108, 107, 106, 105],
[114, 113, 112, 111, 110],
[119, 118, 117, 116, 115]]]])
第二个参数输入列表,则按照列表和翻转规则,依次翻转列表内的维度
python
import torch
# 翻转张量
a = torch.tensor([[1, 2, 3], [4, 5, 6]])
b = torch.flip(a, dims=[0, 1])
print(b)
结果输出
tensor([[6, 5, 4],
[3, 2, 1]])
翻转逻辑
先变成
tensor([[4,5,6],
[1,2,3]])
再变成
tensor([[6,5,4],[]])