当我运行这段代码时:
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)
我得到了这个错误:
Placeholder_2:0 is both fed and fetched.
我不确定我哪里做错了。为什么这不工作呢?
回答:
你确定这段代码涵盖了你想要实现的功能吗?你要求读取你传递的所有内容。这在TensorFlow中不是一个有效的调用。如果你想传递值而不对其做任何处理(为什么要这样做?),你应该使用一个身份操作。
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)
问题在于“馈送”实际上是覆盖了你的操作所生成的任何内容,因此你不能在此时获取它(因为这个特定的操作不再真正产生任何东西)。如果你添加这个身份操作,你就可以正确地馈送(覆盖x),不对结果做任何处理(身份操作),然后获取它(获取身份操作产生的,即你馈送给x的输出)。