博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何使用PyTorch torch.max()
阅读量:2531 次
发布时间:2019-05-11

本文共 4405 字,大约阅读时间需要 14 分钟。

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 , instead of a single element.

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

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 maximummax_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 ! Stay tuned for more!

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

参考资料 (References)

  • on torch.max()

    关于torch.max()的


翻译自:

转载地址:http://yqozd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_5-01分布式核心知识之熔断、降级
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-05熔断降级服务异常报警通知
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-03 高级篇幅之zuul常用问题分析
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-08 断路器监控仪表参数
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-6.微信扫码登录回调本地域名映射工具Ngrock...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-8.用户模块开发之保存微信用户信息...
查看>>
Linux下Nginx安装
查看>>
LVM扩容之xfs文件系统
查看>>
Hbase记录-client访问zookeeper大量断开以及参数调优分析(转载)
查看>>