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

Super Kai (Kazuya Ito) - Apr 18 - - Dev Community

*Memos:

min() can get the one or more minimum elements of a 0D or more D tensor from one or two of 0D or more D tensors as shown below:

*Memos:

  • min() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float or bool) with torch or using a tensor(tensor of int, float or bool) is input(Required).
  • The 2nd argument(int) with torch or the 1st argument(int) is dim(Optional). *Setting dim can get one or more 1st minimum elements and the indices of them.
  • The 2nd argument(tensor of int, float or bool) with torch or the 1st argument(tensor of int, float or bool) is other(Optional). *Memos:
    • It can only be used with input.
    • This is the functionality of minimum().
  • The 3rd argument(bool) with torch or the 2nd argument(bool) is keepdim(Optional). *Memos:
    • It keeps the dimension of the input tensor.
    • It must be used with dim.
import torch

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

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

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

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

tensor1 = torch.tensor([5, 4, 7, 7])
tensor2 = torch.tensor([[6, 5, 3, 5],
                        [3, 8, 9, 3]])
torch.min(input=tensor1, other=tensor2)
# tensor([[5, 4, 3, 5],
#         [3, 4, 7, 3]])

tensor1 = torch.tensor([5., 4., 7., 7.])
tensor2 = torch.tensor([[6., 5., 3., 5.],
                        [3., 8., 9., 3.]])
torch.min(input=tensor1, other=tensor2)
# tensor([[5., 4., 3., 5.],
#         [3., 4., 7., 3.]])

tensor1 = torch.tensor([True, False, True, False])
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.min(input=tensor1, other=tensor2)
# tensor([[True, False, True, False],
#         [False, False, False, False]])
Enter fullscreen mode Exit fullscreen mode

max() can get the one or more maximum elements of a 0D or more D tensor from one or two of 0D or more D tensors as shown below:

  • The 1st argument(tensor of int, float or bool) with torch or using a tensor(tensor of int, float or bool) is input(Required).
  • The 2nd argument(int or tuple of int) with torch or the 1st argument(int or tuple of int) is dim(Optional). *Setting dim can get one or more 1st maximum elements and the indices of them.
  • The 2nd argument(tensor of int, float or bool) with torch or the 1st argument(tensor of int, float or bool) is other(Optional). *Memos:
    • It can only be used with input.
    • This is the functionality of maximum().
  • The 3rd argument(bool) with torch or the 2nd argument(bool) is keepdim(Optional). *Memos:
    • It keeps the dimension of the input tensor.
    • It must be used with dim.
import torch

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

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

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

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

tensor1 = torch.tensor([5, 4, 7, 7])
tensor2 = torch.tensor([[6, 5, 3, 5],
                        [3, 8, 9, 3]])
torch.max(input=tensor1, other=tensor2)
# tensor([[6, 5, 7, 7],
#         [5, 8, 9, 7]])

tensor1 = torch.tensor([5., 4., 7., 7.])
tensor2 = torch.tensor([[6., 5., 3., 5.],
                        [3., 8., 9., 3.]])
torch.max(input=tensor1, other=tensor2)
# tensor([[6., 5., 7., 7.],
#         [5., 8., 9., 7.]])

tensor1 = torch.tensor([True, False, True, False])
tensor2 = torch.tensor([[True, False, True, False],
                        [False, True, False, True]])
torch.max(input=tensor1, other=tensor2)
# tensor([[True, False, True, False],
#         [True, True, True, True]])
Enter fullscreen mode Exit fullscreen mode

argmin() can get the one or more indices of the 1st minimum elements of a 0D or more D tensor from a 0D or more D tensor as shown below:

*Memos:

  • argmin() can be used with torch or a tensor.
  • The 1st argument(tensor of int or float) with torch or using a tensor(tensor of int or float) is input(Required).
  • The 2nd argument(int) with torch or the 1st argument(int) is dim(Optional). *Setting dim can get the one or more indices of the 1st minimum elements.
  • One complex number or boolean value of a 1D or more D tensor with dim= works.
import torch

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

torch.argmin(input=my_tensor, dim=0)
torch.argmin(input=my_tensor, dim=-2)
# tensor([2, 0, 1, 2])

torch.argmin(input=my_tensor, dim=1)
torch.argmin(input=my_tensor, dim=-1)
# tensor([1, 2, 0])

torch.argmin(input=my_tensor, keepdim=True)
# tensor([[6]])

torch.argmin(input=my_tensor, dim=0, keepdim=True)
# tensor([[2, 0, 1, 2]])

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

my_tensor = torch.tensor([5+7j])

torch.argmin(input=my_tensor, dim=0)
torch.argmin(input=my_tensor, dim=-1)
# tensor(0)

my_tensor = torch.tensor([[True]])

torch.argmin(input=my_tensor, dim=0)
torch.argmin(input=my_tensor, dim=-1)
# tensor([0])
Enter fullscreen mode Exit fullscreen mode

argmax() can get the indices of the 1st maximum elements of a 0D or more D tensor from a 0D or more D tensor as shown below:

*Memos:

  • argmax() can be called both from torch and a tensor.
  • The 1st argument(tensor of int or float) with torch or using a tensor(tensor of int or float) is input(Required).
  • The 2nd argument(int) with torch or the 1st argument(int) is dim(Optional). *Setting dim can get the one or more indices of the 1st maximum elements.
  • One complex number or boolean value of a 1D or more D tensor with dim= works.
import torch

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

torch.argmax(input=my_tensor, dim=0)
torch.argmax(input=my_tensor, dim=-2)
# tensor([1, 2, 2, 0])

torch.argmax(input=my_tensor, dim=1)
torch.argmax(input=my_tensor, dim=-1)
# tensor([2, 0, 2])

torch.argmax(input=my_tensor, keepdim=True)
# tensor([[10]])

torch.argmax(input=my_tensor, dim=0, keepdim=True)
# tensor([[1, 2, 2, 0]])

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

my_tensor = torch.tensor([5+7j])

torch.argmax(input=my_tensor, dim=0)
torch.argmax(input=my_tensor, dim=-1)
# tensor(0)

my_tensor = torch.tensor([[True]])

torch.argmax(input=my_tensor, dim=0)
torch.argmax(input=my_tensor, dim=-1)
# tensor([0])
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .