aminmax(), amin() and amax() in PyTorch
Tags: pytorch, aminmax, amin, amax
Published: 2024-04-18 04:07:54

*Memos:
- [My post](https://dev.to/hyperkai/min-and-max-in-pytorch-3ol8) explains [min()](https://pytorch.org/docs/stable/generated/torch.min.html) and [max()](https://pytorch.org/docs/stable/generated/torch.max.html).
- [My post](https://dev.to/hyperkai/argmin-and-argmax-in-pytorch-580g) explains [argmin()](https://pytorch.org/docs/stable/generated/torch.argmin.html) and [argmax()](https://pytorch.org/docs/stable/generated/torch.argmax.html).
- [My post](https://dev.to/hyperkai/minimum-maximum-fmin-and-fmax-in-pytorch-2hj) explains [minimum()](https://pytorch.org/docs/stable/generated/torch.minimum.html), [maximum()](https://pytorch.org/docs/stable/generated/torch.maximum.html). [fmin()](https://pytorch.org/docs/stable/generated/torch.fmin.html) and [fmax()](https://pytorch.org/docs/stable/generated/torch.fmax.html).
- [My post](https://dev.to/hyperkai/kthvalue-and-topk-in-pytorch-njk) explains [kthvalue()](https://pytorch.org/docs/stable/generated/torch.kthvalue.html) and [topk()](https://pytorch.org/docs/stable/generated/torch.topk.html).

[aminmax()](https://pytorch.org/docs/stable/generated/torch.aminmax.html) 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](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(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](https://dev.to/hyperkai/set-keepdim-with-keepdim-argument-functions-pytorch-2fdj) explains `keepdim` argument.
- There is `out` argument with `torch`(Optional-Type:`tuple`(`tensor`, `tensor`) or `list`(`tensor`, `tensor`)):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- The 0D tensor of one complex number with `dim=0` or `dim=-1` works.
- Empty 2D or more D `input` tensor doesn't work if not setting `dim`.
- Empty 1D `input` tesnor doesn't work even if setting `dim`.

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

[amin()](https://pytorch.org/docs/stable/generated/torch.amin.html) 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 with `torch` or using a tensor is `input`(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-Type:`bool`). *[My post](https://dev.to/hyperkai/set-keepdim-with-keepdim-argument-functions-pytorch-2fdj) explains `keepdim` argument.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- Empty 2D or more D `input` tensor doesn't work if not setting `dim`.
- Empty 1D `input` tesnor doesn't work even if setting `dim`.

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

[amax()](https://pytorch.org/docs/stable/generated/torch.amax.html) 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 with `torch` or using a tensor is `input`(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-Type:`bool`). *[My post](https://dev.to/hyperkai/set-keepdim-with-keepdim-argument-functions-pytorch-2fdj) explains `keepdim` argument.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- Empty 2D or more D `input` tensor doesn't work if not setting `dim`.
- Empty 1D `input` tesnor doesn't work even if setting `dim`.

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