DEV Community

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

Posted on • Updated on

isin in PyTorch

Buy Me a Coffee

*Memos:

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

*Memos:

  • isin() can be used with torch but not with a tensor.
  • The 1st argument with torch is elements(Required-Type:tensor or scalar of int or float). *You must use a scalar without elements=.
  • The 2nd argument with torch is test_elements(Required-Type:tensor or scalar of int or float). *You must use a scalar without test_elements=.
  • The 3rd argument with torch is assume_unique(Optional-Default=False-Type:bool). *If True, assumes both elements and test_elements contain unique elements, which can speed up the calculation.
  • The 4th argument with torch is invert(Optional-Default=False-Type:bool). *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)