tf.boolean_mask
Stay organized with collections
Save and categorize content based on your preferences.
Apply boolean mask to tensor.
tf.boolean_mask(
tensor, mask, axis=None, name='boolean_mask'
)
Numpy equivalent is tensor[mask]
.
In general, 0 < dim(mask) = K <= dim(tensor)
, and mask
's shape must match
the first K dimensions of tensor
's shape. We then have:
boolean_mask(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd]
where (i1,...,iK)
is the ith True
entry of mask
(row-major order).
The axis
could be used with mask
to indicate the axis to mask from.
In that case, axis + dim(mask) <= dim(tensor)
and mask
's shape must match
the first axis + dim(mask)
dimensions of tensor
's shape.
See also: tf.ragged.boolean_mask
, which can be applied to both dense and
ragged tensors, and can be used if you need to preserve the masked dimensions
of tensor
(rather than flattening them, as tf.boolean_mask
does).
Examples:
tensor = [0, 1, 2, 3] # 1-D example
mask = np.array([True, False, True, False])
tf.boolean_mask(tensor, mask)
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 2], dtype=int32)>
tensor = [[1, 2], [3, 4], [5, 6]] # 2-D example
mask = np.array([True, False, True])
tf.boolean_mask(tensor, mask)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
[5, 6]], dtype=int32)>
Args |
tensor
|
N-D Tensor.
|
mask
|
K-D boolean Tensor, K <= N and K must be known statically.
|
axis
|
A 0-D int Tensor representing the axis in tensor to mask from. By
default, axis is 0 which will mask from the first dimension. Otherwise K +
axis <= N.
|
name
|
A name for this operation (optional).
|
Returns |
(N-K+1)-dimensional tensor populated by entries in tensor corresponding
to True values in mask .
|
Raises |
ValueError
|
If shapes do not conform.
|
Examples:
# 2-D example
tensor = [[1, 2], [3, 4], [5, 6]]
mask = np.array([True, False, True])
boolean_mask(tensor, mask) # [[1, 2], [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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.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/array_ops.py#L1505-L1562) |\n\nApply boolean mask to tensor. \n\n tf.boolean_mask(\n tensor, mask, axis=None, name='boolean_mask'\n )\n\nNumpy equivalent is `tensor[mask]`.\n\nIn general, `0 \u003c dim(mask) = K \u003c= dim(tensor)`, and `mask`'s shape must match\nthe first K dimensions of `tensor`'s shape. We then have:\n`boolean_mask(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd]`\nwhere `(i1,...,iK)` is the ith `True` entry of `mask` (row-major order).\nThe `axis` could be used with `mask` to indicate the axis to mask from.\nIn that case, `axis + dim(mask) \u003c= dim(tensor)` and `mask`'s shape must match\nthe first `axis + dim(mask)` dimensions of `tensor`'s shape.\n\nSee also: [`tf.ragged.boolean_mask`](../tf/ragged/boolean_mask), which can be applied to both dense and\nragged tensors, and can be used if you need to preserve the masked dimensions\nof `tensor` (rather than flattening them, as [`tf.boolean_mask`](../tf/boolean_mask) does).\n\n#### Examples:\n\n tensor = [0, 1, 2, 3] # 1-D example\n mask = np.array([True, False, True, False])\n tf.boolean_mask(tensor, mask)\n \u003ctf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 2], dtype=int32)\u003e\n\n tensor = [[1, 2], [3, 4], [5, 6]] # 2-D example\n mask = np.array([True, False, True])\n tf.boolean_mask(tensor, mask)\n \u003ctf.Tensor: shape=(2, 2), dtype=int32, numpy=\n array([[1, 2],\n [5, 6]], dtype=int32)\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `tensor` | N-D Tensor. |\n| `mask` | K-D boolean Tensor, K \\\u003c= N and K must be known statically. |\n| `axis` | A 0-D int Tensor representing the axis in `tensor` to mask from. By default, axis is 0 which will mask from the first dimension. Otherwise K + axis \\\u003c= N. |\n| `name` | A name for this operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| (N-K+1)-dimensional tensor populated by entries in `tensor` corresponding to `True` values in `mask`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|---------------------------|\n| `ValueError` | If shapes do not conform. |\n\n\u003cbr /\u003e\n\n#### Examples:\n\n # 2-D example\n tensor = [[1, 2], [3, 4], [5, 6]]\n mask = np.array([True, False, True])\n boolean_mask(tensor, mask) # [[1, 2], [5, 6]]"]]