tf.ragged.boolean_mask

View source on GitHub

Applies a boolean mask to data without flattening the mask dimensions.

tf.ragged.boolean_mask(
    data, mask, name=None
)

Returns a potentially ragged tensor that is formed by retaining the elements in data where the corresponding value in mask is True.

Note that output preserves the mask dimensions a1...aA; this differs from tf.boolean_mask, which flattens those dimensions.

Args:

Returns:

A potentially ragged tensor that is formed by retaining the elements in data where the corresponding value in mask is True.

Raises:

Examples:

>>> # Aliases for True & False so data and mask line up.
>>> T, F = (True, False)
>>> tf.ragged.boolean_mask(  # Mask a 2D Tensor.
...     data=[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
...     mask=[[T, F, T], [F, F, F], [T, F, F]]).to_list()
[[1, 3], [], [7]]
>>> tf.ragged.boolean_mask(  # Mask a 2D RaggedTensor.
...     tf.ragged.constant([[1, 2, 3], [4], [5, 6]]),
...     tf.ragged.constant([[F, F, T], [F], [T, T]])).to_list()
[[3], [], [5, 6]]
>>> tf.ragged.boolean_mask(  # Mask rows of a 2D RaggedTensor.
...     tf.ragged.constant([[1, 2, 3], [4], [5, 6]]),
...     tf.ragged.constant([True, False, True])).to_list()
[[1, 2, 3], [5, 6]]