pytorch.max函数 高维向量的说明

官方解释:

torch.max(inputdimkeepdim=False*out=None) -> (TensorLongTensor)

Returns a namedtuple (values, indices) where values is the maximum value of each row of the input tensor in the given dimension dim. And indices is the index location of each maximum value found (argmax).

If keepdim is True, the output tensors are of the same size as input except in the dimension dim where they are of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensors having 1 fewer dimension than input.

大意:在指定的维数取最大值,返回结果和序号。

对于输入为一或者二维的向量,很好理解。对于高维的向量a,shape为(2,3,4),torch.max(a,1) 代表的表示遍历i∈[0,1],j∈[0,3],取(i,:,j)这几个数的最大值,形成新的张量的size为(2,4),如果keepdim=True,则形成的张量为(2,1,4)。

torch.max(a,2),代表的表示遍历i∈[0,1],j∈[0,2],取(i,j,:)这几个数的最大值。

 

import torch 
a = torch.randn(2, 3,4)
print(a)
c=torch.max(a,1)
d=torch.max(a,2)
print(c[0].shape)
print(c)
print(d[0].shape)
print(d)
tensor([[[ 0.8346, -0.6632,  1.0924,  2.3951],
         [ 0.8407,  0.3949, -0.3020, -1.7022],
         [ 2.7546,  0.5894,  1.9637, -1.0159]],

        [[-1.3591, -0.8133,  0.4478,  0.1800],
         [ 0.0949, -0.3733,  0.4843, -0.9781],
         [ 0.0283, -0.9105, -1.2899,  2.9471]]])
torch.Size([2, 4])
torch.return_types.max(
values=tensor([[ 2.7546,  0.5894,  1.9637,  2.3951],
        [ 0.0949, -0.3733,  0.4843,  2.9471]]),
indices=tensor([[2, 2, 2, 0],
        [1, 1, 1, 2]]))
torch.Size([2, 3])
torch.return_types.max(
values=tensor([[2.3951, 0.8407, 2.7546],
        [0.4478, 0.4843, 2.9471]]),
indices=tensor([[3, 0, 0],
        [2, 2, 3]]))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值