TensorFlow feed_dict: Use feed_dict To Feed Values To TensorFlow Placeholders

TensorFlow feed_dict example: Use feed_dict to feed values to TensorFlow placeholders so that you don't run into the error that says you must feed a value for placeholder tensors

TensorFlow feed_dict example: Use feed_dict to feed values to TensorFlow placeholders so that you don't run into the error that says you must feed a value for placeholder tensors

Video Transcript


We import TensorFlow as tf.

import tensorflow as tf


We then print out what TensorFlow version we are using.

print(tf.__version__)

We are using TensorFlow 1.0.1.


To understand how to use feed_dict to feed values to TensorFlow placeholders, we’re going to create an example of adding three TensorFlow placeholders together.


First, we define our first TensorFlow placeholders with the data type being tf.float32.

placeholder_ex_one = tf.placeholder(tf.float32)

We have tf.placeholder(tf.float32).

We assign it to the Python variable, placeholder_ex_one.


Next, we define our second TensorFlow placeholder with the same dtype as the first one and assign it to placeholder_ex_two.

placeholder_ex_two = tf.placeholder(tf.float32)


Lastly, we define our third TensorFlow placeholder to be the same as the other two.

placeholder_ex_tre = tf.placeholder(tf.float32)

We assign it to the Python variable, placeholder_ex_tre.


Let’s try printing the first placeholder to see what we get.

print(placeholder_ex_one)

We see that it is a TensorFlow tensor, the name is Placeholder:0, and the data type is float32.


Next, let’s define the addition using the tf.add_n operation because we’re adding more than two tensors at the same time.

placeholder_summation = tf.add_n([placeholder_ex_one, placeholder_ex_two, placeholder_ex_tre])

So we say tf.add_n.

We use a Python list to pass in our three placeholder variables – placeholder_ex_one, placeholder_ex_two, placeholder_ex_tre.

We assign that to the Python variable placeholder_summation.


Let’s try printing the addition operation to see what we get.

print(placeholder_summation)

So print(placeholder_summation).

We see that it is a tensor.

The name is AddN:0 and the data type is float32.


Now that we have created our TensorFlow placeholders in an addition operation, it’s time to run the computational graph.


We launch the graph in a session.

sess = tf.Session()


Then we initialize all the global variables in the graph.

sess.run(tf.global_variables_initializer())


Now that the variables have been initialized and the TensorFlow session has been created, let’s try printing the result of running our first placeholder variable.

print(sess.run(placeholder_ex_one))

So we do print(sess.run(placeholder_ex_one)).


Yikes!

We get an error that says, "You must feed a value for the placeholder tensor 'Placeholder' with dtype float."


While we’re at it, let’s also try printing our operation to see what happens.

print(sess.run(placeholder_summation))

So print(sess.run(placeholder_summation)).

We again get the same error, "You must feed a value for the placeholder tensor 'Placeholder' with dtype float."


So this is what we set out to do with this video – use feed_dict to feed values to the TensorFlow placeholders.

The way to feed the values into our tensors is to use a feed_dict that defines the values that we want.


To do this, we can write the following.

print(sess.run(placeholder_summation, feed_dict={placeholder_ex_one: 10,
                                                 placeholder_ex_two: 20,
                                                 placeholder_ex_tre: 30}))

Again, we’re using the print(sess.run(...)) and we’re going to use the placeholder_summation operation and we’re going to pass into it the feed_dict that says placeholder_ex_one is going to be the value of 10, placeholder_ex_two is going to be the value of 20, placeholder_ex_tre is going to be the value of 30.

When we run that, we see that we have the value of 60.0 because we have float32 numbers.


Finally, we close the TensorFlow session to release the TensorFlow resources we used within the session.

sess.close()


That is how you can use the feed_dict to feed values to TensorFlow placeholders.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.