minimum(), maximum(), fmin() and fmax() in PyTorch
Tags: pytorch, minimum, maximum, fmin
Published: 2024-05-20 23:16:08

*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/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/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).

[minimum()](https://pytorch.org/docs/stable/generated/torch.minimum.html) can get the 0D or more D tensor of zero or more minimum elements prioritizing `nan` from two of the 0D or more D tensors of zero or more elements as shown below:

*Memos:
- `minimum()` 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 `other`(Required-Type:`tensor` of `int`, `float` or `bool`).
- 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.
- `nan` is taken if there are a number and `nan`.

```python
import torch

tensor1 = torch.tensor([5., float('nan'), 4., float('nan')])
tensor2 = torch.tensor([[7., 8., float('nan'), float('nan')],
                        [-9., 2., 0., -6.]])
torch.minimum(input=tensor1, other=tensor2)
tensor1.minimum(other=tensor2)
# tensor([[5., nan, nan, nan],
#         [-9., nan, 0., nan]])

tensor1 = torch.tensor(5.)
tensor2 = torch.tensor([[[7., 8.], [float('nan'), float('nan')]],
                        [[-9., 2.], [0., -6.]]])
torch.minimum(input=tensor1, other=tensor2)
# tensor([[[5., 5.], [nan, nan]],
#         [[-9., 2.], [0., -6.]]])

tensor1 = torch.tensor(5)
tensor2 = torch.tensor([[[7, 8], [-5, -1]],
                        [[-9, 2], [0, -6]]])
torch.minimum(input=tensor1, other=tensor2)
# tensor([[[5, 5], [-5, -1]],
#         [[-9, 2], [0, -6]]])

tensor1 = torch.tensor(True)
tensor2 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
torch.minimum(input=tensor1, other=tensor2)
# tensor([[[True, False], [True, False]],
#         [[False, True], [False, True]]])
```

[maximum()](https://pytorch.org/docs/stable/generated/torch.maximum.html) can get the 0D or more D tensor of zero or more maximum elements prioritizing `nan` from two of the 0D or more D tensors of zero or more elements as shown below:

*Memos:
- `maximum()` 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 `other`(Required-Type:`tensor` of `int`, `float` or `bool`).
- 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.
- `nan` is taken if there are a number and `nan`.

```python
import torch

tensor1 = torch.tensor([5., float('nan'), 4., float('nan')])
tensor2 = torch.tensor([[7., 8., float('nan'), float('nan')],
                        [-9., 2., 0., -6.]])
torch.maximum(input=tensor1, other=tensor2)
tensor1.maximum(other=tensor2)
# tensor([[7., nan, nan, nan],
#         [5., nan, 4., nan]])

tensor1 = torch.tensor(5.)
tensor2 = torch.tensor([[[7., 8.], [float('nan'), float('nan')]],
                        [[-9., 2.], [0., -6.]]])
torch.maximum(input=tensor1, other=tensor2)
# tensor([[[7., 8.], [nan, nan]],
#         [[5., 5.], [5., 5.]]])

tensor1 = torch.tensor(5)
tensor2 = torch.tensor([[[7, 8], [-5, -1]],
                        [[-9, 2], [0, -6]]])
torch.maximum(input=tensor1, other=tensor2)
# tensor([[[7, 8], [5, 5]],
#         [[5, 5], [5, 5]]])

tensor1 = torch.tensor(True)
tensor2 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
torch.maximum(input=tensor1, other=tensor2)
# tensor([[[True, True], [True, True]],
#         [[True, True], [True, True]]])
```

[fmin()](https://pytorch.org/docs/stable/generated/torch.fmin.html) can get the 0D or more D tensor of zero or more minimum elements not prioritizing `nan` from two of the 0D or more D tensors of zero or more elements as shown below:

*Memos:
- `fmin()` 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 `other`(Required-Type:`tensor` of `int`, `float` or `bool`).
- 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.
- A number is taken if there are a number and `nan`.  

```python
import torch

tensor1 = torch.tensor([5., float('nan'), 4., float('nan')])
tensor2 = torch.tensor([[7., 8., float('nan'), float('nan')],
                        [-9., 2., 0., -6.]])
torch.fmin(input=tensor1, other=tensor2)
tensor1.fmin(other=tensor2)
# tensor([[5., 8., 4., nan],
#         [-9., 2., 0., -6.]])

tensor1 = torch.tensor(5.)
tensor2 = torch.tensor([[[7., 8.], [float('nan'), float('nan')]],
                        [[-9., 2.], [0., -6.]]])
torch.fmin(input=tensor1, other=tensor2)
# tensor([[[5., 5.], [5., 5.]],
#         [[-9., 2.], [0., -6.]]])

tensor1 = torch.tensor(5)
tensor2 = torch.tensor([[[7, 8], [-5, -1]],
                        [[-9, 2], [0, -6]]])
torch.fmin(input=tensor1, other=tensor2)
# tensor([[[5, 5], [-5, -1]],
#         [[-9, 2], [0, -6]]])

tensor1 = torch.tensor(True)
tensor2 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
torch.fmin(input=tensor1, other=tensor2)
# tensor([[[True, False], [True, False]],
#         [[False, True], [False, True]]])
```

[fmax()](https://pytorch.org/docs/stable/generated/torch.fmax.html) can get the 0D or more D tensor of zero or more maximum elements not prioritizing `nan` from two of the 0D or more D tensors of zero or more elements as shown below:

*Memos:
- `fmax()` 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 `other`(Required-Type:`tensor` of `int`, `float` or `bool`).
- 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.
- A number is taken if there are a number and `nan`.  

```python
import torch

tensor1 = torch.tensor([5., float('nan'), 4., float('nan')])
tensor2 = torch.tensor([[7., 8., float('nan'), float('nan')],
                        [-9., 2., 0., -6.]])
torch.fmax(input=tensor1, other=tensor2)
tensor1.fmax(other=tensor2)
# tensor([[7., 8., 4., nan],
#         [5., 2., 4., -6.]])

tensor1 = torch.tensor(5.)
tensor2 = torch.tensor([[[7., 8.], [float('nan'), float('nan')]],
                        [[-9., 2.], [0., -6.]]])
torch.fmax(input=tensor1, other=tensor2)
# tensor([[[7., 8.], [5., 5.]],
#         [[5., 5.], [5., 5.]]])

tensor1 = torch.tensor(5)
tensor2 = torch.tensor([[[7, 8], [-5, -1]],
                        [[-9, 2], [0, -6]]])
torch.fmax(input=tensor1, other=tensor2)
# tensor([[[7, 8], [5, 5]],
#         [[5, 5], [5, 5]]])

tensor1 = torch.tensor(True)
tensor2 = torch.tensor([[[True, False], [True, False]],
                        [[False, True], [False, True]]])
torch.fmax(input=tensor1, other=tensor2)
# tensor([[[True, True], [True, True]],
#         [[True, True], [True, True]]])
```