12

I think this question may sound weird. Let's say, there is a tensor a.

x = tf.placeholder(tf.float32, [None, 2400], name="x")
y = tf.placeholder(tf.float32, [None, 2400], name="y")
a = tf.add(x, y, name="a")

Is there an efficient way to refer a with a different name, like out? I am thinking a dummy operation like

b = tf.add(a, 0, name="out")

I am asking this because I am trying different network architectures, and regardless of architectures, I would like to access the output tensor with a consistent name like

tf.get_default_graph().get_tensor_by_name("out:0")`

For now, output tensors are depending on architectures like fc1/fc1wb:0 or fc2/fc2wb:0. How can I wrap the final op with a certain name?

YW P Kwon
  • 2,138
  • 5
  • 23
  • 39

1 Answers1

15

This answer suggests that tf.Graph is append only, you cannot modify it.

That being said, a slightly better way to rename a tensor is to use tf.identity like this:

tf.identity(a, name="out")

EDIT: After figuring out answer to one of my own questions, I can see a slightly better way to do this:

graph_def.node[<index of the op you want to change>].name = "out"

the index is usually -1 because final output tensors are in the end of the graph_def

Quanlong
  • 24,028
  • 16
  • 69
  • 79
Priyatham
  • 2,821
  • 1
  • 19
  • 33
  • 2
    Changing `graph_def.node[index].name` does not work. If this node is input to any other node, such a reference is by name, so you need to update those as well. – Thijs Dec 12 '17 at 16:43
  • 1
    To update the references to your renamed node, loop through `graph.node` and for each such node through `node.input`. If `node.input[2]` is your renamed node, you can replace `node.input[2] = new_name`. – Thijs Dec 12 '17 at 16:58