pythontensorflowsessiontensorflow2.0

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?


Solution

  • 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'