Use tf.math.count_nonzero(x) instead of tf.count_nonzero(x). TensorFlow documents the operation in the tf.math namespace; tf.compat.v1.count_nonzero is available when you need the TensorFlow 1.x compatibility API.
Replace the missing attribute
Update the call in your code:
import tensorflow as tf
count = tf.math.count_nonzero(x)
tf.math.count_nonzero counts nonzero elements in a tensor. For new code and modernized TensorFlow code, use this documented namespace rather than a top-level tf.count_nonzero reference.
If you are retaining TensorFlow 1.x-style code and specifically need the compatibility API, use:
count = tf.compat.v1.count_nonzero(x)
The compatibility API reference lists axis and keepdims as the current argument names. Its older names, reduction_indices and keep_dims, are deprecated.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →#1 Best Overall
Check the result you expect
The function reduces the tensor along the selected dimensions. With axis=None, it counts nonzero elements across all dimensions. If you set an axis, only the selected dimension or dimensions are reduced.
For example, this counts all nonzero values in a tensor:
Rank #2
- Use scikit-learn to track an example ML project end to end
- Explore several models, including support vector machines, decision trees, random forests, and ensemble methods
- Exploit unsupervised learning techniques such as dimensionality reduction, clustering, and anomaly detection
- Dive into neural net architectures, including convolutional nets, recurrent nets, generative adversarial networks, autoencoders, diffusion models, and transformers
- Use TensorFlow and Keras to build and train neural nets for computer vision, natural language processing, generative models, and deep reinforcement learning
count = tf.math.count_nonzero(x, axis=None)
The output dtype defaults to tf.int64. Floating-point values are compared exactly with zero, so a small floating-point value that is not exactly zero is counted. For string tensors, the empty string is treated as zero; nonempty strings are counted.
If the error persists, inspect the active Python environment
The error message alone does not establish which TensorFlow version your script loaded or where that module came from. Run these checks in the same terminal, notebook kernel, or virtual environment as the failing code:
Recommended Free Tools
Rank #3
import tensorflow as tf
print(tf.__version__)
print(tf.__file__)
print(tf.math.count_nonzero)
- The version: Confirm the version belongs to the environment that actually runs the script.
- The module path: Check whether
tf.__file__points to the expected installed TensorFlow package. If it points into your project, a local file or folder namedtensorflowmay be taking precedence during import. - Other missing attributes: If several unrelated TensorFlow attributes are absent, investigate the interpreter, import path, and package installation rather than changing each application call separately.
Historical reports of missing public TensorFlow attributes describe particular version or installation contexts; they do not identify the cause of this specific count_nonzero error. Diagnose the environment that reproduces it.
For projects migrating from TensorFlow 1.x
Changing this one call may not be enough for an older project. TensorFlow’s migration guide describes tf_upgrade_v2 for rewriting TensorFlow 1.x API symbols and recommends making dependencies compatible with TensorFlow 2.x. Review the converted code and its dependencies against the TensorFlow version installed in your environment.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




