Placeholders and feed dictionaries

We can think of placeholders as variables, where we only define the type and dimension, but do not assign the value. Values for the placeholders will be fed at runtime. We feed the data to the computational graphs using placeholders. Placeholders are defined with no values.

A placeholder can be defined using tf.placeholder(). It takes an optional argument called shape, which denotes the dimensions of the data. If shape is set to None, then we can feed data of any size at runtime. A placeholder can be defined as follows:

 x = tf.placeholder("float", shape=None)
To put it in simple terms, we use tf.Variable to store the data and tf.placeholder for feeding the external data.

Let's consider a simple example to better understand placeholders:

x = tf.placeholder("float", None)
y = x +3

with tf.Session() as sess:
result = sess.run(y)
print(result)

If we run the preceding code, then it will return an error because we are trying to compute y, where y= x+3 and x is a placeholder whose value is not assigned. As we have learned, values for the placeholders will be assigned at runtime. We assign the values of the placeholder using the feed_dict parameter. The feed_dict parameter is basically the dictionary where the key represents the name of the placeholder, and the value represents the value of the placeholder.

As you can see in the following code, we set feed_dict = {x:5}, which implies that the value for the x placeholder is 5:

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

The preceding code returns 8.0.

What if we want to use multiple values for x? As we have not defined any shapes for the placeholders, it takes any number of values, as shown in the following code:

with tf.Session() as sess:
result = sess.run(y, feed_dict={x: [3,6,9]})
print(result)

It will return the following:

[ 6.  9. 12.]

Let's say we define the shape of x as [None,2], as shown in the following code:

x = tf.placeholder("float", [None, 2])

This means that x can take a matrix of any rows but with 2 columns, as shown in the following code:

with tf.Session() as sess:
x_val = [[1, 2,],
[3,4],
[5,6],
[7,8],]
result = sess.run(y, feed_dict={x: x_val})
print(result)

The preceding code returns the following:

[[ 4.  5.]
 [ 6.  7.]
 [ 8.  9.]
 [10. 11.]]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset