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

Function boolean_mask

tensorflow/python/ops/array_ops.py:1424–1507  ·  view source on GitHub ↗

Apply boolean mask to tensor. Numpy equivalent is `tensor[mask]`. ```python # 1-D example tensor = [0, 1, 2, 3] mask = np.array([True, False, True, False]) boolean_mask(tensor, mask) # [0, 2] ``` In general, `0 < dim(mask) = K <= dim(tensor)`, and `mask`'s shape must match the

(tensor, mask, name="boolean_mask", axis=None)

Source from the content-addressed store, hash-verified

1422
1423@tf_export(v1=["boolean_mask"])
1424def boolean_mask(tensor, mask, name="boolean_mask", axis=None):
1425 """Apply boolean mask to tensor.
1426
1427 Numpy equivalent is `tensor[mask]`.
1428
1429 ```python
1430 # 1-D example
1431 tensor = [0, 1, 2, 3]
1432 mask = np.array([True, False, True, False])
1433 boolean_mask(tensor, mask) # [0, 2]
1434 ```
1435
1436 In general, `0 < dim(mask) = K <= dim(tensor)`, and `mask`&#x27;s shape must match
1437 the first K dimensions of `tensor`&#x27;s shape. We then have:
1438 `boolean_mask(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd]`
1439 where `(i1,...,iK)` is the ith `True` entry of `mask` (row-major order).
1440 The `axis` could be used with `mask` to indicate the axis to mask from.
1441 In that case, `axis + dim(mask) <= dim(tensor)` and `mask`&#x27;s shape must match
1442 the first `axis + dim(mask)` dimensions of `tensor`&#x27;s shape.
1443
1444 See also: `tf.ragged.boolean_mask`, which can be applied to both dense and
1445 ragged tensors, and can be used if you need to preserve the masked dimensions
1446 of `tensor` (rather than flattening them, as `tf.boolean_mask` does).
1447
1448 Args:
1449 tensor: N-D tensor.
1450 mask: K-D boolean tensor, K <= N and K must be known statically.
1451 name: A name for this operation (optional).
1452 axis: A 0-D int Tensor representing the axis in `tensor` to mask from. By
1453 default, axis is 0 which will mask from the first dimension. Otherwise K +
1454 axis <= N.
1455
1456 Returns:
1457 (N-K+1)-dimensional tensor populated by entries in `tensor` corresponding
1458 to `True` values in `mask`.
1459
1460 Raises:
1461 ValueError: If shapes do not conform.
1462
1463 Examples:
1464
1465 ```python
1466 # 2-D example
1467 tensor = [[1, 2], [3, 4], [5, 6]]
1468 mask = np.array([True, False, True])
1469 boolean_mask(tensor, mask) # [[1, 2], [5, 6]]
1470 ```
1471 """
1472
1473 def _apply_mask_1d(reshaped_tensor, mask, axis=None):
1474 """Mask tensor along dimension 0 with a 1-D mask."""
1475 indices = squeeze(where(mask), axis=[1])
1476 return gather(reshaped_tensor, indices, axis=axis)
1477
1478 with ops.name_scope(name, values=[tensor, mask]):
1479 tensor = ops.convert_to_tensor(tensor, name="tensor")
1480 mask = ops.convert_to_tensor(mask, name="mask")
1481

Callers 3

_slice_helperFunction · 0.70
boolean_mask_v2Function · 0.70
repeat_with_axisFunction · 0.70

Calls 10

_apply_mask_1dFunction · 0.85
shapeFunction · 0.70
reshapeFunction · 0.70
concatFunction · 0.70
name_scopeMethod · 0.45
get_shapeMethod · 0.45
num_elementsMethod · 0.45
set_shapeMethod · 0.45
concatenateMethod · 0.45

Tested by

no test coverage detected