torch.max()和numpy.max()使用辨析

torch.max()

单个Tensor

arr = torch.rand(3, 4)
top_value, top_indices = torch.max(arr, dim=1)

>>>
tensor([[0.0038, 0.2420, 0.6851, 0.1004],
        [0.2818, 0.0513, 0.2679, 0.7817],
        [0.3812, 0.3211, 0.9846, 0.1714]])

沿着dim=1求最大值, 所以输出值有3个
tensor([0.6851, 0.7817, 0.9846]) 
tensor([2, 3, 2])

两个Tensor, 这两个Tensor的shape一致
依次比较每一个元素, 并取最大值

arr1 = torch.rand(3, 4)
arr2 = torch.rand(3, 4)

res = torch.max(arr1, arr2)

>>>
tensor([[0.7124, 0.1979, 0.9612, 0.3003],
        [0.4030, 0.6211, 0.4740, 0.9530],
        [0.1326, 0.2359, 0.3715, 0.2889]])
tensor([[0.4937, 0.9679, 0.6621, 0.3993],
        [0.4538, 0.2039, 0.5995, 0.2262],
        [0.5887, 0.4631, 0.7709, 0.5620]])
        
tensor([[0.7124, 0.9679, 0.9612, 0.3993],
        [0.4538, 0.6211, 0.5995, 0.9530],
        [0.5887, 0.4631, 0.7709, 0.5620]])

numpy.max()

arr = np.random.randint(low=1, high=10, size=(3, 4))
top_values = np.max(arr, axis=1)
top_indices = np.argmax(arr, axis=1)

>>>
[[8 5 4 2]
 [7 1 7 3]
 [6 8 7 3]]
 
[8 7 8]
[0 0 1]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值