Attributeerror: module ‘tensorflow’ has no attribute ‘session’ error is caused by syntax incompatibility of TensorFlow module 2. x version. the session is no longer explicitly useful in TensorFlow 2. x version. Actually, this is used in Tensorflow 1. x version. There are multiple scenarios where we use the syntax | code base of the Tensorflow 1. x module in the TensorFlow 2.0 version. A few of them are –
- Complete code is on Tensorflow 2. x but any third-party code integration is required which is on Tensorflow 1. x.
- We took code references from any coder community where the versioning details are not available.
Anyways in this article, we will solve this AttributeError.
Attributeerror: module ‘tensorflow’ has no attribute ‘session'( Solution) –
See, There are multiple ways to fix this error. It completely depends on the efforts required to support your coding context.
Solution 1 : Using TensorFlow 1.x compatible syntax in Tensorflow 2.x –
Since as we explained tf.Session() is incompatible is TensorFlow 2.x then use the below import-
import tensorflow.compat.v1 as tf
If you run the above syntax in TensorFlow 2. x then it will support TensorFlow 1. x syntax.
Solution 2 : Convert TensorFlow 1.x codebase to Tensorflow 2.x –
In this solution, you don’t have to work on syntax. In place of that, you need to run this automated script and it will convert all the Tensorflow 1.x code base to Tensorflow 2.x code base.
--intree my_project/ \
--outtree my_project_v2/ \
Here the my_project_v2 is the target 2.x compatible code.
Solution 3 : Lowering the Tensorflow Version from 2.x to 1.x ( Not Recommended ) –
This is not recommended solution because it may create multiple incompatibilities with the code base if more syntax is 2. x supportive. But If you are sure that you are just using the 1. x code base and only the installation version is somehow 2. x then you should downgrade this with the simple below command.
pip install tensorflow==1.15.5
Attributeerror: module ‘tensorflow’ has no attribute ‘session’ :
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.