dim为-1的解释
t1=torch.tensor([[[0,1,2,3],[1,2,3,4]],
[[2,3,4,5],[4,5,6,7]],
[[5,6,7,8],[6,7,8,9]]])
print(t1)
print(t1.shape)
print('no===')
print(t1.max())
print('-1===')
print(t1.max(-1))
print('0===')
print(t1.max(0))
print('1===')
print(t1.max(1))
print('2===')
print(t1.max(2))
C:\Program\anaconda\envs\pytorch\python.exe C:/pythonproject4paper/pytorch/a.py
tensor([[[0, 1, 2, 3],
[1, 2, 3, 4]],
[[2, 3, 4, 5],
[4, 5, 6, 7]],
[[5, 6, 7, 8],
[6, 7, 8, 9]]])
torch.Size([3, 2, 4])
no===
tensor(9)
-1===
torch.return_types.max(
values=tensor([[3, 4],
[5, 7],
[8, 9]]),
indices=tensor([[3, 3],
[3, 3],
[3, 3]]))
0===
torch.return_types.max(
values=tensor([[5, 6, 7, 8],
[6, 7, 8, 9]]),
indices=tensor([[2, 2, 2, 2],
[2, 2, 2, 2]]))
1===
torch.return_types.max(
values=tensor([[1, 2, 3, 4],
[4, 5, 6, 7],
[6, 7, 8, 9]]),
indices=tensor([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]))
2===
torch.return_types.max(
values=tensor([[3, 4],
[5, 7],
[8, 9]]),
indices=tensor([[3, 3],
[3, 3],
[3, 3]]))
Process finished with exit code 0
也就是说,对于一个(3,2,4)的tensor,其dim可以选择为none,-1,0,1,2
none:所有元素的max,得到一个max值
-1:若dim为负,则将被转化为dim+input.dim()+1,即2
0:最粗粒度的方向
1:
2:最细粒度的方向
一句话概括:dim越大,越深入,none即所有最小元素参与计算。