当前位置:网站首页>PyTorch磨刀篇|argmax和argmin函数
PyTorch磨刀篇|argmax和argmin函数
2022-07-01 21:44:00 【51CTO】
一、语法格式
格式一(只针对argmax函数):
torch.argmax(input) → LongTensor
功能:
Returns the indices of the maximum value of all elements in the input tensor。
即:返回输入张量中所有元素中最大值对应的索引(按行搜索);如果有多个相同的值,则返回第一次遇到的那个值对应的索引。
举例:
In [28]: r=torch.tensor([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])
In [29]: torch.argmax(r)
Out[29]: tensor(14)
格式二:
[1]torch.argmax(input, dim=None, keepdim=False)
功能:
Returns the indices of the maximum values of a tensor across a dimension.
- input( Tensor) – the input tensor.即:输出张量。
- dim( int) – the dimension to reduce. If
None, the argmax of the flattened input is returned.即:要减少的维数。
- keepdim( bool) – whether the output tensor has
dim retained or not. Ignored if dim=None.即:
举例:
In [30]: a = torch.randn(4, 4)
In [31]: a
Out[31]:
tensor([[ 1.4360, 0.6342, -0.5233, 0.4902],
[ 1.1998, -0.8644, 0.5244, 0.2690],
[ 0.0998, -1.5043, 0.1619, -1.4634],
[ 0.0992, -1.0843, -1.3829, 0.5790]])
In [32]: torch.argmax(a)
Out[32]: tensor(0)
In [33]: torch.argmax(a,dim=0)
Out[33]: tensor([0, 0, 1, 3])
In [34]: torch.argmax(a,dim=1)
Out[34]: tensor([0, 0, 2, 3])
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 对于tensor(0)输出,意义如下:
第0个: 1.4360 | 第1个: 0.6342 | 第2个: -0.5233 | 第3个: 0.4902 | 第4个: 1.1998 | 第5个: -0.8644 | 第6个: 0.5244 | 第7个: 0.2690 | 第8个: 0.0998 | 第9个: -1.5043 |
第10个: 0.1619 | 第11个: -1.4634 | 第12个: 0.0992 | 第13个: -1.0843 | 第14个: -1.3829 | 第15个: 0.5790 |
- 对于tensor([0, 0, 1, 3])输出,意义如下:

这时,每一列视为下标从0到3的一个数组。易见,从左到右每一列(数组)中最大值分别为:1.4360、0.6342、0.5244、0.5790,它们对应的一维数组中的下标分别为0、0、1、3,于是得到张量tensor([0, 0, 1, 3])。
- 对于tensor([0, 0, 2, 3])输出:
意义就容易理解了。沿水平方向从左向右从上到下看,每一行对应一个数组,下标向左向右依次为0、1、2、3。于是,这4个数组中最大值分别为1.4360、1.1998、0.1619、1.3829,它们对应的一维数组中的下标分别为0、0、2、3,于是得到张量tensor([0, 0, 2, 3])。
功能:
[2]torch.argmin(input, dim=None, keepdim=False) → LongTensor
argmin功能:Returns the indices of the minimum value(s) of the flattened tensor or along a dimension。
理解类似上面argmax函数的第二种格式,相应于dim=0和dim=1,依次返回由最小值对应下标组成的列方向数组与行方向数组组成的张量。
边栏推荐
- Fundamentals - IO intensive computing and CPU intensive computing
- MySQL清空表数据
- Training on the device with MIT | 256Kb memory
- AirServer2022最新版功能介绍及下载
- [STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
- 100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
- Go — 相关依赖对应的exe
- [monomer] recommended configuration of streaming information i-bpsv3 server
- An operation tool used by we media professionals who earn 1w+ a month
- One of the basic learning of function
猜你喜欢

【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程

手动实现function isInstanceOf(child,Parent)

Why does blocprovider feel similar to provider?

二叉树的基本操作

EasyExcel 复杂数据导出

"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition

【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
![[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分](/img/d1/a56231cd4eb3cc1d91d8a55048ccfe.png)
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分

What is the difference between PMP and NPDP?

【深度学习】利用深度学习监控女朋友的微信聊天?
随机推荐
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
PCB plug hole technology~
基于K-means的用户画像聚类模型
Show member variables and methods in classes in idea
MySQL series transaction log redo log learning notes
91.(cesium篇)cesium火箭发射模拟
业务可视化-让你的流程图'Run'起来
pytest合集(2)— pytest運行方式
AirServer手机第三方投屏电脑软件
MySQL之MHA高可用配置及故障切换
Test cancellation 1
Little p weekly Vol.11
Wechat applet, continuously playing multiple videos. Synthesize the appearance of a video and customize the video progress bar
都能看懂的LIS(最长上升子序列)问题[通俗易懂]
首席信息官对高绩效IT团队定义的探讨和分析
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
News classification based on LSTM model
从零开始学 MySQL —数据库和数据表操作
详解JMM
Getting started with the lockust series
