How to feed a placeholder?

To feed a placeholder, you use the feed_dict argument to Session.run() (or Tensor.eval()). Let’s say you have the following graph, with a placeholder:

x = tf.placeholder(tf.float32, shape=[2, 2])
y = tf.constant([[1.0, 1.0], [0.0, 1.0]])
z = tf.matmul(x, y)

If you want to evaluate z, you must feed a value for x. You can do this as follows:

sess = tf.Session()
print sess.run(z, feed_dict={x: [[3.0, 4.0], [5.0, 6.0]]})

For more information, see the documentation on feeding.

Leave a Comment