Placeholder_2:0 is both fed and fetched

2019-04-24 00:39发布

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?

2条回答
对你真心纯属浪费
2楼-- · 2019-04-24 01:30

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)

查看更多
姐就是有狂的资本
3楼-- · 2019-04-24 01:34

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)

查看更多
登录 后发表回答