<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/ops/numpy.py#L2263-L2293) |

Counts the number of non-zero values in `x` along the given `axis`.

#### View aliases


**Main aliases**

[`tf.keras.ops.numpy.count_nonzero`](https://www.tensorflow.org/api_docs/python/tf/keras/ops/count_nonzero)

<br />

    tf.keras.ops.count_nonzero(
        x, axis=None
    )

If no axis is specified then all non-zeros in the tensor are counted.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x` | Input tensor. |
| `axis` | Axis or tuple of axes along which to count the number of non-zeros. Defaults to `None`. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| int or tensor of ints. ||

<br />

#### Examples:

    x = keras.ops.array([[0, 1, 7, 0], [3, 0, 2, 19]])
    keras.ops.count_nonzero(x)
    5
    keras.ops.count_nonzero(x, axis=0)
    array([1, 1, 2, 1], dtype=int64)
    keras.ops.count_nonzero(x, axis=1)
    array([2, 3], dtype=int64)