TensorFlow - How to Use Placeholders
In this article, we will learn about Placeholders in TensorFlow, how to initialize them and how to pass the data to them.
While Constants are used to feed data from inside the model, Placeholders are used to feed data from outside the model. In simpler terms, placeholders doesn't provide values while initializing, they are passed during the session.
First, Initialize two placeholders.
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
Take a look at the initialization, we aren't passing any value to the tensor. Next, let's add them.
adder_node = tf.add(a, b)
Now, Inside a session we can pass any number of inputs and the model performs operations and gives a value.
# Add two numbers
sess.run(adder_node, {a: 1.3, b: 5})
# Add two 1x2 matrices
sess.run(adder_node, {a: [2, 8], b: [3, 5]})
Below is the code that initializes tensors with placeholders and passes the values at a later stage. Play with it for better understanding.