Attributeerror: Module ‘tensorflow’ has no attribute ‘logging’

In this TensorFlow tutorial, I will show how to fix the error Attributeerror: Module ‘tensorflow’ has no attribute ‘logging’.

To resolve this error, you will understand two methods; in the first method, you solve the error by downgrading your TensorFlow version, and in the second, you solve the error by using all the functionality or attributes of TensorFlow version 1 in the version 2 environment.

Let’s start,

Attributeerror: Module ‘tensorflow’ has no attribute ‘logging’

The error Attributeerror: Module ‘tensorflow’ has no attribute ‘logging’, which means the attribute ‘logging’ doesn’t exist in the TensorFlow module.

The logging() attribute sets the logging level to debug, info, or error. Let’s see which code can cause this error; run the code below.

import tensorflow as tf

print(tf.logging)
Attributeerror Module 'tensorflow' has no attribute 'logging'

When you try to access the attribute ‘logging’ from the tensorflow, it shows the error you can see in the above output.

The reason behind this error is the version of TensorFlow, which means your code is compatible with TensorFlow version 1.x, and you may have updated the TensorFlow to version 2.x, so you can’t access the attribute ‘logging’ in TensorFlow 2.

Because in TensorFlow 2.x, it has been removed, so Tensorflow removed the lesser-used functions from TensorFlow version 2.x and kept only the often-used functions or attributes.

The solution to this error is either downgrade the Tensorflow version to 1 or use the tf.compat.v1 module allows you to access all the attributes and functions of TensorFlow version 1 into the environment of TensorFlow version 2.

READ:  Scikit learn Genetic algorithm

So first, downgrade the Tensorflow current version to 1.x something using the command below. Remember to downgrade to the version you used before getting this error.

pip install tensorflow==1.14 // use it in terminal

!pip install tensorflow==1.14 // use it in Notebook environment

After downgrading, when you try to access the attribute ‘logging’ directly from TensorFlow, you can access it without getting any error.

The next solution is to use the compatibility mode using the tf.compat.v1, TensorFlow version 2 has this module, which allows us to access and use the functions of the older version that don’t exist in the latest version.

To access the attribute logging from the tf. compat.v1 run the code below.

import tensorflow as tf

print(tf.compat.v1.logging)
Solution to Attributeerror Module 'tensorflow' has no attribute 'logging'

Look in the output; it doesn’t show any error because you are accessing the attribute ‘logging’ from the ‘tf. compact.v1’.

This is how to resolve the error Attributeerror: Module ‘tensorflow’ has no attribute ‘logging’ in TensorFlow.

Conclusion

You learned how to resolve the error Attributeerror: Module ‘tensorflow’ has no attribute ‘logging’.

You learned two ways to solve the error, using the tf.compat.v1 module to access the logging() attribute in the current environment of Tensorflow and downgrade the tensorflow version to 1.

You may like to read: