DEV Community

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

Posted on • Updated on

isfinite(), isreal() and isin() in PyTorch

*Memos:

isfinite() can check if the zero or more elements of a 0D or more D tensor are finity as shown below:

*Memos:

  • isfinite() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
import torch

my_tensor = torch.tensor(float('nan'))

torch.isfinite(input=my_tensor)
my_tensor.isfinite()
# tensor(False)

my_tensor = torch.tensor([float('nan'),
                          -5,
                          float('inf'),
                          8.,
                          float('-inf'),
                          3.+0.j,
                          3.+7.j,
                          True])
torch.isfinite(input=my_tensor)
# tensor([False, True, False, True, False, True, True, True])

my_tensor = torch.tensor([[float('nan'), -5, float('inf'), 8.],
                          [float('-inf'), 3.+0.j, 3.+7.j, True]])
torch.isfinite(input=my_tensor)
# tensor([[False, True, False, True],
#         [False, True, True, True]])

my_tensor = torch.tensor([[[float('nan'), -5], [float('inf'), 8.]],
                          [[float('-inf'), 3.+0.j], [3.+7.j, True]]])
torch.isfinite(input=my_tensor)
# tensor([[[False, True], [False, True]],
#         [[False, True], [True, True]]])
Enter fullscreen mode Exit fullscreen mode

isreal() can check if the zero or more elements of a 0D or more D tensor are real-valued as shown below:

*Memos:

  • isreal() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
import torch

my_tensor = torch.tensor(float('nan'))

torch.isreal(input=my_tensor)
my_tensor.isreal()
# tensor(True)

my_tensor = torch.tensor([float('nan'),
                          -5,
                          float('inf'),
                          8.,
                          float('-inf'),
                          3.+0.j,
                          3.+7.j,
                          True])
torch.isreal(input=my_tensor)
# tensor([True, True, True, True, True, True, False, True])

my_tensor = torch.tensor([[float('nan'), -5, float('inf'), 8.],
                          [float('-inf'), 3.+0.j, 3.+7.j, True]])
torch.isreal(input=my_tensor)
# tensor([[True, True, True, True],
#         [True, True, False, True]])

my_tensor = torch.tensor([[[float('nan'), -5], [float('inf'), 8.]],
                          [[float('-inf'), 3.+0.j], [3.+7.j, True]]])
torch.isreal(input=my_tensor)
# tensor([[[True, True], [True, True]],
#         [[True, True], [False, True]]])
Enter fullscreen mode Exit fullscreen mode

isin() can check if the zero or more elements of the 1st 0D or more D tensor contain the zero or more elements of the 2nd 0D or more D tensor as shown below:

*Memos:

  • isin() can be used with torch but not with a tensor.
  • The 1st argument(tensor or scalar of int or float) with torch is elements(Required). *torch must use a scalar without elements=.
  • The 2nd argument(tensor or scalar of int or float) with torch is test_elements(Required). *torch must use a scalar without test_elements=.
  • The 3rd argument(bool) with torch is assume_unique(Optional-Default=False). *If True, assumes both elements and test_elements contain unique elements, which can speed up the calculation.
  • The 4th argument(bool) with torch is invert(Optional-Default=False). *If True, inverts the boolean return tensor, resulting in True values for elements not in test_elements.
  • The combination of a scalar(elements) and a scalar (test_elements) cannot be used.
import torch

tensor1 = torch.tensor([0, 1, 2, 3])
tensor2 = torch.tensor(2)

torch.isin(elements=tensor1, test_elements=tensor2)
torch.isin(tensor1, 2)
# tensor([False, False, True, False])

torch.isin(elements=tensor1, test_elements=tensor2, 
           assume_unique=True, invert=True)
torch.isin(tensor1, 2,
           assume_unique=True, invert=True)
# tensor([True, True, False, True])

torch.isin(elements=tensor2, test_elements=tensor1)
torch.isin(tensor2, 2)
torch.isin(2, test_elements=tensor1)
torch.isin(2, test_elements=tensor2)
# tensor(True)

torch.isin(elements=tensor2, test_elements=tensor1, 
           assume_unique=True, invert=True)
torch.isin(tensor2, 2,
           assume_unique=True, invert=True)
torch.isin(2, test_elements=tensor1,
           assume_unique=True, invert=True)
torch.isin(2, test_elements=tensor2,
           assume_unique=True, invert=True)
# tensor(False)

tensor1 = torch.tensor([[[0., 1., 2.], [3., 4., 5.]],
                        [[6., 7., 8.], [9., 10., 11.]]])
tensor2 = torch.tensor([[3., 5.],
                        [7., 11.]])
torch.isin(elements=tensor1, test_elements=tensor2)
# tensor([[[False, False, False],
#          [True, False, True]],
#         [[False, True, False],
#          [False, False, True]]])

torch.isin(elements=tensor1, test_elements=tensor2, 
           assume_unique=True, invert=True)
# tensor([[[True, True, True],
#          [False, True, False]],
#         [[True, False, True],
#          [True, True, False]]])

torch.isin(elements=tensor2, test_elements=tensor1)
# tensor([[True, True],
#         [True, True]])

torch.isin(elements=tensor2, test_elements=tensor1, 
           assume_unique=True, invert=True)
# tensor([[False, False],
#         [False, False]])

torch.isin(tensor1, 3.)
# tensor([[[False, False, False],
#          [True, False, False]],
#         [[False, False, False],
#          [False, False, False]]])

torch.isin(tensor1, 3.,
           assume_unique=True, invert=True)
# tensor([[[True, True, True],
#          [False, True, True]],
#         [[True, True, True],
#          [True, True, True]]])

torch.isin(tensor2, 3.)
# tensor([[True, False],
#         [False, False]])

torch.isin(tensor2, 3.,
           assume_unique=True, invert=True)
# tensor([[False, True],
#         [True, True]])

torch.isin(3., test_elements=tensor1)
torch.isin(3., test_elements=tensor2)
# tensor(True)

torch.isin(3., test_elements=tensor1,
           assume_unique=True, invert=True)
torch.isin(3., test_elements=tensor2,
           assume_unique=True, invert=True)
# tensor(False)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)