tf.placeholder - Create A TensorFlow Placeholder Tensor and then when it needs to be evaluated pass a NumPy multi-dimensional array into the feed_dict so that the values are used within the TensorFlow session

tf.placeholder - Create A TensorFlow Placeholder Tensor and then when it needs to be evaluated pass a NumPy multi-dimensional array into the feed_dict so that the values are used within the TensorFlow session

We import TensorFlow as tf.

```
import tensorflow as tf
```

Then we print out what version of TensorFlow we are using. We are using 1.0.1.

```
print(tf.__version__)
```

Next, let’s import NumPy as np

```
import numpy as np
```

And then print out what version of NumPy we are using.

```
print(np.__version__)
```

We are using NumPy 1.13.3.

In this video, we are going to create a placeholder tensor using the TensorFlow placeholder operation.

No data will be provided for the tensor until the execution within a TensorFlow session.

At that point, we will use a feed_dict argument which will provide the data that will fill the placeholder tensor.

All right, let’s get started.

In order to do that, let’s start by defining a TensorFlow placeholder tensor that will eventually hold data of data type 32-bit signed integers and have a shape of 1x2x3.

```
placeholder_ex = tf.placeholder(tf.int32, shape=(1, 2, 3))
```

We create this placeholder tensor using the TensorFlow placeholder operation and assign it to the Python variable placeholder_ex.

So you see tf.placeholder.

We give it the data type tf.int32, and we give it a shape of 1x2x3, and we assign it to the Python variable placeholder_ex.

When we print out what our placeholder_ex Python variable has

```
print(placeholder_ex)
```

We see that it’s a tensor.

It’s a placeholder.

The shape is 1x2x3.

And the data type is int32.

Let’s also define a TensorFlow addition operation where we add this placeholder tensor to itself.

```
placeholder_sum_ex = tf.add(placeholder_ex, placeholder_ex)
```

We create the addition using the tf.add operation and assign it to the Python variable placeholder_sum_ex.

So we use tf.add(placeholder_ex, placeholder_ex) and assign it to the Python variable placeholder_sum_ex.

Let’s print out our placeholder_sum_ex Python variable to see what’s in it.

```
print(placeholder_sum_ex)
```

We see that the operation is add, the shape is 1x2x3, and the data type is int32.

Because TensorFlow’s add operation does element-wise addition of tensors, this makes sense.

Now that we’ve created our TensorFlow variable, it’s time to run the computational graph.

We launch the graph in the session.

```
sess = tf.Session()
```

Then we initialize all the global variables in the graph.

```
sess.run(tf.global_variables_initializer())
```

Now that we’ve done that, let’s print our placeholder_ex tensor in the TensorFlow session to see the values.

```
print(sess.run(placeholder_ex))
```

Yikes!

We get an error that says, “You must feed a value for placeholder tensor ‘Placeholder’ with dtype int32 and shape [1,2,3].”

A TensorFlow placeholder tensor is just that, a placeholder, so printing it doesn’t make any sense and that’s why we get an error.

Let’s also check out the placeholder_sum_ex tensor in a TensorFlow session to see the value.

```
print(sess.run(placeholder_sum_ex))
```

We get the same thing.

We get an error that says, “You must feed a value for placeholder tensor ‘Placeholder’.”

This is expected because placeholder_sum_ex is just the sum of the placeholder tensor with itself.

So having it not have any values when we evaluate it, adding it to itself would definitely still not have any values.

So to properly use the placeholder tensor, we have to pass a feed_dict to it.

Since we know the placeholder is an int32 tensor expecting a 1x2x3 shape, we can create a NumPy multidimensional array to feed into it.

```
mda_ex = np.ones((1,2,3), dtype="int32")
```

To do that, we’re going to create a 1x2x3 NumPy array filled with 1s.

So we’re using np.ones and we’re going to give it the data type int32, and we’re going to assign it to the Python variable mda_ex.

```
mda_ex
```

Now that we’ve done that, let’s print the evaluation of the placeholder_sum_ex when we use this mda_ex array in the feed_dict.

```
print(sess.run(placeholder_sum_ex, feed_dict={placeholder_ex: mda_ex}))
```

So we see print(sess.run(placeholder_sum_ex).

For the feed_dict, we’re going to pass in the placeholder_ex.

We want to use this NumPy multidimensional array.

When we do that, we see that the result is a 1x2x3 tensor whose value is all 2s, which is what we expect because the placeholder tensor was full of 1s, so when we added it to itself, 1+1 is 2, and because it’s element-wise addition, all the element places are the integers 2.

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

```
sess.close()
```

That is how you create a TensorFlow placeholder tensor and then when it needs to be evaluated, pass a NumPy multidimensional array into the feed_dict so that the values are used within the TensorFlow session to fill out the TensorFlow placeholder tensor.

Receive the Data Science Weekly Newsletter every Thursday

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