2

I am trying to restore graph from a checkpoint. The checkpoint is created by tf.Supervisor. There are both meta file and checkpoint.

What I try to achive is to load this graph from separate application to run some operation (i.e. resue existing model).

I do this as the following (as explained here: https://www.tensorflow.org/api_docs/python/tf/train/import_meta_graph):

meta = 'path/to/file.meta'

my_graph = tf.Graph()
with my_graph.as_default():
        with tf.Session() as sess:
                saver = tf.train.import_meta_graph(meta)
                saver.restore(sess, tf.train.latest_checkpoint(os.path.dirname(meta)))
                op = my_graph.get_operation_by_name("op")
                print(sess.run(op))

What I see is None. What I expect to see is 1-D Tensor. I inspected my_graph object using get_collection and find that there are all my variables required for op to run correctly initialized with values restored from the checkpoint. How can I figure out why the operation is not evaluated correctly? I am really stuck here.

The following code:

print(sess.run(my_graph.get_operation_by_name("Variable_2")))
print(sess.run(my_graph.get_tensor_by_name("Variable_2:0")))

prints

None
4818800

as if there is no connection between an operation and corresponding variable.

0x2207
  • 878
  • 1
  • 6
  • 20

1 Answers1

5

The tf.Graph.get_operation_by_name() method always returns a tf.Operation object. When you pass a tf.Operation object to tf.Session.run(), TensorFlow will execute that operation (and everything on which it depends) and discard its outputs (if any).

If you are interested in the value of a particular output, you have to tell TensorFlow which output (a tf.Tensor) you are interested in. There are two main options:

  • Get a tf.Operation from the graph and then select one of its outputs:

    op = my_graph.get_operation_by_name("op")
    output = op.outputs[0]
    print(sess.run(output))
    
  • Get a tf.Tensor from the graph by calling tf.Graph.get_tensor_by_name(), and appending ":<output index>" to the operation's name:

    output = my_graph.get_tensor_by_name("op:0")
    print(sess.run(output))
    

Why does TensorFlow draw this distinction? For one thing, a operation can have multiple outputs, so it is sometimes necessary to be specific about which output you want to fetch. For another, an operation may have a side effect and produce a large output—see tf.assign() for an example—and it is often more efficient to pass the tf.Operation to sess.run() so that the value is not copied back into the Python program.

mrry
  • 125,488
  • 26
  • 399
  • 400