Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: The Session graph is empty. Add operations to the graph before calling run() #400

Closed
Krishnarohith10 opened this issue Nov 5, 2019 · 14 comments

Comments

@Krishnarohith10
Copy link

@Krishnarohith10 Krishnarohith10 commented Nov 5, 2019

keras==2.3.1 and tensorflow==2.0.0
can somebody help me ?
Please !

@Krishnarohith10
Copy link
Author

@Krishnarohith10 Krishnarohith10 commented Nov 5, 2019

# Build a graph.
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b

# Launch the graph in a session.
sess = tf.compat.v1.Session()

# Evaluate the tensor `c`.
print(sess.run(c))

This above code is taken from Tensorflow Core r2.0 Document
But the above code when it is run, then we get this error

RuntimeError: The Session graph is empty. Add operations to the graph before calling run()

The thing is

  1. The tensorflow core r2.0 have enabled eager execution by default so doesn't need to write tf.compat.v1.Session() and use .run() function
    If we want to use tf.compat.v1.Session() then we need to do thi
    tf.compat.v1.disable_eager_execution() in the starting of algorithm. Now we can use tf.compat.v1.Session() and .run() function.

  2. Tensorflow core r2.0 have enabled eager execution by default. so, without changing it
    we just have to change our code

     # Launch the graph in a session.
     with tf.compat.v1.Session() as ses:
    
         # Build a graph.
         a = tf.constant(5.0)
         b = tf.constant(6.0)
         c = a * b
    
         # Evaluate the tensor `c`.
         print(ses.run(c))
    

This gives the output without any errors
And one more thing to make eager execution enable in case then remember it has to be called in the startup of the algorithm
For more please go through documentation
If any issues please feel free to ask.
By the way i am just a beginner in tensorflow and keras.
Thank You !

@HARIKABANDARU
Copy link

@HARIKABANDARU HARIKABANDARU commented Dec 8, 2019

Running the tensor flow session after disabling eager_execution as stated above still finding the same bug.
Tensoflow - 2.0.0 Keras - 2.3.1
Enbironment Kaggle

@haricoolharan
Copy link

@haricoolharan haricoolharan commented Dec 11, 2019

@Krishnarohith10 The resolution helped me.
Thanks for sharing

@Krishnarohith10
Copy link
Author

@Krishnarohith10 Krishnarohith10 commented Dec 11, 2019

@HARIKABANDARU

Screenshot (10)

Here's my implementation of the above issue. I have had it done using Kaggle Notebook. It is working. If not, can you post your code?

@HARIKABANDARU
Copy link

@HARIKABANDARU HARIKABANDARU commented Dec 25, 2019

@Krishnarohith10
I worked for me for small data set.
The only issue i faced was when I ran with large dataset it throws session graph empty error.

@Krishnarohith10
Copy link
Author

@Krishnarohith10 Krishnarohith10 commented Dec 26, 2019

Okay. Did you try first method? Using tf.compact.v1.disable_eager_execution(), you can now use tf.session() with .run(). Which I hope it works and hope you are familiar with.
And I didn't try this on large datasets so, I will let you now when I did.

@pushkarravi
Copy link

@pushkarravi pushkarravi commented Jan 2, 2020

Hi @Krishnarohith10 , I tried following your approach, but still having the issue.
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

Please suggest on how to fix this.

Here is my code
`import tensorflow as tf

hello_constant = tf.constant('Hello World!')
hello_constant

tf.compat.v1.disable_eager_execution()
sess = tf.compat.v1.Session()
print(sess.run(hello_constant))`
TF Error

@Krishnarohith10
Copy link
Author

@Krishnarohith10 Krishnarohith10 commented Jan 3, 2020

1
Please try move In [3] command to In [2] that is after importing tensorflow.
OR
You can use this method:
1

@pushkarravi
Copy link

@pushkarravi pushkarravi commented Jan 3, 2020

1
Please try move In [3] command to In [2] that is after importing tensorflow.
OR
You can use this method:
1

@Krishnarohith10 .. it works. Thank you so much

@marcelotavaress
Copy link

@marcelotavaress marcelotavaress commented Jan 7, 2020

it worked for me too

@Renuga-DataScience
Copy link

@Renuga-DataScience Renuga-DataScience commented Jul 13, 2020

Untitled

@Renuga-DataScience
Copy link

@Renuga-DataScience Renuga-DataScience commented Jul 13, 2020

worked out perfectly

@Krishnarohith10
Copy link
Author

@Krishnarohith10 Krishnarohith10 commented Jul 14, 2020

@HARIKABANDARU and also to everyone,
Good Morning or Good Afternoon or Good Evening or Good Night (respective to your time zones)
Straight to the point,
tensorflow=2.0.0 keras=2.2.4-tf, The tf.Session() is used for Tensorflow=1.x, we been using tf.Session() in Tensorfow=2.0, What a dumb, not that we can't use but we have a option, and in this I will Explain it all well.
See with upto tensorflow=1.x, we have to use tf.Session() that what makes your code to run and give us output cause there is this tf.executing_eagerly()-->False in tensorflow=1.x version. Have a look:

1

Ignore those messages those are because of the version of tensorflow, since we're using old version and also there been a lot changes to that version.

Now Unlike tensorflow=1.x, tensorflow=2.x has tf.executing_eagerly()-->True which means we no need to use tf.Session(), tf.Session() is only meant for if tf.executing_eagerly()-->False, which in case of tensorflow=1.x, but as tensorflow=2.x is True for tf.executing_eagerly(), we don't need to use it. We can surely omit that statement and can do your coding or projects. Have a look:

2

and if we want the result or value of your output variable we can access it by using .numpy()

3

Here it is! That's all I wanted to say, and everyone please update your working libraries like tensorflow or anyother librabries to the latest version so that we don't wanna stay behind the world, do we? And I myself with the latest versions of machine-learning and deep-learning libraries working on few projects, because I wanted to work or get a job in this A.I domain. This is a little background of me to you. I been dealing with this libraries pretty much years till now, still even I got few issues which can't solve but anyways. If any queries please don't hesitate to ask regarding tensorflow, please! And @HARIKABANDARU Please try just doing this with your project or If you give me data or If it available online then I can tell test it myself and tell you the results if not you can try your own.

@allarassemjonathan
Copy link

@allarassemjonathan allarassemjonathan commented Dec 26, 2020

@Krishnarohith10 thank you for sharing it was very useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants