Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tensorflow placeholder - understanding `shape=[None,`

I'm trying to understand placeholders in tensorflow. Specifically what shape=[None, means in the example below.

X = tf.placeholder(tf.float32, shape=[None, 128, 128, 3], name="X")

This answer describes it as:

You can think of a placeholder in TensorFlow as an operation specifying the shape and type of data that will be fed into the graph.placeholder X defines that an unspecified number of rows of shape (128, 128, 3) of type float32 will be fed into the graph. a Placeholder does not hold state and merely defines the type and shape of the data to flow into the graph.

When it says "unspecified number of ROWS" does it really mean unspecified number of tensors of shape 128*128*3? Like you are creating a placeholder for input images for input images to a CNN?

like image 303
arcoxia tom Avatar asked Jul 16 '18 17:07

arcoxia tom


1 Answers

The first dimension represents the number of samples (images in your case). The reason why you do not want to hardcode a specific number there is to keep things flexible and allow for any number of samples. By putting None as the first dimension of the tensor you enable that. Consider the following 3 very common actions:

  1. Batch training: You are going to use batches of samples of relatively small length (32, 64, ...)
  2. Train evaluation: evaluation of perfomance over all training samples
  3. Test evaluation: evaluation performance over all testing samples

All of these will work with a different number of samples in general. However, you do not have to worry because the None got you covered.

like image 105
Jan K Avatar answered Sep 29 '22 16:09

Jan K