Attributeerror: module ‘tensorflow’ has no attribute ‘count_nonzero’

In this TensorFlow tutorial, I will show how to solve the error Attributeerror: module ‘tensorflow’ has no attribute ‘count_nonzero’.

How did I get this error when I upgraded my tensorflow version from 1.x to 2.x? This error began to appear suddenly. After that, I found the solution to this error.

So, in this tutorial, I will share the solution to that error, explaining how to access the function count_nonzero() in tensorflow to solve that error.

Attributeerror: module ‘tensorflow’ has no attribute ‘count_nonzero’

The error Attributeerror: module ‘tensorflow’ has no attribute ‘count_nonzero’ is indicating that you are trying to use the ‘count_nonzero’ function from the TensorFlow framework, and in the current tensorflow environment, it doesn’t recognize this attribute.

Here is a simple example to show you how this error may occur.

import tensorflow as tf

new_tens = tf.constant([16,0,24,78,0], dtype = tf.int32)

print("Input: ",new_tens)
new_output = tf.count_nonzero(new_tens)

print("Number of non-zero elements: ",new_output)
Attributeerror module 'tensorflow' has no attribute 'count_nonzero'

In the above code, first import the TensorFlow library as tf and create a tensor using the tf.constant() function. Within this function, assign the integer values as an argument.

After that, we used the tf.count_nonzero() function, and within this function, we assigned the input tensor as an argument. This function will work in the TensorFlow 1.x version.

But you get the error because you are using TensorFlow version 2.x, and your code is compatible with TensorFlow 1.x.

Before resolving this error, I want to explain the count_nonzero() function.

  • The multidimensional tensor’s nonzero element count is calculated using this function. This function allows us to determine the number of elements in the multidimensional tensors that are not zero.
  • The function’s return value is the number of nonzero values in the given tensors along the specified axis. If not, the array’s nonzero value count is returned.
READ:  Python Tkinter drag and drop

First, ensure you have installed the latest version of Tensorflow to fix the above error.

Then, in the new version of the Tensorflow, the non_zero() function can be accessed in the tf.math submodule.

The syntax is given below, which you can follow.


Where parameters are:

  • input: This parameter reduces the tensor. It should be of the bool, string, or numeric type.
  • axis: By default, it takes no value—the reduced dimensions. If None (the default), all dimensions are reduced. range [-rank(input), rank(input)] is required.
  • keepdims: It will check the condition. If it is true, then it will reduce dimensions with length 1.
  • dtype: This parameter specifies the data type of the input tensor.
  • name: It specifies the name of the operation.

Now you know about the count_nonzero() function and why the error occurs, so to fix the error, look at the below part of how the count_nonzero() function is accessed.

import tensorflow as tf

# Creation of input tensor
new_tens = tf.constant([16,0,24,78,0], dtype = tf.int32)

print("Input: ",new_tens)
new_output = tf.math.count_nonzero(new_tens)
# Display the Content
print("Number of non-zero elements: ",new_output)
Solution to Attributeerror module 'tensorflow' has no attribute 'count_nonzero'

As you can see, the error is gone because the correct way to access the count_nonzero() function in tensorflow is tf.math.count_nonzero().

This is how to fix the error Attributeerror: module ‘tensorflow’ has no attribute ‘count_nonzero’.

Always remember that you should refer to the official documentation of TensorFlow for the version you are using to ensure that you are accessing and using the function correctly.


In this tutorial, You learned how to solve the error Attributeerror: module ‘tensorflow’ has no attribute ‘count_nonzero’.

READ:  How to View Uploaded Files in Django

You learned that the count_nonzero() function has moved to submodule tf.math in TensorFlow, so you have access to the function from the tf.math submodule and solved the error problem.

You may like to read: