Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.masked_select function

PyTorch torch.masked_select function


PyTorch torch reference manual 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

)

print

(

result

)

The output is:

tensor([1, 3, 5])


PyTorch torch reference manual PyTorch torch reference manual