Module ‘TensorFlow’ has no attribute ‘get_default_graph’

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

As TensorFlow’s latest version arrived, I updated to that version; when I ran my code, an error appeared in my command prompt.

To resolve the error, I found two solutions: using the compatibility mode of TensorFlow version 1 and downgrading the TensorFlow version.

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

The error Attributeerror: Module ‘tensorflow’ has no attribute ‘get_default_graph’ is indicating you are trying to access the attribute ‘get_default_graph’ from the TensorFlow directly, but this attribute doesn’t exist.

The error is generated by executing the code below.

import tensorflow as tf

tensor1 = tf.Variable(4)
tensor2 = tf.Variable(6)
tensor3 = tf.Variable(3)
result = (tensor1 + tensor2) * tensor3

for result in tf.get_default_graph().get_operations():
    print (result.name)
Attributeerror Module 'tensorflow' has no attribute 'get_default_graph'

Look at the output of the above code, which shows the error when you try to access the get_default_graph from the tensorflow library.

This error is because you are executing this code from the TensorFlow version 2, but this code only works till version 1.x.

So here, your code is based on the old version of TensorFlow, and somehow, you updated the version of your TensorFlow 2.x something so that this error began to appear.

There are several solutions to this error, but here I will explain two ways to resolve this error, first using the tf.compat.v1 module and the second by downgrading the tensorflow version.

Let’s begin with the first method; you can use the tf. compat.v1 module in the TensorFlow version 2 to access all functionality or attributes of TensorFlow version 1.

import tensorflow as tf

tensor1 = tf.Variable(4)
tensor2 = tf.Variable(6)
tensor3 = tf.Variable(3)
result = (tensor1 + tensor2) * tensor3

for result in tf.compat.v1.get_default_graph().get_operations():
    print(result.name)
First Solution Attributeerror Module 'tensorflow' has no attribute 'get_default_graph'

When you try to access the get_default_graph() from the tf.compat.v1 module in the above code works without showing any error.

READ:  Python Tkinter Autocomplete

Remember, any function or attribute of TensorFlow you want to access in the environment of TensorFlow version 2, use the tf.compat.v1 module.

The next solution is to downgrade the current TensorFlow version to version 1 using the code below.

pip install tesnorflow==1.14

After downgrading, your code will work because your code is based on TensorFlow version 1, and you updated the TensorFlow to the latest version, so you are getting that error.

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

Conclusion

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

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

You may like to read: