Tensorflow: When should I use or not use `feed_dict`?
Tensorflow: When should I use or not use `feed_dict`?
In a tensorflow model you can define a placeholder such as x = tf.placeholder(tf.float32)
, then you will use x
in your model.
For example, I define a simple set of operations as:
x = tf.placeholder(tf.float32)
y = x * 42
Now when I ask tensorflow to compute y
, its clear that y
depends on x
.
with tf.Session() as sess:
sess.run(y)
This will produce an error because I did not give it a value for x
. In this case, because x
is a placeholder, if it gets used in a computation you must pass it in via feed_dict
. If you dont its an error.
Lets fix that:
with tf.Session() as sess:
sess.run(y, feed_dict={x: 2})
The result this time will be 84
. Great. Now lets look at a trivial case where feed_dict
is not needed:
x = tf.constant(2)
y = x * 42
Now there are no placeholders (x
is a constant) and so nothing needs to be fed to the model. This works now:
with tf.Session() as sess:
sess.run(y)