Attributeerror: module ‘tensorflow’ has no attribute ‘session’

Attributeerror_ module 'tensorflow' has no attribute 'session'

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 –

  1. Complete code is on Tensorflow 2. x but any third-party code integration is required which is on Tensorflow 1. x.
  2. 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
tf.disable_v2_behavior()

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.

tf_upgrade_v2 \
  --intree my_project/ \
  --outtree my_project_v2/ \
  --reportfile report.txt
attributeerror module 'tensorflow' has no attribute 'session' by code base conversion
Attributeerror: module ‘tensorflow’ has no attribute ‘session’ by code base conversion

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’   :

1.attributeerror: module tensorflow has no attribute contrib ( Solved )

2. Attributeerror: tensor object has no attribute numpy : Tricks to Fix

Thanks

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner