Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 904
ISha Avatar asked Mar 04 '17 08:03

ISha


2 Answers

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(...)
    
like image 194
Xi Zhu Avatar answered Sep 22 '22 04:09

Xi Zhu


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>]
like image 43
prosti Avatar answered Sep 21 '22 04:09

prosti