pytorch参数dim为-1的解释

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即所有最小元素参与计算。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值