2

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.

pohe
  • 129
  • 1
  • 3
  • 11

1 Answers1

5

Naming conventions in tensorflow are subtle and a bit offsetting at first.

The thing is, when you write

a = tf.constant(5.0, name='a')

a is not the constant op, but its output. Names of op outputs derive from the op name by adding a number corresponding to its rank. Here, constant has only one output, so its name is

print(a.name)
# `a:0`

When you run sess.graph.get_operation_by_name('a') you do get the constant op. But what you actually wanted is to get 'a:0', the tensor that is the output of this operation, and whose evaluation returns an array.

a = sess.graph.get_tensor_by_name('a:0')
print(sess.run(a))
# 5
P-Gn
  • 23,115
  • 9
  • 87
  • 104
  • 3
    thanks so much for the clarification! I also learned that I can print out the same thing by `a = sess.graph.get_operation_by_name('a')` and `sess.run(a.outputs.pop())` – pohe Jul 12 '17 at 13:59