Sharyn Hu
Sharyn Hu

Reputation: 11

Why do I get different results when printing tensor names?

I was trying to print the names of the nodes in a graph and I got different results using different codes.

The placeholder is defined as:

x = tf.placeholder("float", shape=[None, 784], name = 'input_x')

If I run the code:

node_names = [node.name for node in tf.get_default_graph().as_graph_def().node]
for item in node_names:
    print(item)

And I get the result like this:

input_x
origin_y
truncated_normal/shape
truncated_normal/mean
truncated_normal/stddev
truncated_normal/TruncatedNormal
truncated_normal/mul
truncated_normal

But if I run the following code:

print('Name for input:')
print(x.name)

There is a ":0" added at the end of the name:

Name for input:
input_x:0

I am confused about it. Could any one explain this for me? Thanks.

Upvotes: 0

Views: 30

Answers (1)

dm0_
dm0_

Reputation: 2156

Node in the graph represents an operation. In the loop you iterate over nodes and print their names.

Names ending with :<num> correspond to tensors. Tensors are outputs of operations.

tf.placeholder function returns tensor, but you also can get corresponding operation:

x = tf.placeholder('float', shape=[None, 784], name = 'input_x')

print(repr(x))         # <tf.Tensor 'input_x:0' shape=(?, 784) dtype=float32>
print(repr(x.name))    # u'input_x:0'

print(repr(x.op))      # <tf.Operation 'input_x' type=Placeholder>
print(repr(x.op.name)) # u'input_x'

Upvotes: 1

Related Questions