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
torch.max基本使用相关推荐
- PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)
1. 常用函数 比较函数中有一些是逐元素比较,操作类似逐元素操作,还有一些类似归并操作,常用的比较函数如下表所示. 表中第一行的比较操作已经实现了运算符重载,因此可以使用 a>=b,a>b ...
- Pytorch view()、squeeze()、unsqueeze()、torch.max()
本篇博客主要向大家介绍Pytorch中view().squeeze().unsqueeze().torch.max()函数,这些函数虽然简单,但是在 神经网络编程总却经常用到,希望大家看了这篇博文能够 ...
- torch.max和torch.softmax
softmax 先exp,再在上面求总和的百分比 解决了负数相互抵消的问题. if __name__ == '__main__':import torchimport torch.nn.functio ...
- torch max 判断与筛选
判断max是否大于0:item()后,就变成了一个值了, import torch y=torch.Tensor(1,2,3) if torch.max(y>=2).item(): print( ...
- Pytorch学习-torch.max()和min()深度解析
Pytorch学习-torch.max和min深度解析 max的使用 min同理 dim参数理解 二维张量使用max() 三维张量使用max() max的使用 min同理 参考链接: 参考链接: 对于 ...
- PyTorch基础(十)----- torch.max()方法
一.前言 这个方法跟上一篇文章的torch.max()方法非常类似,只不过一个是求最大值,一个是求平均值.在某些情况下,甚至可以代替下采样中的最大池化和平均池化,所以说,这两个方法的用处还是蛮大的. ...
- pytorch 之 torch.max() 和 torch.min() 记录
两个函数用法相同,此处就介绍max函数. 1.torch.max(data),不指定维度,返回data的最大值. 2.torch.max(data,dim),返回data中指定维度的最大值. 3.to ...
- torch.max()函数==》返回该维度的最大值以及该维度最大值对应的索引
今天在学习TTSR的过程总遇到了一行代码,我发现max()函数竟然可以返回两个值,于是我决定重新学习一下这个函数 R_lv3_star, R_lv3_star_arg = torch.max(R_lv ...
- 如何使用PyTorch torch.max()
In this article, we'll take a look at using the PyTorch torch.max() function. 在本文中,我们将介绍如何使用PyTorch ...
- torch.max()、expand()、expand_as()使用讲解
在分类问题中,通常需要使用max()函数对softmax函数的输出值进行操作,求出预测值索引,然后与标签进行比对,计算准确率.下面讲解一下torch.max()函数的输入及输出值都是什么,便于我们理解 ...
最新文章
- 小米今日正式进军越南市场 借助合作方铺渠道分销
- 【SSH三个框架】Hibernate第八部分基础:经营-many关系
- 知识点讲解五:处理js异步加载问题
- idea的2020.2版本
- 算法导论系列:分治算法
- java中super可以重新父类么_Java Super 覆盖方法
- html使标签为正方形,W3C CSS测试答案
- pytest测试实战pdf_Pytest测试实战
- Struts2的struts.xml的配置细节
- 一个不错微服务架构图
- java 支付宝预下单失败,系统异常,预下单状态未知!!! connect timed out
- day12-HTML、CSS与blog页面讲解
- SAP中报表清单导出的常用方法
- centOs7.6+Typecho+Bt面板搭建个人Blog
- 普华永道高级JAVA面试记录
- win11记事本出现乱码怎么恢复 windows11记事本出现乱码的解决方法
- 我的Android进阶之旅:经典的大牛博客推荐
- 大物下第11章熟记内容
- BT协议-torrent编码
- Cerebro:一个好用的单细胞数据展示shiny工具