Tensorflow: When should I use or not use `feed_dict`?

前端 未结 1 1382
猫巷女王i
猫巷女王i 2021-02-04 03:56

I am kind of confused why are we using feed_dict? According to my friend, you commonly use feed_dict when you use placeholder, and this is

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 04:46

    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, it's 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 don't it's an error.

    Let's fix that:

    with tf.Session() as sess:
      sess.run(y, feed_dict={x: 2})
    

    The result this time will be 84. Great. Now let's 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)
    

    0 讨论(0)
提交回复
热议问题