Python 如何喂养占位符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33810990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to feed a placeholder?
提问by displayname
I am trying to implement a simple feed forward network. However, I can't figure out how to feed a Placeholder
. This example:
我正在尝试实现一个简单的前馈网络。但是,我不知道如何喂养Placeholder
. 这个例子:
import tensorflow as tf
num_input = 2
num_hidden = 3
num_output = 2
x = tf.placeholder("float", [num_input, 1])
W_hidden = tf.Variable(tf.zeros([num_hidden, num_input]))
W_out = tf.Variable(tf.zeros([num_output, num_hidden]))
b_hidden = tf.Variable(tf.zeros([num_hidden]))
b_out = tf.Variable(tf.zeros([num_output]))
h = tf.nn.softmax(tf.matmul(W_hidden,x) + b_hidden)
sess = tf.Session()
with sess.as_default():
print h.eval()
Gives me the following error:
给了我以下错误:
...
results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 419, in _do_run
e.code)
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape dim { size: 2 } dim { size: 1 }
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[2,1], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'Placeholder', defined at:
File "/home/sfalk/workspace/SemEval2016/java/semeval2016-python/slot1_tf.py", line 8, in <module>
x = tf.placeholder("float", [num_input, 1])
...
I have tried
我试过了
tf.assign([tf.Variable(1.0), tf.Variable(1.0)], x)
tf.assign([1.0, 1.0], x)
but that does not work apparently.
但这显然不起作用。
采纳答案by mrry
To feed a placeholder, you use the feed_dict
argument to Session.run()
(or Tensor.eval()
). Let's say you have the following graph, with a placeholder:
要提供占位符,请使用(或)的feed_dict
参数。假设您有下图,并带有占位符:Session.run()
Tensor.eval()
x = tf.placeholder(tf.float32, shape=[2, 2])
y = tf.constant([[1.0, 1.0], [0.0, 1.0]])
z = tf.matmul(x, y)
If you want to evaluate z
, you must feed a value for x
. You can do this as follows:
如果要评估z
,则必须为 提供值x
。您可以按如下方式执行此操作:
sess = tf.Session()
print sess.run(z, feed_dict={x: [[3.0, 4.0], [5.0, 6.0]]})
For more information, see the documentation on feeding.