How to Fix Module ‘TensorFlow’ has no attribute ‘session’

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

In one of my projects, I used the Session() attribute; when I ran my project after installing the latest version of Tensorflow, the error began to pop up on my terminal.

After digging into TensorFlow’s official document, I found some solutions, which I will explain here.

The solution is based on the tensorflow version that you are currently using. Let’s see how to fix the error.

What does Module ‘TensorFlow’ has no attribute ‘session’ mean?

The error Module ‘TensorFlow’ has no attribute ‘session’, indicating that the attribute ‘seesion’ doesn’t exist in the TensorFlow framework.

How I got this error when I tried to multiply two constant values through the session is shown below.

import tensorflow as tf

l = tf.constant(23,dtype="int32",name="val1")
m = tf.constant(22,dtype="int32",name="val2")
with tf.Session() as val:
    new_output=val.run(l*m)
    print(new_output)
Module ‘TensorFlow’ has no attribute ‘session’

As you can see in the output, shows the error module ‘tensorFlow’ has no attribute ‘session’, but ‘What is the reason behind this error?’.

The reason is the version of TensorFlow you are using. The above code is compatible with TensorFlow version 1.x, but you are trying to execute this code on TensorFlow version 2.x.

There are two solutions: one is to use the tf.compat.v1 module and the other is to downgrade the version of Tensorflow.

Let’s start with the first one,

So you have updated TensorFlow version 2.x something, and when you execute that code, you get an error. To resolve that error, you must access the function or attribute of TensorFlow version 1.x into the environment of TensorFlow version 2.x.

READ:  Python Turtle Font

TensorFlow version 2.x has a module called tf.compat.v1, which allows you to access and use all the TensorFlow 1 functions or attributes in the current TensorFlow version 2.x environment.

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

l = tf.constant(23,dtype="int32",name="val1")
m = tf.constant(22,dtype="int32",name="val2")

with tf.compat.v1.Session() as val:
    new_output=val.run(l*m)
    print(new_output)
First Solution to Module ‘TensorFlow’ has no attribute ‘session’

From the output, you can see the code is executed successfully without showing any error; here, look at the two lines of code; the first is the tf. compact.v1.disable_eager_execution(), and the second is tf. compact.v1.Session().

The tf. compact.v1.disable_eager_execution() disables eager execution. In TensorFlow version 1.x, eager execution was a mode where operations were executed immediately, like regular Python programs.

When the eager execution is disabled, it changes the mode to graph execution mode, which means that instead of running the operation immediately, it builds a computation graph of the operations and then executes or runs them within the session.

The following code tf.compat.v1.Session() accesses the Session() function from the tf.compat.v1 module because it doesn’t exist in the latest version of TensorFlow.

Remember to access the function or attribute of the old version of TensorFlow using the tf. compat.v1 module.

Moving to the next solution, you have to downgrade Tensorflow version 1.15 to fix the error.

Open your terminal and execute the below command.

pip install tensorflow==1.14  # run this on your terminal
!pip install tensorflow==1.14 # run this on your Notebook

When executing the above command, it uninstalls the current version of Tensorflow and installs version 1.14.

Again, you won’t get an error when you run the code or try to access the Session() attribute from the tensorflow library.

READ:  How to use Python Scipy Linprog

But here, I suggest you migrate all your tensorflow code from version 1 to 2. In version 2, several modules or functions have been removed. You must always stick with the latest version of Tensorflow and its documentation on using the function, API or module.

I hope you understand how to fix the error Module ‘TensorFlow’ has no attribute ‘session’ using the above two methods.

Conclusion

This tutorial taught you how to resolve the error Module ‘TensorFlow’ has no attribute ‘session’.

Where you have used the tf.compat.v1 module in the TensorFlow 2 environment to access the Session() function of TensorFlow version 1.

Also, you learned to downgrade the tensorflow version 1.14 to fix that error.

You may like to read: