MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / boolean_mask

Function boolean_mask

tensorflow/python/ops/ragged/ragged_array_ops.py:42–202  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

40
41@tf_export('ragged.boolean_mask')
42def 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)

Callers

nothing calls this directly

Calls 15

is_raggedMethod · 0.80
reduce_sumMethod · 0.80
row_lengthsMethod · 0.80
from_row_splitsMethod · 0.80
reshapeMethod · 0.80
from_row_lengthsMethod · 0.80
rangeFunction · 0.70
minFunction · 0.50
name_scopeMethod · 0.45
from_tensorMethod · 0.45
control_dependenciesMethod · 0.45

Tested by

no test coverage detected