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.
Asked
Active
Viewed 2.3k times
9
-
1What is the code you're trying to run? – Franck Dernoncourt Mar 04 '17 at 17:31
2 Answers
13
My suggestion is to make sure your session knows which graph it is running on. Somethings you can try are:
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(...)
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(...)

Xi Zhu
- 311
- 4
- 10
1
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.
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>]

prosti
- 42,291
- 14
- 186
- 151