【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
.

发布了397 篇原创文章 · 获赞 541 · 访问量 255万+
展开阅读全文

keras 并发load_model报错

10-24

我通过web代码实时加载模型进行预测,但报如下错误 Traceback (most recent call last): File "/root/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__ return self.wsgi_app(environ, start_response) File "/root/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app response = self.handle_exception(e) File "/root/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception reraise(exc_type, exc_value, tb) File "/root/anaconda3/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/root/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "/root/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "/root/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "/root/anaconda3/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/root/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "/root/anaconda3/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/root/anaconda3/code/App.py", line 41, in predict model=load_model(root_path+model_name) File "/root/anaconda3/lib/python3.6/site-packages/keras/models.py", line 249, in load_model topology.load_weights_from_hdf5_group(f['model_weights'], model.layers) File "/root/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 3008, in load_weights_from_hdf5_group K.batch_set_value(weight_value_tuples) File "/root/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2189, in batch_set_value get_session().run(assign_ops, feed_dict=feed_dict) File "/root/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 895, in run run_metadata_ptr) File "/root/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1071, in _run + e.args[0]) TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(1, 16), dtype=float32) is not an element of this graph. 问答

Tensorboard报错 feed_dict问题

04-10

``` merge_summary = tf.summary.merge_all() summary_writer = tf.summary.FileWriter('D:/mypath',sess.graph) # 执行训练迭代 for it in range(iterations): for n in range(batches_count): summary,_=sess.run([merge_summary,train_step],feed_dict={x: input_images[n*batch_size:(n+1)*batch_size], y_: input_labels[n*batch_size:(n+1)*batch_size], keep_prob: 0.45}) summary_writer.add_summary(summary, i) if remainder > 0: start_index = batches_count * batch_size; summary,_=sess.run([merge_summary,train_step],feed_dict={x: input_images[start_index:input_count-1], y_: input_labels[start_index:input_count-1], keep_prob: 0.45}) summary_writer.add_summary(summary, i) # 每完成五次迭代,判断准确度是否已达到100%,达到则退出迭代循环 iterate_accuracy = 0 if it%5 == 0: iterate_accuracy = accuracy.eval(feed_dict={x: val_images, y_: val_labels, keep_prob: 1.0}) print ('第 %d 次训练迭代: 准确率 %0.5f%%' % (it, iterate_accuracy*100)) if iterate_accuracy >= 0.95: count=count+1 if count>4: break; ``` 报错:InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_2' with dtype float and shape [?,1280] [[Node: Placeholder_2 = Placeholder[dtype=DT_FLOAT, shape=[?,1280], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]] 感觉是这个句子错了: ``` summary,_=sess.run([merge_summary,train_step],feed_dict={x: input_images[n*batch_size:(n+1)*batch_size], y_: input_labels[n*batch_size:(n+1)*batch_size], keep_prob: 0.45}) summary_writer.add_summary(summary, i) ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览