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

Share This Post

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
tf.disable_v2_behavior()
  • 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()
print(sess.run(msg))

  • 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!')
tf.print(msg)
  • 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.

spot_img

Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img