在 Tensorflow 中,您是否需要输入与您的需求无关的值?

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: You must feed a value for placeholder tensor 'B' with dtype float and shape [3]

[[Node: B = Placeholderdtype=DT_FLOAT, shape=[3], _device="/job:localhost/replica:0/task:0/device: CPU:0"]]


因此,要使其正常工作,您必须使用 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.]