今天在学习TTSR的过程总遇到了一行代码,我发现max()函数竟然可以返回两个值,于是我决定重新学习一下这个函数

R_lv3_star, R_lv3_star_arg = torch.max(R_lv3, dim=1) #[N, H*W]  hi


1、基础用法:

首先是 torch.max()的基础用法,输入一个张量,返回一个确定的最大值

torch.max(input) → Tensor

Example:

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.6763,  0.7445, -2.2369]])
>>> torch.max(a)
tensor(0.7445)

2、深度用法:

torch.max(inputdimkeepdim=False*out=None)

按维度dim 返回最大值,并且返回索引。

Parameters

  • input (Tensor) – the input tensor.

  • dim (int) – the dimension to reduce.

  • keepdim (bool) – whether the output tensor has dim retained or not. Default: False.

Keyword Arguments

out (tupleoptional) – the result tuple of two output tensors (max, max_indices),返回的最大值和索引各是一个tensor,分别表示该维度的最大值,以及该维度最大值的索引,一起构成元组(Tensor, LongTensor)

Example:

torch.max(a,0)返回每一列中最大值的那个元素,且返回索引(返回最大元素在这一列的行索引)。返回的最大值和索引各是一个tensor,一起构成元组(Tensor, LongTensor)

a = torch.randn(4, 4)
print(a)
print(torch.max(a,0))tensor([[ 0.7439,  2.2739, -2.7576, -0.0676],[-0.7755, -0.6696,  0.3009, -1.4939],[-0.9244,  2.7325,  1.7982,  1.2904],[-0.9091, -0.1857, -1.3392, -1.2928]])
torch.return_types.max(
values=tensor([0.7439, 2.7325, 1.7982, 1.2904]),
indices=tensor([0, 2, 2, 2]))

torch.max(a,1)返回每一行中最大值的那个元素,且返回其索引(返回最大元素在这一行的列索引)

>>> a = torch.randn(4, 4)
>>> a
tensor([[-1.2360, -0.2942, -0.1222,  0.8475],[ 1.1949, -1.1127, -2.2379, -0.6702],[ 1.5717, -0.9207,  0.1297, -1.8768],[-0.6172,  1.0036, -0.6060, -0.2432]])
>>> torch.max(a, 1)
torch.return_types.max(values=tensor([0.8475, 1.1949, 1.5717, 1.0036]), indices=tensor([3, 0, 0, 1]))

Pytorch笔记torch.max() - 知乎

torch.max — PyTorch 1.10 documentation

torch.max()函数==》返回该维度的最大值以及该维度最大值对应的索引相关推荐

  1. Pytorch中tensor维度和torch.max()函数中dim参数的理解

    Pytorch中tensor维度和torch.max()函数中dim参数的理解 维度 参考了 https://blog.csdn.net/qq_41375609/article/details/106 ...

  2. R语言str_subset函数和str_which函数:str_subset函数提取字符串向量中所有包含匹配字符的字符串、str_which函数返回字符串向量中所有包含匹配字符的位置(索引)

    R语言str_subset函数和str_which函数:str_subset函数提取字符串向量中所有包含匹配字符的字符串.str_which函数返回字符串向量中所有包含匹配字符的位置(索引) 目录

  3. torch max()函数

    torch.max()返回的是两个值, 第一个是最大值, 第二个是最大值所在的索引, 一般情况,我们都是求最大值所在的索引 import torcha = torch.tensor([[1, 5, 2 ...

  4. torch.max()、expand()、expand_as()使用讲解

    在分类问题中,通常需要使用max()函数对softmax函数的输出值进行操作,求出预测值索引,然后与标签进行比对,计算准确率.下面讲解一下torch.max()函数的输入及输出值都是什么,便于我们理解 ...

  5. 【torch.argmax与torch.max详解】

    Pytorch常用函数 一.torch.max 1.调用方式 2.相关介绍 3.代码实例及图示理解 二.torch.argmax 1.调用方式 2.相关介绍 3.代码实例及图示理解 三.torch.m ...

  6. Pytorch学习-torch.max()和min()深度解析

    Pytorch学习-torch.max和min深度解析 max的使用 min同理 dim参数理解 二维张量使用max() 三维张量使用max() max的使用 min同理 参考链接: 参考链接: 对于 ...

  7. pytorch之---max()函数

    形式: torch.max(input) → Tensor 返回输入tensor中所有元素的最大值: a = torch.randn(1, 3) >>0.4729 -0.2266 -0.2 ...

  8. Matlab的Max函数详解

    1.M = max(A) 描述:返回向量 A 中的最大值,如果 A 为矩阵,则返回每列的最大值组成的行向量. 参数:A 是待求最大值的向量或矩阵. 输出:M 是向量 A 中的最大值行向量,如果 A 为 ...

  9. PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)

    1. 常用函数 比较函数中有一些是逐元素比较,操作类似逐元素操作,还有一些类似归并操作,常用的比较函数如下表所示. 表中第一行的比较操作已经实现了运算符重载,因此可以使用 a>=b,a>b ...

最新文章

  1. 正道的光!这有个用TensorFlow做的小黄图过滤器
  2. Nacos更新配置参数不生效
  3. java token跨域,java 允许跨域
  4. [4]Telerik Grid 简单使用方法
  5. osgi框架 android,基于OSGi的Android应用模块动态加载框架设计与实现
  6. git commit 规范指南
  7. clone git 修改保存路径_用git管理版本,你必须知道的事情
  8. 文言文能编程了,是噱头还是突破?
  9. 取景框图片 小程序_微信小程序实现身份证取景框拍摄
  10. 在chrome-console中进行xpath/css/js定位
  11. MySQL (9)---数据类型
  12. Linux命令(五)之service服务查找、启动/停止等相关操作
  13. python服务器查看文件更改记录,python 查看远程服务器上的文件
  14. matlab如何求解定积分,matlab如何求解定积分
  15. 【142】阿蛮歌霸使用技巧
  16. matlab自适应高斯滤波,[matlab] 自适应高斯滤波器在二维图像上的应用
  17. Gantt for .NET
  18. 【贪玩巴斯】数字信号处理Digital Signal Processing(DSP)——第三节「离散时间 系统 详解」2021-09-29
  19. Excel 模拟form表单提交
  20. Python 将TXT格式转换为手机通讯录格式vcf

热门文章

  1. jboss7.1.安装配置
  2. 读书笔记—《发现你的行为模式(钻石版)》-开篇
  3. 5G更大的发展在于产业应用
  4. TOP命令 详解CPU 查看多个核心的利用率按1
  5. memcache 缓存的批量删除方案(转)
  6. POJ 1252 Euro Efficiency
  7. MFC无标题栏对话框移动的处理方法
  8. Magento调用静态块 static block
  9. OJ1068: 二进制数(C语言)
  10. 说说图片加载不出来_孩子做错事不可怕,令人担忧的是你说出来的话!