What is TensorFlow 2.0 has no attribute session error and how to solve this?What is TensorFlow 2.0 has no attribute session error and how to solve this?
Today we will see the main reason behind the "TensorFlow 2.0 has no attribute session" error in TensorFlow and also see what can be done in TensorFlow 2.0. First of all let me tell you that TensorFlow 2.0 is developed from scratch to ease the development, training and deployment of machine learning model. In TensorFlow 2.0 many new API has been introduced and many old has been removed or deprecated.
This is not an error or issue with the TensorFlow 2.0, actually the session function has been removed from the TensorFlow 2.0 in favour of eager execution. If you try to run the following code:
sess = tf.Session()
For getting the Session object in TensorFlow 2.0 then it will throw the error "TensorFlow 2.0 has no attribute session". So, this error comes in TensorFlow 2.0 because this has been removed in the eager execution mode.
Here is simple example of Hello World program written in TensorFlow 1.x:
import tensorflow as tf msg = tf.constant('Say Hello to TensorFlow!') sess = tf.Session() print(sess.run(msg))
If you try to run in TensorFlow 2.0 it will throw following error:
AttributeError Traceback (most recent call last) <ipython-input-2-ab14ce10de0b> in <module>() 1 import tensorflow as tf 2 msg = tf.constant('Say Hello to TensorFlow!') ----> 3 sess = tf.Session() 4 print(sess.run(msg)) AttributeError: module 'tensorflow' has no attribute 'Session'
Here is the screen shot of error:
It is recommended to upgrade your code to latest version of TensorFlow 2.0. You should replace every occurrences of sess.Session.run call with the Python function. TensorFlow 2.0 provides the utility to upgrade your TensorFlow 1.x code to TensorFlow 2.0. You can use this utility to upgrade your code.
The eager execution is by default enabled in TensorFlow 2.0. TensorFlow 2.0 eager execution is extremely necessary programming environment. It immediately executes the operations without building the graphs. The operation in TensorFlow 2.0 returns concrete value as in Python. During this process no graph is constructed and computation is performed immediately. This makes development and debugging of program much easier.
Now any Python interpreter can be used for interactive debugging of the TensorFlow 2.0 code, which makes development process much easier. Previously in TensorFlow 1.x debugging code and model was very tough task. TensorFlow 2.0 changes all that by introducing eager execution, which makes debugging of machine learning model much easier.
For compatibility reason and to run the TensorFlow 1.x code TensorFlow 2.0 provided option to disable eager execution for running old code. But it is advisable to upgrade your code to TensorFlow 2.0.
Here is simple of running the Session in TensorFlow 2.0:
import tensorflow as tf tf.compat.v1.disable_eager_execution() hello = tf.constant('Hello, Welcome') sess = tf.compat.v1.Session() print(sess.run(hello))
Above code shows you how to run the Session in TensorFlow 2.0. But you should upgrade your code for TensorFlow 2.0.
Here is the screen short of running example in Google Colab:
Check related Tutorials of TensorFlow 2.0: