![](/img/trans.png)
[英]Do you need the Django template brackets to pass string values in a form?
[英]In Tensorflow, do you need to feed values that aren't relevant to what you need?
我是否正确,在Tensorflow中,当我run
任何内容时,我的feed_dict
需要为所有占位符提供值,即使那些值与我正在运行的内容无关?
特别是我正在考虑进行预测,在这种情况下,我的targets
占位符是不相关的。
好吧,这取决于计算图的外观以及如何运行由张量(此处为placeholders
)馈送的操作。 如果您将在会话中执行的计算图的任何部分都不依赖于占位符,则不需要为其输入值。 这是一个小例子:
In [90]: a = tf.constant([5, 5, 5], tf.float32, name='A')
...: b = tf.placeholder(tf.float32, shape=[3], name='B')
...: c = tf.constant([3, 3, 3], tf.float32, name='C')
...: d = tf.add(a, c, name="Add")
...:
...: with tf.Session() as sess:
...: print(sess.run(d))
...:
# result
[8. 8. 8.]
另一方面,如果执行计算图的一部分,该部分依赖于占位符,则必须将其馈入一个值,否则它将引发InvalidArgumentError
。 这是一个演示此示例:
In [89]: a = tf.constant([5, 5, 5], tf.float32, name='A')
...: b = tf.placeholder(tf.float32, shape=[3], name='B')
...: c = tf.add(a, b, name="Add")
...:
...: with tf.Session() as sess:
...: print(sess.run(c))
...:
执行上面的代码,引发以下InvalidArgumentError
InvalidArgumentError:您必须使用dtype float和shape [3]输入占位符张量“ B”的值
[[节点:B = Placeholderdtype = DT_FLOAT,形状= [3],_ device =“ / job:localhost /副本:0 /任务:0 /设备:CPU:0”]]
因此,要使其正常工作,您必须使用feed_dict
来feed_dict
占位符,如下所示:
In [91]: a = tf.constant([5, 5, 5], tf.float32, name='A')
...: b = tf.placeholder(tf.float32, shape=[3], name='B')
...: c = tf.add(a, b, name="Add")
...:
...: with tf.Session() as sess:
...: print(sess.run(c, feed_dict={b: [3, 3, 3]}))
...:
...:
[8. 8. 8.]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.