Applies a boolean mask to `data` without flattening the mask dimensions. Returns a potentially ragged tensor that is formed by retaining the elements in `data` where the corresponding value in `mask` is `True`. * `output[a1...aA, i, b1...bB] = data[a1...aA, j, b1...bB]` Where `j` is th
(data, mask, name=None)
| 40 | |
| 41 | @tf_export('ragged.boolean_mask') |
| 42 | def boolean_mask(data, mask, name=None): |
| 43 | """Applies a boolean mask to `data` without flattening the mask dimensions. |
| 44 | |
| 45 | Returns a potentially ragged tensor that is formed by retaining the elements |
| 46 | in `data` where the corresponding value in `mask` is `True`. |
| 47 | |
| 48 | * `output[a1...aA, i, b1...bB] = data[a1...aA, j, b1...bB]` |
| 49 | |
| 50 | Where `j` is the `i`th `True` entry of `mask[a1...aA]`. |
| 51 | |
| 52 | Note that `output` preserves the mask dimensions `a1...aA`; this differs |
| 53 | from `tf.boolean_mask`, which flattens those dimensions. |
| 54 | |
| 55 | Args: |
| 56 | data: A potentially ragged tensor. |
| 57 | mask: A potentially ragged boolean tensor. `mask`'s shape must be a prefix |
| 58 | of `data`'s shape. `rank(mask)` must be known statically. |
| 59 | name: A name prefix for the returned tensor (optional). |
| 60 | |
| 61 | Returns: |
| 62 | A potentially ragged tensor that is formed by retaining the elements in |
| 63 | `data` where the corresponding value in `mask` is `True`. |
| 64 | |
| 65 | * `rank(output) = rank(data)`. |
| 66 | * `output.ragged_rank = max(data.ragged_rank, rank(mask) - 1)`. |
| 67 | |
| 68 | Raises: |
| 69 | ValueError: if `rank(mask)` is not known statically; or if `mask.shape` is |
| 70 | not a prefix of `data.shape`. |
| 71 | |
| 72 | #### Examples: |
| 73 | ```python |
| 74 | >>> # Aliases for True & False so data and mask line up. |
| 75 | >>> T, F = (True, False) |
| 76 | |
| 77 | >>> tf.ragged.boolean_mask( # Mask a 2D Tensor. |
| 78 | ... data=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], |
| 79 | ... mask=[[T, F, T], [F, F, F], [T, F, F]]).tolist() |
| 80 | [[1, 3], [], [7]] |
| 81 | |
| 82 | >>> tf.ragged.boolean_mask( # Mask a 2D RaggedTensor. |
| 83 | ... tf.ragged.constant([[1, 2, 3], [4], [5, 6]]), |
| 84 | ... tf.ragged.constant([[F, F, T], [F], [T, T]])).tolist() |
| 85 | [[3], [], [5, 6]] |
| 86 | |
| 87 | >>> tf.ragged.boolean_mask( # Mask rows of a 2D RaggedTensor. |
| 88 | ... tf.ragged.constant([[1, 2, 3], [4], [5, 6]]), |
| 89 | ... tf.ragged.constant([True, False, True])).tolist() |
| 90 | [[1, 2, 3], [5, 6]] |
| 91 | ``` |
| 92 | """ |
| 93 | with ops.name_scope(name, 'RaggedMask', [data, mask]): |
| 94 | # Convert inputs to tensors. |
| 95 | data = ragged_tensor.convert_to_tensor_or_ragged_tensor(data, name='data') |
| 96 | mask = ragged_tensor.convert_to_tensor_or_ragged_tensor( |
| 97 | mask, dtypes.bool, name='mask') |
| 98 | row_splits_dtype, (data, mask) = ragged_tensor.match_row_splits_dtypes( |
| 99 | data, mask, return_dtype=True) |
nothing calls this directly
no test coverage detected