min(), max(), argmin() and argmax() in PyTorch

Rmag Breaking News

*My post explains sum() and mean().

min() can get the minimum value as shown below:

*Memos:

min() can be called both from torch and a tensor.
Setting a dimension to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st minimum values and the indices of them.

import torch

my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.min(my_tensor)
my_tensor.min()
# tensor(3)

torch.min(my_tensor, 0)
my_tensor.min(0)
torch.min(my_tensor, 2)
my_tensor.min(2)
# torch.return_types.min(
# values=tensor([3, 4, 3, 3]),
# indices=tensor([2, 0, 1, 2]))

torch.min(my_tensor, 1)
my_tensor.min(1)
torch.min(my_tensor, 1)
my_tensor.min(1)
# torch.return_types.min(
# values=tensor([4, 3, 3]),
# indices=tensor([1, 2, 0]))

max() can get the maximum value as shown below:

*Memos:

max() can be called both from torch and a tensor.
Setting a dimension to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st maximum values and the indices of them.

import torch

my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.max(my_tensor)
my_tensor.max()
# tensor(9)

torch.max(my_tensor, 0)
my_tensor.max(0)
torch.max(my_tensor, 2)
my_tensor.max(2)
# torch.return_types.max(
# values=tensor([6, 8, 9, 7]),
# indices=tensor([1, 2, 2, 0]))

torch.max(my_tensor, 1)
my_tensor.max(1)
torch.max(my_tensor, 1)
my_tensor.max(1)
# torch.return_types.max(
# values=tensor([7, 6, 9]),
# indices=tensor([2, 0, 2]))

argmin() can get the indices of the 1st minimum values as shown below:

*Memos:

argmin() can be called both from torch and a tensor.
The 2nd argument is a dimension with torch.
The 1st argument is a dimension with a tensor.

import torch

my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.argmin(my_tensor)
my_tensor.argmin()
# tensor(6)

torch.argmin(my_tensor, 0)
my_tensor.argmin(0)
torch.argmin(my_tensor, 2)
my_tensor.argmin(2)
# tensor([2, 0, 1, 2])

torch.argmin(my_tensor, 1)
my_tensor.argmin(1)
torch.argmin(my_tensor, 1)
my_tensor.argmin(1)
# tensor([1, 2, 0])

argmax() can get the indices of the 1st maximum values:

*Memos:

argmax() can be called both from torch and a tensor.
The 2nd argument is a dimension with torch.
The 1st argument is a dimension with a tensor.

import torch

my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.argmax(my_tensor)
my_tensor.argmax()
# tensor(10)

torch.argmax(my_tensor, 0)
my_tensor.argmax(0)
torch.argmax(my_tensor, 2)
my_tensor.argmax(2)
# tensor([1, 2, 2, 0])

torch.argmax(my_tensor, 1)
my_tensor.argmax(1)
torch.argmax(my_tensor, 1)
my_tensor.argmax(1)
# tensor([2, 0, 2])

Leave a Reply

Your email address will not be published. Required fields are marked *