torch.max基本使用

toch.max

在使用前首先要import torch

import torch
a0 = torch.randn(3, 1)
print('a0=', a0)
b0=torch.max(a0, 1)
print('b0=', b0, '\n')
a1 = torch.randn(1, 3)
print('a1=', a1)
b1=torch.max(a1,1)
print('b1=', b1 )

output

a0= tensor([[-0.5406],
        [ 0.2674],
        [ 0.5057]])
b0= torch.return_types.max(
values=tensor([-0.5406,  0.2674,  0.5057]),
indices=tensor([0, 0, 0])) 

a1= tensor([[0.3328, 0.5163, 1.4075]])
b1= torch.return_types.max(
values=tensor([1.4075]),
indices=tensor([2]))

可见torch.max(变量,1)按行输出最大值和最大值所在行的索引(索引从0开始)
torch.max(变量,0)按列输出最大值和最大值所在列的索引(索引从0开始)

import torch
data = torch.randn(3, 2)
print('data=', data)
a0=torch.max(data, 1)
print(a0)
print('a0[0]=', a0[0],'a0[1]=', a0[1], '\n')
b0=torch.max(data, 1)[0]
c0=torch.max(data, 1)[1].data
d0=torch.max(data, 1)[0].data
print('torch.max(data, 1)[1]=', b0, '\n', 'torch.max(data, 1)[1].data=', c0, '\n', 'torch.max(data, 1)[0].data=', d0)

output

data= tensor([[-0.7846, -0.5480],
        [-1.1179,  0.4518],
        [ 0.1165,  1.3662]])
torch.return_types.max(
values=tensor([-0.5480,  0.4518,  1.3662]),
indices=tensor([1, 1, 1]))
a0[0]= tensor([-0.5480,  0.4518,  1.3662]) a0[1]= tensor([1, 1, 1]) 
torch.max(data, 1)[1]= tensor([-0.5480,  0.4518,  1.3662]) 
 torch.max(data, 1)[1].data= tensor([1, 1, 1]) 
 torch.max(data, 1)[0].data= tensor([-0.5480,  0.4518,  1.3662])

[ ]里数字代表torch.return_types.max里参数的索引,0表示values值,1代表值对应索引,总结就是:
torch.max()[0], 返回最大值的每个数
troch.max()[1], 返回最大值的每个索引
troch.max(),返回最大值的数,最大值的索引
torch.max()[1].data.numpy() 把数据转化成numpy ndarry

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值