8

I know that when getting a tensor by name that I must append an output index

ex)

graph.get_tensor_by_name('example:0')

Where :0 is the output index. But why is this necessary?

here is a link to get_tensor_by_name in tensorflow's docs. However, it does not mention specifying an output index.

BadProgrammer
  • 371
  • 3
  • 17

2 Answers2

11

In TensorFlow, names are given to tf.Operation objects (which correspond to nodes in the tf.Graph), and a tf.Tensor object is named for the tf.Operation that produces it as an output.

Since a tf.Operation can have more than one output, to name a tf.Tensor uniquely, we include its index in the the outputs as part of its name.

Therefore, we chose the following format for the name of a tf.Tensor object, which is also the value returned by the tf.Tensor.name property:

<name of operation>:<index of output>
mrry
  • 125,488
  • 26
  • 399
  • 400
  • I couldn't understand this function! :( I couldn't find this anywhere. Please explain clearly what it does and what is the use of this function!! – SRIDHARAN Dec 06 '17 at 15:05
  • @Sridharan If you have a question about this function (I'm assuming you mean `tf.Graph.get_tensor_by_name()`?), then I'd suggest you ask it as a new question. From your comment, it's unclear what you couldn't understand. – mrry Dec 06 '17 at 15:12
  • What I couldn't understand is "What will be effect/use after executing that function?" – SRIDHARAN Dec 06 '17 at 15:36
0

The crux to understand the get_tensor_by_name function is to realize that a TensorFlow model is first specified as a graph during the construction phase. In the question, example is the name of an operation node in this graph. When this operation is run during the execution phase, output is produced by this example node. This output is indexed by a number indicating its production order. In this case, it is 1.

The graph and its outputs described above were saved during an earlier run. Later in a different project execution, this run time state was restored possibly by using tf.train.import_meta_graph and graph becomes a reference to it. The function get_tensor_by_name simply recovers a reference to the saved second output of the example node from the restored run time meta graph. You can now make use this recovered tensor in your own task during another session run.

Taimi
  • 1
  • 2