DEV Community

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

Posted on • Edited on

fmin and fmax in PyTorch

Buy Me a Coffee

*Memos:

fmin() 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(input) with torch or using a tensor(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-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • A number is taken if there are a number and nan.
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]]])
Enter fullscreen mode Exit fullscreen mode

fmax() 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(input) with torch or using a tensor(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-Default:None-Type:tensor): *Memos:
  • A number is taken if there are a number and nan.
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]]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)