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 1.
If axis has no entries, all dimensions are reduced, and a
tensor with a single element is returned.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.compat.v1.count_nonzero\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/math_ops.py#L2269-L2344) |\n\nComputes number of nonzero elements across dimensions of a tensor. (deprecated arguments) (deprecated arguments)\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.math.count_nonzero`](https://www.tensorflow.org/api_docs/python/tf/compat/v1/count_nonzero)\n\n\u003cbr /\u003e\n\n tf.compat.v1.count_nonzero(\n input_tensor=None,\n axis=None,\n keepdims=None,\n dtype=../../../tf/dtypes#int64,\n name=None,\n reduction_indices=None,\n keep_dims=None,\n input=None\n )\n\n| **Deprecated:** SOME ARGUMENTS ARE DEPRECATED: `(keep_dims)`. They will be removed in a future version. Instructions for updating: keep_dims is deprecated, use keepdims instead\n| **Deprecated:** SOME ARGUMENTS ARE DEPRECATED: `(reduction_indices)`. They will be removed in a future version. Instructions for updating: reduction_indices is deprecated, use axis instead\n\nReduces `input_tensor` along the dimensions given in `axis`.\nUnless `keepdims` is true, the rank of the tensor is reduced by 1 for each\nentry in `axis`. If `keepdims` is true, the reduced dimensions\nare retained with length 1.\n\nIf `axis` has no entries, all dimensions are reduced, and a\ntensor with a single element is returned.\n| **Note:** Floating point comparison to zero is done by exact floating point equality check. Small values are **not** rounded to zero for purposes of the nonzero check.\n\n#### For example:\n\n x = tf.constant([[0, 1, 0], [1, 1, 0]])\n tf.math.count_nonzero(x) # 3\n tf.math.count_nonzero(x, 0) # [1, 2, 0]\n tf.math.count_nonzero(x, 1) # [1, 2]\n tf.math.count_nonzero(x, 1, keepdims=True) # [[1], [2]]\n tf.math.count_nonzero(x, [0, 1]) # 3\n\n| **Note:** Strings are compared against zero-length empty string `\"\"`. Any string with a size greater than zero is already considered as nonzero.\n\n#### For example:\n\n x = tf.constant([\"\", \"a\", \" \", \"b\", \"\"])\n tf.math.count_nonzero(x) # 3, with \"a\", \" \", and \"b\" as nonzero strings.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------|\n| `input_tensor` | The tensor to reduce. Should be of numeric type, `bool`, or `string`. |\n| `axis` | The dimensions to reduce. If `None` (the default), reduces all dimensions. Must be in the range `[-rank(input_tensor), rank(input_tensor))`. |\n| `keepdims` | If true, retains reduced dimensions with length 1. |\n| `dtype` | The output dtype; defaults to [`tf.int64`](../../../tf#int64). |\n| `name` | A name for the operation (optional). |\n| `reduction_indices` | The old (deprecated) name for axis. |\n| `keep_dims` | Deprecated alias for `keepdims`. |\n| `input` | Overrides input_tensor. For compatibility. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| The reduced tensor (number of nonzero values). ||\n\n\u003cbr /\u003e"]]