AttributeError: module ‘tensorflow’ has no attribute ‘Session’

Sometimes working with TensorFlow, you might face AttributeError: module ‘tensorflow’ has no attribute ‘Session’ error. This error is mainly caused by the syntax incompatibility of the TensorFlow module 2.x version. The session is no longer explicitly useful in 2.x version of TensorFlow. Although, the session attribute was used in 1.x version, no longer available in TensorFlow’s later versions. In this short article, we will discuss how we can solve the AttributeError: module ‘tensorflow’ has no attribute ‘Session’ error using various methods. Moreover, we will also discuss how the session and graph work in Tensorflow.

AttributeError: module 'tensorflow' has no attribute 'Session'

AttributeError: module ‘tensorflow’ has no attribute ‘Session’ – Possible Solutions

The error occurs because of the difference in the versions of TensorFlow. The session attribute works fine in 1.x version of TensorFlow but 2.x version of TensorFlow does not support the session attribute anymore.

Let us take an example to check why we are getting AttributeError: module ‘tensorflow’ has no attribute ‘Session’ error.

# importing the tensorlfow
import tensorflow as tf

# session in tensorflow
tf.Session()

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_598984/2232440934.py in <module>
      3 
      4 # session in tensorflow
----> 5 tf.session()

AttributeError: module 'tensorflow' has no attribute 'Session'

As we discussed the main reason for getting this error is because of the new version of TensorFlow. Let us check the version of the installed TensorFlow.

# checking the version
tf.__version__

Output:

'2.11.0'

Check the version of the installed TensorFlow on your system as well. If the version if greater than 1, then most probably the error is because of the updated version.

Solution-1: Using compat.v1.Session

Accordingly to the documentation, in TensorFlow 2.x, we can use the Session() method with compat.v1 as shown below:

# importing the tensorflow
import tensorflow.compat.v1 as tf

# session 
tf.compat.v1.Session()

Now, you will no more get the error if the error was because of the difference in the versions of TensorFlow.

Solution-2: Using TensorFlow 1.x Compatible syntax

There can be many possible ways to fix the error, one of the solutions is to use the TensorFlow 1.x compatible syntax. As session() is not compatible in 2.x Version of TensorFlow, we can disable the syntax of the new version of TensorFlow and can use the session with the old version. The following code will help us to disable 2.x syntax.

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

# session in tensorflow
tf.Session()

Now, if you run the code, you will no longer get the error because we fixed the error by disabling the 2.x syntax.

Solution-3: Convert TensorFlow 1.x Codebase to 2.x

Another method to get rid of AttributeError: module ‘tensorflow’ has no attribute ‘Session’ error is to convert the codebase of TensorFlow 1.x to TensorFlow 2.x using the following code.

tf_upgrade_v2 \
  --intree my_project/ \
  --outtree my_project_v2/ \
  --reportfile report.txt
AttributeError: module 'tensorflow' has no attribute 'Session'

For more details about how to use convert the TensorFlow 1.x codebase, you can visit their official website.

Solution-4: Degrading TensorFlow version

Degrading TensorFlow from version 2 to version 1.x will solve the problem but this method is not recommended as you will no longer be able to use the updates and new features of TensorFlow.

You can use any of the following commands to degrade the version of TensorFlow.

# pip version
pip install tensorflow==1.15.5

# pip3 version
pip3 install tensorflow==1.15.5

# if you are using conda
conda install tensorflow ==1.15.5

# if you are using jupyternote book
!pip install tensorflow==1.15.5

Hopefully, you will no more get the AttributeError: module ‘TensorFlow’ has no attribute ‘Session’ error.

Examples of Using Session() in TensorFlow 2.x

Let us now go through various kinds of examples to understand how we can use the Session() in Tensorflow version 2.x without getting AttributeError: module ‘tensorflow’ has no attribute ‘Session’ error.

Example -1 : Using compat.v1.Session()

# importing tensorflow
import tensorflow as tf

# using compat.v1
tf.compat.v1.disable_eager_execution()

# printing 
hello = tf.constant('Hello, TensorFlow!')

# session
sess = tf.compat.v1.Session()
print(sess.run(hello))

Output:

b'Hello, TensorFlow!'

By disabling eager execution, this code imports the TensorFlow library and configures it to operate in version 1 compatibility mode. The phrase “Hello, TensorFlow!” is then defined as a constant tensor. Finally, a TensorFlow session is created, the defined constant tensor is executed within the session, and the output is printed to the terminal with the words “Hello, TensorFlow!”

Example-2: Using the with keyword

# importing the tensorflow
import tensorflow as tf

# using session()
with tf.compat.v1.Session() as sess:
    hello = tf.constant('hello world')
    print(sess.run(hello))

Output:

b'hello world'

As you can see, we printed the word using the Session() in TensorFlow.

Summary

In this short article, we discussed how we can solve AttributeError: module ‘tensorflow’ has no attribute ‘Session’ error using various methods. We covered 4 different methods to solve the error and went through various examples to use Session() method in TensorFlow.

Related Issues:

Leave a Comment