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 –
Anyways in this article, we will solve this AttributeError.
See, There are multiple ways to fix this error. It completely depends on the efforts required to support your coding context.
Since as we explained tf.Session() is incompatible is TensorFlow 2.x then use the below import-
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
If you run the above syntax in TensorFlow 2. x then it will support TensorFlow 1. x syntax.
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.
tf_upgrade_v2 \
--intree my_project/ \
--outtree my_project_v2/ \
--reportfile report.txt
Here the my_project_v2 is the target 2.x compatible code.
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
Thanks
Data Science Learner Team