## DEV Community

Super Kai (Kazuya Ito)

Posted on • Updated on

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

*Memos:

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

*Memos:

• `aminmax()` 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).
• There is `dim` argument(`int`) (Optional) with torch or a tensor. *Memos:
• Setting `dim` can get one or more 1st minimum and maximum elements.
• You must use `dim=`.
• There is `keepdim` argument(`bool`) (Optional-Default:`False`) with torch or a tensor. *Memos:
• It keeps the dimension of the `input` tensor.
• You must use `keepdim=`.
• One complex number of a 0D tensor with `dim=0` or `dim=-1` works.
``````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]))

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

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

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+7j)

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))
``````

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

*Memos:

• `amin()` 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` 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 minimum elements.
• The 3rd argument(`bool`) with `torch` or the 2nd argument(`bool`) is `keepdim`(Optional). *It keeps the dimension of the `input` tensor.
``````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])

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

torch.amin(input=my_tensor, dim=0, keepdim=True)
# tensor([[3, 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)
``````

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

*Memos:

• `amax()` 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` 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.
• The 3rd argument(`bool`) with `torch` or the 2nd argument(`bool`) is `keepdim`(Optional). *It keeps the dimension of the `input` tensor.
``````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])

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

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

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)
``````