PyTorch torch.masked_select function
PyTorch torch reference manual
torch.masked_select is a function in PyTorch used to select elements based on a Boolean mask.
Function definition
torch.masked_select(input, mask)
Usage examples
Example
import
torch
x
=
torch.
tensor
(
[
1
,
2
,
3
,
4
,
5
]
)
mask
=
torch.
tensor
(
[
True
,
False
,
True
,
False
,
True
]
)
Select elements based on the mask
result
=
torch.
masked_select
(
x
,
mask
)
(
result
)
The output is:
tensor([1, 3, 5])