PyTorch torch.isin function
PyTorch torch reference manual
torch.isin is a function in PyTorch used to check whether elements are present in another tensor. It returns a Boolean tensor indicating whether each element of the first tensor appears in the second tensor.
Function definition
torch.isin(elements, test_elements, assume_unique=False, invert=False)
Usage examples
Example
import
torch
Create two tensors
elements
=
torch.
tensor
(
[
1
,
2
,
3
,
4
]
)
test_elements
=
torch.
tensor
(
[
2
,
4
]
)
Check whether elements are in test_elements
result
=
torch.
isin
(
elements
,
test_elements
)
(
result
)
The output is:
tensor([False, True, False, True])