为什么张量流的 MNIST 教程中 x 变量张量被重塑为 -1?
Why is the x variable tensor reshaped with -1 in the MNIST tutorial for tensorflow?
我正在学习 TensorFlow 教程
最初x
定义为
x = tf.placeholder(tf.float32, shape=[None, 784])
稍后它重塑 x
,我正在尝试理解为什么。
To apply the layer, we first reshape x to a 4d tensor, with the second and third dimensions corresponding to image width and height, and the final dimension corresponding to the number of color channels.
x_image = tf.reshape(x, [-1,28,28,1])
重塑向量中的 -1 是什么意思,为什么要重塑 x
?
1) 整形向量中的-1是什么意思
来自 reshape 的文档:
If one component of shape is the special value -1, the size of that
dimension is computed so that the total size remains constant. In
particular, a shape of [-1] flattens into 1-D. At most one component
of shape can be -1.
这是一项标准功能,在 numpy 中也可用。基本上这意味着 - 我没有时间计算所有维度,所以为我推断一个。在你的情况下,因为 x * 28 * 28 * 1 = 784
所以你的 -1 = 1
2) 为什么要重塑 x
他们计划使用卷积进行图像分类。所以他们需要使用一些空间信息。当前数据是一维的。所以他们将其转换为 4 个维度。我不知道第四维的意义,因为在我看来他们可能只使用了(x,y,颜色)。甚至 (x, y)。尝试修改它们的重塑和卷积,很可能你会得到类似的精度。
TensorFlow 的卷积 conv2d 操作需要一个 4 维张量,其维度对应于 batch、width、height 和 channel。
[batch, in_height, in_width, in_channels]
我正在学习 TensorFlow 教程
最初x
定义为
x = tf.placeholder(tf.float32, shape=[None, 784])
稍后它重塑 x
,我正在尝试理解为什么。
To apply the layer, we first reshape x to a 4d tensor, with the second and third dimensions corresponding to image width and height, and the final dimension corresponding to the number of color channels.
x_image = tf.reshape(x, [-1,28,28,1])
重塑向量中的 -1 是什么意思,为什么要重塑 x
?
1) 整形向量中的-1是什么意思
来自 reshape 的文档:
If one component of shape is the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, a shape of [-1] flattens into 1-D. At most one component of shape can be -1.
这是一项标准功能,在 numpy 中也可用。基本上这意味着 - 我没有时间计算所有维度,所以为我推断一个。在你的情况下,因为 x * 28 * 28 * 1 = 784
所以你的 -1 = 1
2) 为什么要重塑 x
他们计划使用卷积进行图像分类。所以他们需要使用一些空间信息。当前数据是一维的。所以他们将其转换为 4 个维度。我不知道第四维的意义,因为在我看来他们可能只使用了(x,y,颜色)。甚至 (x, y)。尝试修改它们的重塑和卷积,很可能你会得到类似的精度。
TensorFlow 的卷积 conv2d 操作需要一个 4 维张量,其维度对应于 batch、width、height 和 channel。
[batch, in_height, in_width, in_channels]