tensorflow placeholder - understanding `shape=[None,`

我怕爱的太早我们不能终老 提交于 2021-01-19 16:53:37

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/51366871/tensorflow-placeholder-understanding-shape-none

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!