Home Python AttributeError: module ‘TensorFlow’ has no attribute ‘Session’

AttributeError: module ‘TensorFlow’ has no attribute ‘Session’

AttributeError: module ‘TensorFlow’ has no attribute ‘Session’

Today, we will discuss on Tensorflow what is it actually and why to return AttributeError: module ‘TensorFlow’ has no attribute ‘Session’. let begin! and learn module ‘TensorFlow’ has no attribute ‘Session’.

So, without wasting time as I ready cover some interesting topics on Python, I have solved lots of errors of Python. Such as TypeError: must be str, not tuple which is still going on.

Let’s begin!


What is Tensorflow?

TensorFlow crashes when running a session with an attribute error.

Three necessary ingredients for this issue to occur:

  1. A tensor file.
  2. An uploaded dataset.
  3. The model trained on that data

Actually, it is the Python most popular library which is using for machine learning. Students of College and University getting assignments on Tensorflow. So, I will be happy if I describe something special today for you guys.

Why not, if Tensorflow author solved all bugs while launching, but no problem. Let’s have a look.

AttributeError: module ‘TensorFlow’ has no attribute ‘Session’

Most of the time it comes. Whenever you run a file in Visual Studio or sublime text 3 or which you feel comfortable software, it shows.

Let’s get started!

  • First of all, you should have a look at here.
  • On line number 45, there is tf.Session.
  • Pick this up from there or you can directly use it by the following line.
import tensorflow.compat.v1 as tf
  • Of course, the above line will help well.
  • Then you should avoid migrating it. More information for Migration in Tensorflow here.

TF1.x hello world

In this section, we will use TF1, which is preferable for all of us.

Your code looks like following:

import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
sess = tf.Session()

  • In the first line, we just import the Tensorflow library.
  • Then we mentioned that name to tf so, no need to again use Tensorflow word.
  • Okay, then made a new variable and pass the new built-in function.
  • Then create a new Session().
  • Simple print the whole statement you made. Run is a built-in function keep in mind.
  • Pass the msg variable which you made.

TF2.x hello world

Here we will use a new way. So, it is very simple and short to use.

import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
  • Isn’t it very short than TF1?
  • Just import and variable and then print it.

Or if the above codes will help work you should consider the following command.

pip3 install --upgrade --force-reinstall tensorflow-gpu==1.15.0
  • Now, in the above line, you are going to change Tensorflow version.
  • Or else, you can check the history of all versions here.

That’s it.

Let us know in the comment box what your thought.


Please enter your comment!
Please enter your name here