A placeholder is a tensor that is fed at runtime. Usually, placeholders are used as input for models. Every input passed to a computational graph at runtime is fed with feed_dict. feed_dict is an optional argument that allows the caller to override the value of tensors in the graph. In the following snippet, the a placeholder is overridden by [[0.1,0.2,0.3]]:
import tensorflow as tf
a = tf.placeholder(shape=(1,3), dtype=tf.float32)
b = tf.constant([[10,10,10]], dtype=tf.float32)
c = a + b
sess = tf.Session()
res = sess.run(c, feed_dict={a:[[0.1,0.2,0.3]]})
print(res)
>> [[10.1 10.2 10.3]]
If the size of the first dimension of the input is not known during the creation of the graph, TensorFlow can take care of it. Just set it to None:
import tensorflow as tf
import numpy as np
# NB: the first dimension is 'None', meaning that it can be of any length
a = tf.placeholder(shape=(None,3), dtype=tf.float32)
b = tf.placeholder(shape=(None,3), dtype=tf.float32)
c = a + b
print(a)
>> Tensor("Placeholder:0", shape=(?, 3), dtype=float32)
sess = tf.Session()
print(sess.run(c, feed_dict={a:[[0.1,0.2,0.3]], b:[[10,10,10]]}))
>> [[10.1 10.2 10.3]]
v_a = np.array([[1,2,3],[4,5,6]])
v_b = np.array([[6,5,4],[3,2,1]])
print(sess.run(c, feed_dict={a:v_a, b:v_b}))
>> [[7. 7. 7.]
[7. 7. 7.]]
This feature is useful when the number of training examples is not known initially.