In TensorFlow 2.0, tf.Session is no longer in use. TensorFlow 2.0 encapsulates graph computations as Python functions instead of using
Session making TensorFlow more Pythonic.
If you want to continue using Session in TensorFlow 2.0, use
You can follow the migration guide to migrate your TensorFlow code from TensorFlow 1.x to TensorFlow 2.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
AttributeError: module ‘tensorflow’ has no attribute ‘Session’
AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘module ‘tensorflow’ has no attribute ‘Session’” tells us that the TensorFlow module does not have the attribute
Session() class belongs to TensorFlow 1.x API, and we use it to run TensorFlow operations.
Generally, if the AttributeError refers to a module not having an attribute, either the functionality is under a different name or deprecated. Consult the documentation of the module to find where functionalities and sub-modules are.
Do not name python scripts after module names. For example, naming a script
tensorflow.py. If you try:
import tensorflow as tf
you will import the script file
tensorflow.py under your current working directory, rather than the actual TensorFlow module. The Python interpreter searches for a module first in the current working directory, then the PYTHONPATH, then the installation-dependent default path. You can name a script after its functionality instead.
What is a TensorFlow Session?
A Session is a class for running TensorFlow operations. A Session object encapsulates the environment to execute Operation objects and evaluate Tensor objects. We can create a Session in two ways, the standard object instantiation and using the context manager.
A session can own resources, which need to be released when they are no longer required. We can do this using the
tf.Session.close method on the Session object or use the session as a context manager.
Let’s look at the two implementations:
# Object instantiation sess = tf.Session() sess.run(...) sess.close() # Context manager with tf.Session as sess: sess.run(...)
We create a graph when we launch a session, and the Session returns a
graph_pb2.GraphDef proto containing the nodes for all the Operations in the underlying TensorFlow graph.
Let’s look at an example of defining a session to execute a graph operation.
import tensorflow as tf print(tf.__version__) # Initialize session using tf.Session with tf.Session() as sess: a = tf.constant(3.0) b = tf.constant(4.0) c = a + b # Execute addition operation print(sess.run(c))
Let’s run the code to see what happens:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-12-e18551284c0a> in <module> 1 import tensorflow as tf ----> 2 with tf.Session() as sess: 3 a = tf.constant(3.0) 4 b = tf.constant(4.0) 5 c = a + b AttributeError: module 'tensorflow' has no attribute 'Session'
The error occurs because the Session class is no longer present in TensorFlow 2.0.
Solution #1: Use tf.compat.v1
We can use the
tf.compat.v1 module to solve this error. The module contains the complete TF1.x API with its original semantics. Generally, you should avoid using the legacy
compat.v1 APIs for any new code you write in TensorFlow 2.0, but this approach is suitable for previously written code. Let’s look at the revised code:
import tensorflow as tf # Initialize session using tf.compat.v1.Session with tf.compat.v1.Session() as sess: a = tf.constant(3.0) b = tf.constant(4.0) c = a + b print(sess.run(c))
Let’s run the code to get the result:
Solution #2: Use tf.function
TensorFlow 2 uses functions instead of sessions, which integrates better with Python runtime. tf.function compiles a function into a callable TensorFlow graph. We can define a function with the decorator
@tf.function. Let’s look at how to add two numbers using
import tensorflow as tf @tf.function def compute_add(x, y): return tf.add(x, y) result = compute_add(3, 4) print(result)
tf.Tensor(7, shape=(), dtype=int32)
TensorFlow 1.x vs TensorFlow 2
TensorFlow 2 follows a fundamentally different programming paradigm from TensorFlow 1.x. There are different runtime behaviors around execution, variables, control flow, tensor shapes, and tensor equality comparisons. TensorFlow 2 is preferable to use as it removes redundant APIs and makes APIs more consistent.
To migrate to TensorFlow 2, follow the TF1.x to TF2 migration guide.
Congratulations on reading to the end of this tutorial! The AttributeError: module ‘tensorflow’ has no attribute ‘Session’ occurs when you try to create a Session to execute Operations or evaluate Tensors in TensorFlow 2.0. Session is a TF1.x class. To solve this error, you can either migrate to TensorFlow 2.0 and use the updated APIs or use
For further reading on TensorFlow, go to the articles:
- How To Solve Python AttributeError: module ‘tensorflow’ has no attribute ‘ConfigProto’
- How to Solve Python AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’
- How to Solve Python AttributeError: module ‘tensorflow.python.framework.ops’ has no attribute ‘_TensorLike’
- How to Solve Python AttributeError: module ‘tensorflow’ has no attribute ‘GraphDef’
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!