min() and max() in PyTorch
Tags: pytorch, min, max, function
Published: 2024-07-04 17:25:11

*Memos:
- [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/aminmax-amin-and-amax-in-pytorch-25ji) explains [aminmax()](https://pytorch.org/docs/stable/generated/torch.aminmax.html), [amin()](https://pytorch.org/docs/stable/generated/torch.amin.html) and [amax()](https://pytorch.org/docs/stable/generated/torch.amax.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).

[min()](https://pytorch.org/docs/stable/generated/torch.min.html) can get the 0D of the 1st one minimum element or two of the 0D or more D tensors of the 1st zero or more minimum elements and their indices from the one or two 0D or more D tensors of zero or more elements as shown below:

*Memos:
- `min()` 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`).
- The 2nd argument with `torch` or the 1st argument is `dim`(Optional-Type:`int`). *Setting `dim` can get zero or more 1st minimum elements and their indices.
- The 2nd argument with `torch` or the 1st argument is `other`(Optional-Type:`tensor` of `int`, `float` or `bool`).
  *Memos:
  - It can only be used with `input`.
  - This is the functionality of [minimum()](https://pytorch.org/docs/stable/generated/torch.minimum.html).
- The 3rd argument with `torch` or the 2nd argument is `keepdim`(Optional-Type:`bool`).
  *Memos:
  - It must be used with `dim` without `other`.
  - [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` or `tuple`(`tensor`, `tensor`):
  *Memos:
  - The type of `tensor` must be used without `dim` and `keepdim`.
  - The type of `tuple`(`tensor`, `tensor`) must be used with `dim` without `other`.
  - `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 without `other` tensor doesn't work if not setting `dim`.
- Empty 1D `input` tesnor without `other` tensor 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.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]))

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

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

torch.min(input=my_tensor) # Error

my_tensor = torch.tensor([])

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

my_tensor = torch.tensor([[]])

torch.min(input=my_tensor, dim=0)
# torch.return_types.min(
# values=tensor([]),
# indices=tensor([], dtype=torch.int64))

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

torch.min(input=my_tensor, dim=0)
# torch.return_types.min(
# values=tensor([], size=(1, 0)),
# indices=tensor([], size=(1, 0), dtype=torch.int64))
```

[max()](https://pytorch.org/docs/stable/generated/torch.max.html) can get the 0D of the 1st one maximum element or two of the 0D or more D tensors of the 1st zero or more maximum elements and their indices from the one or two 0D or more D tensors of zero or more elements as shown below:

- `max()` 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 is `dim`(Optional-Type:`int`). *Setting `dim` can get zero or more 1st maximum elements and their indices.
- The 2nd argument with `torch` or the 1st argument is `other`(Optional-Type:`tensor` of `int`, `float` or `bool`).
  *Memos:
  - It can only be used with `input`.
  - This is the functionality of [maximum()](https://pytorch.org/docs/stable/generated/torch.maximum.html).
- The 3rd argument with `torch` or the 2nd argument is `keepdim`(Optional-Type:`bool`).
  *Memos:
  - It must be used with `dim` without `other`.
  - [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` or `tuple`(`tensor`, `tensor`):
  *Memos:
  - The type of `tensor` must be used without `dim` and `keepdim`.
  - The type of `tuple`(`tensor`, `tensor`) must be used with `dim` without `other`.
  - `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 without `other` tensor doesn't work if not setting `dim`.
- Empty 1D `input` tesnor without `other` tensor 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.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]))

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

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

torch.max(input=my_tensor) # Error

my_tensor = torch.tensor([])

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

my_tensor = torch.tensor([[]])

torch.max(input=my_tensor, dim=0)
# torch.return_types.max(
# values=tensor([]),
# indices=tensor([], dtype=torch.int64))

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

torch.max(input=my_tensor, dim=0)
# torch.return_types.max(
# values=tensor([], size=(1, 0)),
# indices=tensor([], size=(1, 0), dtype=torch.int64))
```