Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: The Session graph is empty. Python

Hello guys I am using Tensorflow 2.0

and in these lines of code:

import tensorflow as tf
hello = tf.constant('Hello World')
sess = tf.compat.v1.Session()
sess.run(hello) <-- Error in this line

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

Any idea on how to solve it?

like image 743
Alexander Rika Avatar asked Oct 18 '25 14:10

Alexander Rika


2 Answers

g = tf.Graph() 
with g.as_default():   
  # Define operations and tensors in `g`.   
  hello = tf.constant('hello')   
  assert hello.graph is g
    
sess = tf.compat.v1.Session(graph=g) 
sess.run(hello)

b'hello'

like image 67
Alexander Rika Avatar answered Oct 21 '25 04:10

Alexander Rika


Tensorflow core r2.0 have enabled eager execution by default. so, without changing it we just have to change our code as like as below by Launch the graph in a session.

> with tf.compat.v1.Session() as sess:
>     # Building a graph
>     hello = tf.constant("hello")
>     print(sess.run(hello))

According to the Tensorflow doc..

A default Graph is always registered, and accessible by calling tf.compat.v1.get_default_graph

For this basic operations not required to declare a tf.Graph() , you can define a graph which has more computations and dataset you can define a graph and invoke into the session.

Please Refer: For More informations https://www.tensorflow.org/api_docs/python/tf/Graph https://github.com/OlafenwaMoses/ImageAI/issues/400

like image 34
vel pradeep.MS Avatar answered Oct 21 '25 04:10

vel pradeep.MS