arcoxia tom
arcoxia tom

Reputation: 1711

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?

Upvotes: 17

Views: 19624

Answers (2)

kebenny
kebenny

Reputation: 51

Referring to the official document of tf.TensorShape

"none" means:

Partially-known shape: has a known number of dimensions, and an unknown size for one or more dimension. e.g. TensorShape([None, 256])

https://www.tensorflow.org/api_docs/python/tf/TensorShape#used-in-the-notebooks

Upvotes: 0

Jan K
Jan K

Reputation: 4150

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.

Upvotes: 19

Related Questions