torch.max()函数==》返回该维度的最大值以及该维度最大值对应的索引
今天在学习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
(input, dim, keepdim=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 (tuple, optional) – 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()函数==》返回该维度的最大值以及该维度最大值对应的索引相关推荐
- Pytorch中tensor维度和torch.max()函数中dim参数的理解
Pytorch中tensor维度和torch.max()函数中dim参数的理解 维度 参考了 https://blog.csdn.net/qq_41375609/article/details/106 ...
- R语言str_subset函数和str_which函数:str_subset函数提取字符串向量中所有包含匹配字符的字符串、str_which函数返回字符串向量中所有包含匹配字符的位置(索引)
R语言str_subset函数和str_which函数:str_subset函数提取字符串向量中所有包含匹配字符的字符串.str_which函数返回字符串向量中所有包含匹配字符的位置(索引) 目录
- torch max()函数
torch.max()返回的是两个值, 第一个是最大值, 第二个是最大值所在的索引, 一般情况,我们都是求最大值所在的索引 import torcha = torch.tensor([[1, 5, 2 ...
- torch.max()、expand()、expand_as()使用讲解
在分类问题中,通常需要使用max()函数对softmax函数的输出值进行操作,求出预测值索引,然后与标签进行比对,计算准确率.下面讲解一下torch.max()函数的输入及输出值都是什么,便于我们理解 ...
- 【torch.argmax与torch.max详解】
Pytorch常用函数 一.torch.max 1.调用方式 2.相关介绍 3.代码实例及图示理解 二.torch.argmax 1.调用方式 2.相关介绍 3.代码实例及图示理解 三.torch.m ...
- Pytorch学习-torch.max()和min()深度解析
Pytorch学习-torch.max和min深度解析 max的使用 min同理 dim参数理解 二维张量使用max() 三维张量使用max() max的使用 min同理 参考链接: 参考链接: 对于 ...
- pytorch之---max()函数
形式: torch.max(input) → Tensor 返回输入tensor中所有元素的最大值: a = torch.randn(1, 3) >>0.4729 -0.2266 -0.2 ...
- Matlab的Max函数详解
1.M = max(A) 描述:返回向量 A 中的最大值,如果 A 为矩阵,则返回每列的最大值组成的行向量. 参数:A 是待求最大值的向量或矩阵. 输出:M 是向量 A 中的最大值行向量,如果 A 为 ...
- PyTorch 笔记(08)— Tensor 比较运算(torch.gt、lt、ge、le、eq、ne、torch.topk、torch.sort、torch.max、torch.min)
1. 常用函数 比较函数中有一些是逐元素比较,操作类似逐元素操作,还有一些类似归并操作,常用的比较函数如下表所示. 表中第一行的比较操作已经实现了运算符重载,因此可以使用 a>=b,a>b ...
最新文章
- 正道的光!这有个用TensorFlow做的小黄图过滤器
- Nacos更新配置参数不生效
- java token跨域,java 允许跨域
- [4]Telerik Grid 简单使用方法
- osgi框架 android,基于OSGi的Android应用模块动态加载框架设计与实现
- git commit 规范指南
- clone git 修改保存路径_用git管理版本,你必须知道的事情
- 文言文能编程了,是噱头还是突破?
- 取景框图片 小程序_微信小程序实现身份证取景框拍摄
- 在chrome-console中进行xpath/css/js定位
- MySQL (9)---数据类型
- Linux命令(五)之service服务查找、启动/停止等相关操作
- python服务器查看文件更改记录,python 查看远程服务器上的文件
- matlab如何求解定积分,matlab如何求解定积分
- 【142】阿蛮歌霸使用技巧
- matlab自适应高斯滤波,[matlab] 自适应高斯滤波器在二维图像上的应用
- Gantt for .NET
- 【贪玩巴斯】数字信号处理Digital Signal Processing(DSP)——第三节「离散时间 系统 详解」2021-09-29
- Excel 模拟form表单提交
- Python 将TXT格式转换为手机通讯录格式vcf