问题描述:
我尝试给一个tensor输入值的时候报错:
Cannot interpret feed_dict key as Tensor: The name 'x' refers to an operation, not a Tensor. Tensor names must be of the form "<op_name>:<output_index>".
代码如下:
import tensorflow as tf
x = tf.placeholder(tf.float32, (None,), 'x')
y = tf.reduce_sum(x)
sess = tf.Session()
sess.run(y, {x: [1, 2, 3]}
# > 6.0
sess.run(y, {'x': [1, 2, 3]}
# > Cannot interpret feed_dict key as Tensor: The name 'x' refers to an operation,
# > not a Tensor. Tensor names must be of the form "<op_name>:<output_index>".
sess.run(y, {tf.get_default_graph().get_operation_by_name('x').outputs[0]: [1, 2, 3]})
# > 6.0
有没有可能feed placeholders以他们的变量名呢?如果不能,为什么?这对随后从磁盘恢复网络后进行feed很有用。
问题解决:
你需要添加 ":0",例如:
print sess.run(y, {'x:0': [1, 2, 3]})
这里是为什么 ":0"要被加上的原因:http://stackoverflow.com/a/37870634/419116
.