DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

aminmax(), amin() and amax() in PyTorch

*Memos:

aminmax() can get the one or more minimum and maximum values of a 0D or more D tensor as shown below:

*Memos:

  • aminmax() can be called both from torch and a tensor.
  • Setting a dimension(dim) to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st minimum and maximum values. *You must use dim=.
  • Only zero or more integers, floating-point numbers or boolean values can be used so zero or more complex numbers cannot be used except the 0D tensor of a complex number with dim=0 or dim=-1.
import torch

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

torch.aminmax(my_tensor, dim=0)
my_tensor.aminmax(dim=0)
torch.aminmax(my_tensor, dim=-2)
my_tensor.aminmax(dim=-2)
# torch.return_types.aminmax(
# min=tensor([3, 4, 3, 3]),
# max=tensor([6, 8, 9, 7]))

torch.aminmax(my_tensor, dim=1)
my_tensor.aminmax(dim=1)
torch.aminmax(my_tensor, dim=-1)
my_tensor.aminmax(dim=-1)
# torch.return_types.aminmax(
# min=tensor([4, 3, 3]),
# max=tensor([7, 6, 9]))

my_tensor = torch.tensor([[5., 4., 7., 7.],
                          [6., True, 3., 5.],
                          [3., 8., False, 3.]])
torch.aminmax(my_tensor, dim=0)
my_tensor.aminmax(dim=0)
torch.aminmax(my_tensor, dim=-2)
my_tensor.aminmax(dim=-2)
# torch.return_types.aminmax(
# min=tensor([3., 1., 0., 3.]),
# max=tensor([6., 8., 7., 7.]))

my_tensor = torch.tensor(5+7j)
torch.aminmax(my_tensor, dim=0)
my_tensor.aminmax(dim=0)
torch.aminmax(my_tensor, dim=-1)
my_tensor.aminmax(dim=-1)
# torch.return_types.aminmax(
# min=tensor(5.+7.j),
# max=tensor(5.+7.j))
Enter fullscreen mode Exit fullscreen mode

amin() can get the one or more minimum values of a 0D or more D tensor as shown below:

*Memos:

  • amin() can be called both from torch and a tensor.
  • Setting a dimension(dim) to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st minimum values. *You must use dim=.
  • Only zero or more integers, floating-point numbers or boolean values can be used so zero or more complex numbers cannot be used.
import torch

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

torch.amin(my_tensor, dim=0)
my_tensor.amin(dim=0)
torch.amin(my_tensor, dim=-2)
my_tensor.amin(dim=-2)
# tensor([3, 4, 3, 3])

torch.amin(my_tensor, dim=1)
my_tensor.amin(dim=1)
torch.amin(my_tensor, dim=-1)
my_tensor.amin(dim=-1)
# tensor([4, 3, 3])

my_tensor = torch.tensor([[5., 4., 7., 7.],
                          [6., True, 3., 5.],
                          [3., 8., False, 3.]])
torch.amin(my_tensor, dim=0)
my_tensor.amin(dim=0)
torch.amin(my_tensor, dim=-2)
my_tensor.amin(dim=-2)
# tensor([3., 1., 0., 3.])
Enter fullscreen mode Exit fullscreen mode

amax() can get the one or more maximum values of a 0D or more D tensor as shown below:

*Memos:

  • amax() can be called both from torch and a tensor.
  • Setting a dimension(dim) to the 2nd argument with torch or the 1st argument with a tensor gets zero or more 1st maximum values. *You must use dim=.
  • Only zero or more integers, floating-point numbers or boolean values can be used so zero or more complex numbers cannot be used.
import torch

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

torch.amax(my_tensor, dim=0)
my_tensor.amax(dim=0)
torch.amax(my_tensor, dim=-2)
my_tensor.amax(dim=-2)
# tensor([6, 8, 9, 7])

torch.amax(my_tensor, dim=1)
my_tensor.amax(dim=1)
torch.amax(my_tensor, dim=-1)
my_tensor.amax(dim=-1)
# tensor([7, 6, 9])

my_tensor = torch.tensor([[5., 4., 7., 7.],
                          [6., True, 3., 5.],
                          [3., 8., False, 3.]])
torch.amax(my_tensor, dim=0)
my_tensor.amax(dim=0)
torch.amax(my_tensor, dim=-2)
my_tensor.amax(dim=-2)
# tensor([6., 8., 7., 7.])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)