I'd like to build a tensorflow graph in a separate function get_graph()
, and to print out a simple ops a
in the main function. It turns out that I can print out the value of a
if I return a
from get_graph()
. However, if I use get_operation_by_name()
to retrieve a
, it print out None. I wonder what I did wrong here? Any suggestion to fix it? Thank you!
import tensorflow as tf
def get_graph():
graph = tf.Graph()
with graph.as_default():
a = tf.constant(5.0, name='a')
return graph, a
if __name__ == '__main__':
graph, a = get_graph()
with tf.Session(graph=graph) as sess:
print(sess.run(a))
a = sess.graph.get_operation_by_name('a')
print(sess.run(a))
it prints out
5.0
None
p.s. I'm using python 3.4 and tensorflow 1.2.