ISha
ISha

Reputation: 151

Session graph is empty

I have been trying to iterate the tensorflow model using the answer provided on Tensorflow : Memory leak even while closing Session? I have the same error as mentioned in one of the comments i.e. "The Session graph is empty. Add operations to the graph before calling run()." I cannot figure out how to solve it.

Upvotes: 9

Views: 23390

Answers (2)

prosti
prosti

Reputation: 46331

If you search the TensorFlow source code you will find this exclusive message is telling you the graph version is 0, which means it is empty.

enter image description here You need to load the graph from file or to create a graph like this (just an example):

import tensorflow as tf

pi = tf.constant(3.14, name="pi")
r = tf.placeholder(tf.float32, name="r")
a = pi * r * r

graph = tf.get_default_graph()
print(graph.get_operations())

The line print(graph.get_operations()) will confirm the graph is not empty:

[<tf.Operation 'pi' type=Const>, <tf.Operation 'r' type=Placeholder>, <tf.Operation 'mul' type=Mul>, <tf.Operation 'mul_1' type=Mul>]

Upvotes: 1

Xi Zhu
Xi Zhu

Reputation: 321

My suggestion is to make sure your session knows which graph it is running on. Somethings you can try are:

  1. build the graph first and pass in the graph to a session.

    myGraph = tf.Graph()
    with myGraph.as_default():
        # build your graph here
    
    mySession = tf.Session(graph=myGraph)
    mySession.run(...)
    
    # Or
    
    with tf.Session(graph=myGraph) as mySession:
        mySession.run(...)
    
  2. If you want to use multiple with statement, use it in a nested way.

    with tf.Graph().as_default():
        # build your graph here
        with tf.Session() as mySession:
            mySession.run(...)
    

Upvotes: 13

Related Questions