paddle. masked_select ( x, mask, name=None ) [source]

This OP Returns a new 1-D tensor which indexes the input tensor according to the mask which is a tensor with data type of bool.

  • x (Tensor) – The input Tensor, the data type can be int32, int64, float32, float64.

  • mask (Tensor) – The Tensor containing the binary mask to index with, it’s data type is bool.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns: A 1-D Tensor which is the same data type as x.


import paddle

x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0],
                      [5.0, 6.0, 7.0, 8.0],
                      [9.0, 10.0, 11.0, 12.0]])
mask = paddle.to_tensor([[True, False, False, False],
                         [True, True, False, False],
                         [True, False, False, False]])
out = paddle.masked_select(x, mask)
#[1.0 5.0 6.0 9.0]