DEV Community

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

Posted on • Edited on

aminmax, amin and amax in PyTorch

Buy Me a Coffee

*Memos:

aminmax() can get two of the 0D or more D tensors of zero or more minimum and maximum elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • aminmax() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • There is dim argument with torch or a tensor(Optional-Type:int): *Memos:
    • Setting dim can get zero or more 1st minimum and maximum elements.
    • You must use dim=.
  • There is keepdim argument with torch or a tensor(Optional-Default:False-Type:bool): *Memos:
    • You must use keepdim=.
    • My post explains keepdim argument.
  • There is out argument with torch(Optional-Default:None-Type:tuple(tensor, tensor) or list(tensor, tensor)): *Memos:
    • out= must be used.
    • My post explains out argument.
  • The 0D tensor of one complex number with dim=0 or dim=-1 works.
  • An empty 2D or more D input tensor or tensor doesn't work if not setting dim.
  • An empty 1D input tensor or tensor doesn't work even if setting dim.
import torch

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

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

torch.aminmax(input=my_tensor, dim=1)
torch.aminmax(input=my_tensor, 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., 5., 3., 5.],
                          [3., 8., 9., 3.]])
torch.aminmax(input=my_tensor)
# torch.return_types.aminmax(
# min=tensor(3.),
# max=tensor(9.))

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True],
                          [True, False, True, False]])
torch.aminmax(input=my_tensor)
# torch.return_types.aminmax(
# min=tensor(False),
# max=tensor(True))

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

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

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

torch.aminmax(input=my_tensor) # Error

my_tensor = torch.tensor([])

torch.aminmax(input=my_tensor, dim=0) # Error

my_tensor = torch.tensor([[]])

torch.aminmax(input=my_tensor, dim=0)
# torch.return_types.aminmax(
# min=tensor([]),
# max=tensor([]))

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

torch.aminmax(input=my_tensor, dim=0)
# torch.return_types.aminmax(
# min=tensor([], size=(1, 0)),
# max=tensor([], size=(1, 0)))
Enter fullscreen mode Exit fullscreen mode

amin() can get the 0D or more D tensor of zero or more minimum elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • amin() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is dim(Optional-Type:int, tuple of int or list of int). *Setting dim can get zero or more 1st minimum elements.
  • The 3rd argument with torch or the 2nd argument with a tensor is keepdim(Optional-Default:False-Type:bool). *My post explains keepdim argument.
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • An empty 2D or more D input tensor or tensor doesn't work if not setting dim.
  • An empty 1D input tensor or tensor doesn't work even if setting dim.
import torch

my_tensor = torch.tensor([[5, 4, 7, 7],
                          [6, 5, 3, 5],
                          [3, 8, 9, 3]])
torch.amin(input=my_tensor)
my_tensor.amin()
torch.amin(input=my_tensor, dim=(0, 1))
torch.amin(input=my_tensor, dim=(0, -1))
torch.amin(input=my_tensor, dim=(1, 0))
torch.amin(input=my_tensor, dim=(1, -2))
torch.amin(input=my_tensor, dim=(-1, 0))
torch.amin(input=my_tensor, dim=(-1, -2))
torch.amin(input=my_tensor, dim=(-2, 1))
torch.amin(input=my_tensor, dim=(-2, -1))
# tensor(3)

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

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

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

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True],
                          [True, False, True, False]])
torch.amin(input=my_tensor)
# tensor(False)

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

torch.amin(input=my_tensor) # Error

my_tensor = torch.tensor([])

torch.amin(input=my_tensor, dim=0) # Error

my_tensor = torch.tensor([[]])

torch.amin(input=my_tensor, dim=0)
# tensor([])

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

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

amax() can get the 0D or more D tensor of zero or more maximum elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • amax() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float or bool).
  • The 2nd argument with torch or the 1st argument with a tensor is dim(Optional-Type:int, tuple of int or list of int). *Setting dim can get zero or more 1st maximum elements.
  • The 3rd argument with torch or the 2nd argument is keepdim(Optional-Default:False-Type:bool). *My post explains keepdim argument.
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • An empty 2D or more D input tensor or tensor doesn't work if not setting dim.
  • An empty 1D input tensor or tensor doesn't work even if setting dim.
import torch

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

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

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

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

my_tensor = torch.tensor([[True, False, True, False],
                          [False, True, False, True],
                          [True, False, True, False]])
torch.amax(input=my_tensor)
# tensor(True)

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

torch.amax(input=my_tensor) # Error

my_tensor = torch.tensor([])

torch.amax(input=my_tensor, dim=0) # Error

my_tensor = torch.tensor([[]])

torch.amax(input=my_tensor, dim=0)
# tensor([])

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

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay