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, axis=None, name="boolean_mask")
| 1510 | @tf_export("boolean_mask", v1=[]) |
| 1511 | @dispatch.add_dispatch_support |
| 1512 | def boolean_mask_v2(tensor, mask, axis=None, name="boolean_mask"): |
| 1513 | """Apply boolean mask to tensor. |
| 1514 | |
| 1515 | Numpy equivalent is `tensor[mask]`. |
| 1516 | |
| 1517 | ```python |
| 1518 | # 1-D example |
| 1519 | tensor = [0, 1, 2, 3] |
| 1520 | mask = np.array([True, False, True, False]) |
| 1521 | boolean_mask(tensor, mask) # [0, 2] |
| 1522 | ``` |
| 1523 | |
| 1524 | In general, `0 < dim(mask) = K <= dim(tensor)`, and `mask`'s shape must match |
| 1525 | the first K dimensions of `tensor`'s shape. We then have: |
| 1526 | `boolean_mask(tensor, mask)[i, j1,...,jd] = tensor[i1,...,iK,j1,...,jd]` |
| 1527 | where `(i1,...,iK)` is the ith `True` entry of `mask` (row-major order). |
| 1528 | The `axis` could be used with `mask` to indicate the axis to mask from. |
| 1529 | In that case, `axis + dim(mask) <= dim(tensor)` and `mask`'s shape must match |
| 1530 | the first `axis + dim(mask)` dimensions of `tensor`'s shape. |
| 1531 | |
| 1532 | See also: `tf.ragged.boolean_mask`, which can be applied to both dense and |
| 1533 | ragged tensors, and can be used if you need to preserve the masked dimensions |
| 1534 | of `tensor` (rather than flattening them, as `tf.boolean_mask` does). |
| 1535 | |
| 1536 | Args: |
| 1537 | tensor: N-D tensor. |
| 1538 | mask: K-D boolean tensor, K <= N and K must be known statically. |
| 1539 | axis: A 0-D int Tensor representing the axis in `tensor` to mask from. By |
| 1540 | default, axis is 0 which will mask from the first dimension. Otherwise K + |
| 1541 | axis <= N. |
| 1542 | name: A name for this operation (optional). |
| 1543 | |
| 1544 | Returns: |
| 1545 | (N-K+1)-dimensional tensor populated by entries in `tensor` corresponding |
| 1546 | to `True` values in `mask`. |
| 1547 | |
| 1548 | Raises: |
| 1549 | ValueError: If shapes do not conform. |
| 1550 | |
| 1551 | Examples: |
| 1552 | |
| 1553 | ```python |
| 1554 | # 2-D example |
| 1555 | tensor = [[1, 2], [3, 4], [5, 6]] |
| 1556 | mask = np.array([True, False, True]) |
| 1557 | boolean_mask(tensor, mask) # [[1, 2], [5, 6]] |
| 1558 | ``` |
| 1559 | """ |
| 1560 | return boolean_mask(tensor, mask, name, axis) |
| 1561 | |
| 1562 | |
| 1563 | @tf_export("sparse.mask", v1=["sparse.mask", "sparse_mask"]) |
nothing calls this directly
no test coverage detected