0

Am I correct that in Tensorflow, when I run anything, my feed_dict needs to give values to all my placeholders, even ones that are irrelevant to what I'm running?

In particular I'm thinking of making a prediction, in which case my targets placeholder is irrelevant.

kmario23
  • 57,311
  • 13
  • 161
  • 150
Eric Auld
  • 1,156
  • 2
  • 14
  • 23
  • 1
    Did you try? Did it give error? Usually, the answer is no. Tensorflow builds computation graph that is connected to the variable that you are running in the session. You only need to feed in variables that are needed as input to compute it. – Dinesh Jul 10 '18 at 20:58

1 Answers1

2

Well, it depends on how your computation graph looks like and how you run the ops which are fed by tensors (here: placeholders). If there's no dependency on the placeholder in any part of the computation graph that you'll execute in the session, then it does not need to be fed a value. Here's a small example:

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

On the other hand, if you execute a part of the computation graph which has a dependency on the placeholder then a value it must be fed else it will raise InvalidArgumentError. Here's an example demonstrating this:

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))
    ...:       

Executing the above code, throws the following 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"]]


So, to make it work, you've to feed the placeholder using feed_dict as in:

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.]
Community
  • 1
  • 1
kmario23
  • 57,311
  • 13
  • 161
  • 150