Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Placeholder_2:0 is both fed and fetched

When I run this code:

x = tf.placeholder(tf.int32, shape=(None, 3))
with tf.Session() as sess: 
    feed_dict = dict()
    feed_dict[x] = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
    input = sess.run([x], feed_dict=feed_dict)

I get this error:

Placeholder_2:0 is both fed and fetched.

I'm not sure what I'm doing wrong here. Why does this not work?

like image 517
Taivanbat Badamdorj Avatar asked Sep 03 '16 12:09

Taivanbat Badamdorj


2 Answers

Are you sure this code covers what you are trying to achieve? You ask to read out whatever you pass through. This is not a valid call in tensorflow. If you want to pass through values and do nothing with it (what for?) you should have an identity operation.

x = tf.placeholder(tf.int32, shape=(None, 3))
y = tf.identity(x)

with tf.Session() as sess: 
    feed_dict = dict()
    feed_dict[x] = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
    input = sess.run([y], feed_dict=feed_dict)

The problem is "feeding" actually kind of overwrites whatever your op generates, thus you cannot fetch it at this moment (since there is nothing being really produced by this particular op anymore). If you add this identity op, you correctly feed (override x) do nothing with the result (identity) and fetch it (what identity produces, which is whatever you feeded as an output of x)

like image 148
lejlot Avatar answered Sep 20 '22 16:09

lejlot


I found out what I was doing wrong.

x is a placeholder -- it holds information and evaluating x does not do anything. I forgot that vital piece of information and proceeded to attempt to run the Tensor x inside sess.run()

Code similar to this would work if, say, there was another Tensor y that depended on x and I ran that like sess.run([y], feed_dict=feed_dict)

like image 25
Taivanbat Badamdorj Avatar answered Sep 23 '22 16:09

Taivanbat Badamdorj