Computes number of nonzero elements across dimensions of a tensor. Reduces `input_tensor` along the dimensions given in `axis`. Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each entry in `axis`. If `keepdims` is true, the reduced dimensions are retained with length
(input_tensor=None,
axis=None,
keepdims=None,
dtype=dtypes.int64,
name=None,
reduction_indices=None,
keep_dims=None,
input=None)
| 1612 | @deprecation.deprecated_args( |
| 1613 | None, "reduction_indices is deprecated, use axis instead", "axis") |
| 1614 | def count_nonzero(input_tensor=None, |
| 1615 | axis=None, |
| 1616 | keepdims=None, |
| 1617 | dtype=dtypes.int64, |
| 1618 | name=None, |
| 1619 | reduction_indices=None, |
| 1620 | keep_dims=None, |
| 1621 | input=None): # pylint: disable=redefined-builtin |
| 1622 | """Computes number of nonzero elements across dimensions of a tensor. |
| 1623 | |
| 1624 | Reduces `input_tensor` along the dimensions given in `axis`. |
| 1625 | Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each |
| 1626 | entry in `axis`. If `keepdims` is true, the reduced dimensions |
| 1627 | are retained with length 1. |
| 1628 | |
| 1629 | If `axis` has no entries, all dimensions are reduced, and a |
| 1630 | tensor with a single element is returned. |
| 1631 | |
| 1632 | **NOTE** Floating point comparison to zero is done by exact floating point |
| 1633 | equality check. Small values are **not** rounded to zero for purposes of |
| 1634 | the nonzero check. |
| 1635 | |
| 1636 | For example: |
| 1637 | |
| 1638 | ```python |
| 1639 | x = tf.constant([[0, 1, 0], [1, 1, 0]]) |
| 1640 | tf.math.count_nonzero(x) # 3 |
| 1641 | tf.math.count_nonzero(x, 0) # [1, 2, 0] |
| 1642 | tf.math.count_nonzero(x, 1) # [1, 2] |
| 1643 | tf.math.count_nonzero(x, 1, keepdims=True) # [[1], [2]] |
| 1644 | tf.math.count_nonzero(x, [0, 1]) # 3 |
| 1645 | ``` |
| 1646 | |
| 1647 | **NOTE** Strings are compared against zero-length empty string `""`. Any |
| 1648 | string with a size greater than zero is already considered as nonzero. |
| 1649 | |
| 1650 | For example: |
| 1651 | ```python |
| 1652 | x = tf.constant(["", "a", " ", "b", ""]) |
| 1653 | tf.math.count_nonzero(x) # 3, with "a", " ", and "b" as nonzero strings. |
| 1654 | ``` |
| 1655 | |
| 1656 | Args: |
| 1657 | input_tensor: The tensor to reduce. Should be of numeric type, `bool`, or |
| 1658 | `string`. |
| 1659 | axis: The dimensions to reduce. If `None` (the default), reduces all |
| 1660 | dimensions. Must be in the range `[-rank(input_tensor), |
| 1661 | rank(input_tensor))`. |
| 1662 | keepdims: If true, retains reduced dimensions with length 1. |
| 1663 | dtype: The output dtype; defaults to `tf.int64`. |
| 1664 | name: A name for the operation (optional). |
| 1665 | reduction_indices: The old (deprecated) name for axis. |
| 1666 | keep_dims: Deprecated alias for `keepdims`. |
| 1667 | input: Overrides input_tensor. For compatibility. |
| 1668 | |
| 1669 | Returns: |
| 1670 | The reduced tensor (number of nonzero values). |
| 1671 | """ |
nothing calls this directly
no test coverage detected