torch.max()的用法

torch.max(input) → Tensor
用于计算张量(tensor)中的最大值的值和索引。

import torch
a = torch.randn(1, 3)
print(a)
print(torch.max(a))

tensor([[-0.6500, -0.3239, -0.2899]])
tensor(-0.2899)

dim=1表示从每行中选取最大

import torch
a = torch.randn(4, 4)
print(a)
print(torch.max(input=a, dim=1))

tensor([[-1.2154,  1.1472, -2.3489,  0.6813],
        [ 1.4553,  1.9520, -0.8385,  0.7121],
        [ 0.2057,  1.2206,  1.3722, -0.7365],
        [-0.0992,  0.3109,  1.5323, -0.5297]])

torch.return_types.max(
values=tensor([1.1472, 1.9520, 1.3722, 1.5323]),
indices=tensor([1, 1, 2, 2]))

dim=0表示从每列中选取最大

import torch
a = torch.randn(4, 4)
print(a)
print(torch.max(input=a, dim=0))

tensor([[-0.2915, -0.1751,  0.2905,  0.5155],
        [ 1.3450, -1.0030, -0.1825, -0.0415],
        [ 1.4867, -1.4277,  0.1875, -1.9632],
        [-0.0909, -1.7179, -0.8297, -1.4644]])
torch.return_types.max(
values=tensor([ 1.4867, -0.1751,  0.2905,  0.5155]),
indices=tensor([2, 0, 0, 0]))
import torch
a = torch.randn(4, 4)
print(a)
torch_data = torch.max(input=a, dim=1)
print(torch_data[0])
# tensor([0.9787, 0.0933, 0.9512, 0.9254])
print(torch_data[0].view(torch_data[0].size()[0], 1))
# tensor([[ 1.6914],
#         [-0.1405],
#         [ 0.8139],
#         [-0.3309]])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数产第一混子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值