1. 程式人生 > >【Tensorflow】報錯:Cannot interpret feed_dict key as Tensor: The name 'x' refers to an operation, # > no

【Tensorflow】報錯:Cannot interpret feed_dict key as Tensor: The name 'x' refers to an operation, # > no

問題描述:

我嘗試給一個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
.