*Memos:
- My post explains isreal(), isnan() and isfinite().
- My post explains isinf(), isposinf() and isneginf().
- My post explains is_floating_point(), is_complex() and is_nonzero().
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
iselements
(Required-Type:tensor
orscalar
ofint
orfloat
). *You must use a scalar withoutelements=
. - The 2nd argument with
torch
istest_elements
(Required-Type:tensor
orscalar
ofint
orfloat
). *You must use a scalar withouttest_elements=
. - The 3rd argument with
torch
isassume_unique
(Optional-Default=False
-Type:bool
). *IfTrue
, assumes bothelements
andtest_elements
contain unique elements, which can speed up the calculation. - The 4th argument with
torch
isinvert
(Optional-Default=False
-Type:bool
). *IfTrue
, inverts the boolean return tensor, resulting inTrue
values for elements not intest_elements
. - The combination of a scalar(
elements
) and ascalar
(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)
Top comments (0)