Computes number of nonzero elements across dimensions of a tensor. Reduces `input` 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 1. I
(
input, # pylint: disable=redefined-builtin
axis=None,
keepdims=None,
dtype=dtypes.int64,
name=None)
| 1683 | |
| 1684 | @tf_export("math.count_nonzero", v1=[]) |
| 1685 | def count_nonzero_v2( |
| 1686 | input, # pylint: disable=redefined-builtin |
| 1687 | axis=None, |
| 1688 | keepdims=None, |
| 1689 | dtype=dtypes.int64, |
| 1690 | name=None): |
| 1691 | """Computes number of nonzero elements across dimensions of a tensor. |
| 1692 | |
| 1693 | Reduces `input` along the dimensions given in `axis`. |
| 1694 | Unless `keepdims` is true, the rank of the tensor is reduced by 1 for each |
| 1695 | entry in `axis`. If `keepdims` is true, the reduced dimensions |
| 1696 | are retained with length 1. |
| 1697 | |
| 1698 | If `axis` has no entries, all dimensions are reduced, and a |
| 1699 | tensor with a single element is returned. |
| 1700 | |
| 1701 | **NOTE** Floating point comparison to zero is done by exact floating point |
| 1702 | equality check. Small values are **not** rounded to zero for purposes of |
| 1703 | the nonzero check. |
| 1704 | |
| 1705 | For example: |
| 1706 | |
| 1707 | ```python |
| 1708 | x = tf.constant([[0, 1, 0], [1, 1, 0]]) |
| 1709 | tf.math.count_nonzero(x) # 3 |
| 1710 | tf.math.count_nonzero(x, 0) # [1, 2, 0] |
| 1711 | tf.math.count_nonzero(x, 1) # [1, 2] |
| 1712 | tf.math.count_nonzero(x, 1, keepdims=True) # [[1], [2]] |
| 1713 | tf.math.count_nonzero(x, [0, 1]) # 3 |
| 1714 | ``` |
| 1715 | |
| 1716 | **NOTE** Strings are compared against zero-length empty string `""`. Any |
| 1717 | string with a size greater than zero is already considered as nonzero. |
| 1718 | |
| 1719 | For example: |
| 1720 | ```python |
| 1721 | x = tf.constant(["", "a", " ", "b", ""]) |
| 1722 | tf.math.count_nonzero(x) # 3, with "a", " ", and "b" as nonzero strings. |
| 1723 | ``` |
| 1724 | |
| 1725 | Args: |
| 1726 | input: The tensor to reduce. Should be of numeric type, `bool`, or `string`. |
| 1727 | axis: The dimensions to reduce. If `None` (the default), reduces all |
| 1728 | dimensions. Must be in the range `[-rank(input), rank(input))`. |
| 1729 | keepdims: If true, retains reduced dimensions with length 1. |
| 1730 | dtype: The output dtype; defaults to `tf.int64`. |
| 1731 | name: A name for the operation (optional). |
| 1732 | |
| 1733 | Returns: |
| 1734 | The reduced tensor (number of nonzero values). |
| 1735 | """ |
| 1736 | if keepdims is None: |
| 1737 | keepdims = False |
| 1738 | with ops.name_scope(name, "count_nonzero", [input]): |
| 1739 | input = ops.convert_to_tensor(input, name="input") |
| 1740 | # A scalar of 'zero' is enough as `not_equal` will broadcast. |
| 1741 | zero = array_ops.zeros([], dtype=input.dtype) |
| 1742 | return cast( |
no test coverage detected