tf.ragged.boolean_mask
Stay organized with collections
Save and categorize content based on your preferences.
Applies a boolean mask to data
without flattening the mask dimensions.
tf.ragged.boolean_mask(
data, mask, name=None
)
Used in the notebooks
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 the i
th True
entry of mask[a1...aA]
.
Note that output
preserves the mask dimensions a1...aA
; this differs
from tf.boolean_mask
, which flattens those dimensions.
Args |
data
|
A potentially ragged tensor.
|
mask
|
A potentially ragged boolean tensor. mask 's shape must be a prefix
of data 's shape. rank(mask) must be known statically.
|
name
|
A name prefix for the returned tensor (optional).
|
Returns |
A potentially ragged tensor that is formed by retaining the elements in
data where the corresponding value in mask is True .
rank(output) = rank(data) .
output.ragged_rank = max(data.ragged_rank, rank(mask) - 1) .
|
Raises |
ValueError
|
if rank(mask) is not known statically; or if mask.shape is
not a prefix of data.shape .
|
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]]
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.ragged.boolean_mask\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/ragged/ragged_array_ops.py#L49-L211) |\n\nApplies a boolean mask to `data` without flattening the mask dimensions.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.ragged.boolean_mask`](https://www.tensorflow.org/api_docs/python/tf/ragged/boolean_mask)\n\n\u003cbr /\u003e\n\n tf.ragged.boolean_mask(\n data, mask, name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|----------------------------------------------------------------------------------|\n| - [Subword tokenizers](https://www.tensorflow.org/text/guide/subwords_tokenizer) |\n\nReturns a potentially ragged tensor that is formed by retaining the elements\nin `data` where the corresponding value in `mask` is `True`.\n\n- `output[a1...aA, i, b1...bB] = data[a1...aA, j, b1...bB]`\n\n Where `j` is the `i`th `True` entry of `mask[a1...aA]`.\n\nNote that `output` preserves the mask dimensions `a1...aA`; this differs\nfrom [`tf.boolean_mask`](../../tf/boolean_mask), which flattens those dimensions.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|--------------------------------------------------------------------------------------------------------------------------------|\n| `data` | A potentially ragged tensor. |\n| `mask` | A potentially ragged boolean tensor. `mask`'s shape must be a prefix of `data`'s shape. `rank(mask)` must be known statically. |\n| `name` | A name prefix for the returned tensor (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A potentially ragged tensor that is formed by retaining the elements in `data` where the corresponding value in `mask` is `True`. \u003cbr /\u003e - `rank(output) = rank(data)`. - `output.ragged_rank = max(data.ragged_rank, rank(mask) - 1)`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|----------------------------------------------------------------------------------------------|\n| `ValueError` | if `rank(mask)` is not known statically; or if `mask.shape` is not a prefix of `data.shape`. |\n\n\u003cbr /\u003e\n\n#### Examples:\n\n # Aliases for True & False so data and mask line up.\n T, F = (True, False)\n\n tf.ragged.boolean_mask( # Mask a 2D Tensor.\n data=[[1, 2, 3], [4, 5, 6], [7, 8, 9]],\n mask=[[T, F, T], [F, F, F], [T, F, F]]).to_list()\n [[1, 3], [], [7]]\n\n tf.ragged.boolean_mask( # Mask a 2D RaggedTensor.\n tf.ragged.constant([[1, 2, 3], [4], [5, 6]]),\n tf.ragged.constant([[F, F, T], [F], [T, T]])).to_list()\n [[3], [], [5, 6]]\n\n tf.ragged.boolean_mask( # Mask rows of a 2D RaggedTensor.\n tf.ragged.constant([[1, 2, 3], [4], [5, 6]]),\n tf.ragged.constant([True, False, True])).to_list()\n [[1, 2, 3], [5, 6]]"]]