In this article, we’ll take a look at using the PyTorch torch.max() function.

在本文中,我们将介绍如何使用PyTorch torch.max()函数。

As you may expect, this is a very simple function, but interestingly, it has more than you imagine.

如您所料,这是一个非常简单的功能,但有趣的是,它具有的功能超出了您的想象。

Let’s take a look at using this function, using some simple examples.

让我们使用一些简单的示例来看看如何使用此功能。

NOTE: At the time of writing, the PyTorch version used is PyTorch 1.5.0

注意 :在撰写本文时,使用的PyTorch版本是PyTorch 1.5.0



PyTorch torch.max()–基本语法 (PyTorch torch.max() – Basic Syntax)

To use PyTorch torch.max(), first import torch.

要使用PyTorch torch.max() ,请首先导入torch


import torch

Now, this function returns the maximum among the elements in the Tensor.

现在,此函数返回张量中元素之间的最大值。

PyTorch torch.max()的默认行为 (Default Behavior of PyTorch torch.max())

The default behavior is to return a single element and an index, corresponding to the global maximum element.

默认行为是返回单个元素和对应于全局最大元素的索引。


max_element = torch.max(input_tensor)

Here is an example:

这是一个例子:


p = torch.randn([2, 3])
print(p)
max_element = torch.max(p)
print(max_element)

Output

输出量


tensor([[-0.0665,  2.7976,  0.9753],[ 0.0688, -1.0376,  1.4443]])
tensor(2.7976)

Indeed, this gives us the global maximum element in the Tensor!

确实,这使我们在张量中具有全局最大元素!



沿尺寸使用torch.max() (Use torch.max() along a dimension)

However, you may wish to get the maximum along a particular dimension, as a Tensor, instead of a single element.

但是,您可能希望沿特定尺寸获得最大值,即Tensor而不是单个元素。

To specify the dimension (axis – in numpy), there is another optional keyword argument, called dim

要指定尺寸( axis –在numpy ),还有另一个可选的关键字参数,称为dim

This represents the direction that we take for the maximum.

这代表了我们追求最大的方向。

This returns a tuple, max_elements and max_indices.

这将返回一个元组max_elementsmax_indices

  • max_elements -> All the maximum elements of the Tensor.max_elements >张量的所有最大元素。
  • max_indices -> Indices corresponding to the maximum elements.max_indices >对应于最大元素的索引。

max_elements, max_indices = torch.max(input_tensor, dim)

This will return a Tensor, which has the maximum elements along the dimension dim.

这将返回一个Tensor,其在dim维度上具有最大元素。

Let’s now look at some examples.

现在让我们看一些示例。


p = torch.randn([2, 3])
print(p)# Get the maximum along dim = 0 (axis = 0)
max_elements, max_idxs = torch.max(p, dim=0)
print(max_elements)
print(max_idxs)

Output

输出量


tensor([[-0.0665,  2.7976,  0.9753],[ 0.0688, -1.0376,  1.4443]])
tensor([0.0688, 2.7976, 1.4443])
tensor([1, 0, 1])

As you can see, we find the maximum along the dimension 0 (maximum along columns).

如您所见,我们在维度0上找到最大值(在列上找到最大值)。

Also, we get the indices corresponding to the elements. For example,0.0688 has the index 1 along column 0

同样,我们获得与元素相对应的索引。 例如, 0.0688在第0列的索引为1

Similarly, if you want to find the maximum along the rows, use dim=1.

同样,如果要在行中查找最大值,请使用dim=1


# Get the maximum along dim = 1 (axis = 1)
max_elements, max_idxs = torch.max(p, dim=1)
print(max_elements)
print(max_idxs)

Output

输出量


tensor([2.7976, 1.4443])
tensor([1, 2])

Indeed, we get the maximum elements along the row, and the corresponding index (along the row).

确实,我们获得了沿着该行的最大元素以及相应的索引(沿着该行)。



使用torch.max()进行比较 (Using torch.max() for comparison)

We can also use torch.max() to get the maximum values between two Tensors.

我们还可以使用torch.max()获得两个张量之间的最大值。


output_tensor = torch.max(a, b)

Here, a and b must have the same dimensions, or must be “broadcastable” Tensors.

在此, ab必须具有相同的尺寸,或者必须是“可广播的”张量。

Here is a simple example to compare two Tensors having the same dimensions.

这是一个比较两个具有相同尺寸的张量的简单示例。


p = torch.randn([2, 3])
q = torch.randn([2, 3])print("p =", p)
print("q =",q)# Compare elements of p and q and get the maximum
max_elements = torch.max(p, q)print(max_elements)

Output

输出量


p = tensor([[-0.0665,  2.7976,  0.9753],[ 0.0688, -1.0376,  1.4443]])
q = tensor([[-0.0678,  0.2042,  0.8254],[-0.1530,  0.0581, -0.3694]])
tensor([[-0.0665,  2.7976,  0.9753],[ 0.0688,  0.0581,  1.4443]])

Indeed, we get the output tensor having maximum elements between p and q.

实际上,我们得到的输出张量具有在pq之间的最大元素。



结论 (Conclusion)

In this article, we learned about using the torch.max() function, to find out the maximum element of a Tensor.

在本文中,我们学习了如何使用torch.max()函数来查找张量的最大元素。

We also used this function to compare two tensors and get the maximum among them.

我们还使用此函数比较两个张量,并在其中得到最大值。

For similar articles, do go through our content on our PyTorch tutorials! Stay tuned for more!

对于类似的文章,请在我们的PyTorch教程中浏览我们的内容! 敬请期待更多!

参考资料 (References)

  • PyTorch Official Documentation on torch.max()关于torch.max()的PyTorch官方文档


翻译自: https://www.journaldev.com/39463/pytorch-torch-max

如何使用PyTorch torch.max()相关推荐

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

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

  2. Pytorch view()、squeeze()、unsqueeze()、torch.max()

    本篇博客主要向大家介绍Pytorch中view().squeeze().unsqueeze().torch.max()函数,这些函数虽然简单,但是在 神经网络编程总却经常用到,希望大家看了这篇博文能够 ...

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

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

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

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

  5. PyTorch系列 | _, predicted = torch.max(outputs.data, 1)的理解

    使用pytorch的小伙伴们,一定看过下面这段代码 _, predicted = torch.max(outputs.data, 1) 那么,这里的 下划线_ 表示什么意思? 首先,torch.max ...

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

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

  7. PyTorch基础(十)----- torch.max()方法

    一.前言 这个方法跟上一篇文章的torch.max()方法非常类似,只不过一个是求最大值,一个是求平均值.在某些情况下,甚至可以代替下采样中的最大池化和平均池化,所以说,这两个方法的用处还是蛮大的. ...

  8. pytorch 之 torch.max() 和 torch.min() 记录

    两个函数用法相同,此处就介绍max函数. 1.torch.max(data),不指定维度,返回data的最大值. 2.torch.max(data,dim),返回data中指定维度的最大值. 3.to ...

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

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

最新文章

  1. 运维企业专题(8)LVS高可用与负载均衡后篇——LVS健康检查与高可用详解
  2. 厌倦了SWT TABLE,何不试试KTABLE?
  3. java B2B2C电子商务平台分析之十五-----EureKa服务注册与发现
  4. 优先队列 HDOJ 5437 Alisha's Party
  5. Sql如何统计连续打卡天数
  6. 【js拾遗】名称空间
  7. java的标量和聚合量_JVM 角度看代码优化
  8. CentOS7下使用yum快速安装配置oracle数据库
  9. excel比较两列数据,相同?包含?
  10. 【2020年TI杯江苏省大学生电子设计竞赛回顾——C题:坡道行驶电动小车(江苏省二等奖)】
  11. 为什么文本框里的字只显示一半_Word文本框文字显示不全、无法选择、不能编辑调整大小和跨页,怎么解决...
  12. 联想Lephone与Apple iPAD的完美组合
  13. 15分钟详解 Python 安全认证的那些事儿~
  14. UT-Exynos4412开发板三星ARM四核旗舰开发平台android4.0体验-11有线网络功能调试
  15. 为什么一个数的平方,会变负数?结果令人惊讶(sq代码解析)
  16. 笔记 GWAS 操作流程2-3:最小等位基因频率
  17. ERP项目实施过程中的致命过失(转)
  18. C语言----取反~
  19. linux开放端口命令
  20. java基础综合练习(嗖嗖移动)

热门文章

  1. PL/SQL TOAD 不安装Oracle客户端连接数据库的方法
  2. oracle 锁表查询及解决、表字段查询
  3. [转载] 了解Node.js-to-Angular 套件组件
  4. 基本概念----Beginning Visual C#
  5. RouterOS安装以及搭建DHCP PPPoE PPTP L2TP服务
  6. zabbix--基础概念及原理
  7. MySQL数据库篇之索引原理与慢查询优化之一
  8. 算法总结之 在数组中找到一个局部最小的位置
  9. django-celery beat报错 error pid
  10. javascript中的对象之间继承关系