Talespin_Kit
Talespin_Kit

Reputation: 21877

How does the tensorflow session.run() method knows the name of the placeholder variables?

From the tensorflow tutorial section https://www.tensorflow.org/guide/low_level_intro#feeding the following code creates the placeholders and assign it to variables 'x' and 'y' and is passed to the run method.

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = x + y

print(sess.run(z, feed_dict={x: 3, y: 4.5}))

How does the sess.run() method know the name of the variables 'x' and 'y'. ie. How does the run method know the keys of the feed_dict argument. Is there a mechanism in the python to figure out the name of the variables ?

Upvotes: 2

Views: 337

Answers (1)

Kaihong Zhang
Kaihong Zhang

Reputation: 419

Most of objects in tensorflow can be found with a string.

When you invoke tf.placeholder(tf.float32), tensorflow will do the following:

  • create a node with the Placeholder op
  • add this node to default graph
  • return the node output tensor

You can set a name for any node, say tf.placeholder(tf.float32, name='myplaceholder'), if you don't specify a node name, tensorflow will generate one, you can use print x.op to see the name of the op.

A tensor is named with the node name plus the output index, for example

x = tf.placeholder(tf.float32)
print x

you will see something like Placeholder:0, which is the tensor name.

So, in you code, tensorflow can first get tensor name from x, and iterate the default graph to find proper node.

You can also use string for feed_dict, {"Placeholder:0": 3}

Upvotes: 2

Related Questions