. Advertisement .
. Advertisement .
Industry 4.0 entails the development of many different technology fields. Among them can be mentioned AI – Artificial Intelligence. Artificial intelligence has been contributing a lot of resources to human life. The explosion of AI, of course, led to the emergence of machine learning and deep learning. Because to make machine learning easier, humans have created a new type of tool called Tensorflow. If you want to go further in this AI industry, you will definitely have to know what Tensorflow is and how to use it.
Today, we will introduce to you a common error when using Tensorflow. It’s “AttributeError: module ‘tensorflow’ has no attribute ‘Session’”. Let’s see how to handle it!
When do you get “AttributeError: module ‘tensorflow’ has no attribute ‘Session’” error?
Below is an example of this error.
Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'tensorflow' has no attribute 'Session'
This error appeared when the you were running this line sess = tf.Session().
Here are more information about the versions that users use:
- OS Platform & Distribution: Windows10
- Python Version: 3.7.1
- Tensorflow Version: 2.0.0-alpha0 (installed with pip)
Next is the process of replication:
Firstly, install as follows:
- pip install –upgrade pip
- pip install tensorflow==2.0.0-alpha0
- pip install keras
- pip install numpy==1.16.2
Then, continue performance:
- Execute command: import tensorflow as tf
- Execute command: sess = tf.Session()
What is TensorFlow?
TensorFlow 2.0, released in October 2019, improves the framework in many ways based on user feedback, to make it easier and more efficient to work with. However, if you have already written code across versions Before that, you have to rewrite, sometimes a little, sometimes quite a bit, to take full advantage of the new features of TensorFlow 2.0.
With Tensorflow 2.0, according to the information given in the TF 1:1 Symbols Map, the declaration tf.Session() has been replaced by the command line tf.compat.v1.Session(). You can refer to this link to get more information about the changes.
You can change the command like below to get the same result as TF version 1.x while using 2.0 ver.
import tensorflow.compat.v1 as tf tf.disable_v2_behavior()
How to solve the “AttributeError: module ‘tensorflow’ has no attribute ‘Session’” error?
Method 1: Utilize tf.function
Sessions are replaced with functions in TensorFlow 2, which integrates better with the Python runtime. A function is converted into a callable TensorFlow graph using tf.function. The decorator @tf.function allows you to define a function. Let’s examine the tf.function’s two-number addition method:
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)
Method 2: Utilize session with compat v1
To solve the error “AttributeError: module ‘tensorflow’ has no attribute ‘Session’”, you simply need to use a session with compat v1 if you are using TensorFlow 2.0, as shown below.
Method 3: With TF 2.0, “Session()” has been eliminated.
With TF 2.0, “Session()” has been eliminated. Look at the following example:
import tensorflow as tf tf.compat.v1.disable_eager_execution() arg = tf.constant('Hello, World!!') sess = tf.compat.v1.Session() print(sess.run(arg))
Above is the basic information and how to handle AttributeError: module ‘tensorflow’ has no attribute ‘Session’. Hope readers can solve their problems and create their own more complex calculation functions later. See you in the next post.
→ Fixing AttributeError module ‘datetime’ has no attribute ‘now’
- VINTAGE ADS THAT WOULD HAVE NOT BEEN TOLERATED TODAY
- TIMES PEOPLE HAD PHOTO EVIDENCE TO BACK UP THEIR CRAZY STORIES
- THE COOLEST THINGS PEOPLE ACCIDENTLY DISCOVERED IN THEIR HOME
- SURPRISING COMPARISONS PHOTOS THAT MADE US SEE THINGS IN A NEW LIGHT
- POPULAR MYTHS EVERYONE BELIEVES BUT ARE NOT ACTUALLY TRUE
Leave a comment