本文共 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
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.
现在,此函数返回张量中元素之间的最大值。
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!
确实,这使我们在张量中具有全局最大元素!
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_elements
和max_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).
确实,我们获得了沿着该行的最大元素以及相应的索引(沿着该行)。
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.
在此, a
和b
必须具有相同的尺寸,或者必须是“可广播的”张量。
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
.
实际上,我们得到的输出张量具有在p
和q
之间的最大元素。
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!
对于类似的文章,请在我们的浏览我们的内容! 敬请期待更多!
翻译自:
转载地址:http://yqozd.baihongyu.com/