简体   繁体   English

为什么convorflow中的conv2d给出的输出具有与输入相同的形状

[英]Why conv2d in tensorflow gives an output has the same shape as input

According to this Deep Learning course http://cs231n.github.io/convolutional-networks/#conv , It says that if there is an input x with shape [W,W] (where W = width = height ) goes through a Convolutional Layer with filter shape [F,F] and stride S , the Layer will return an output with shape [(WF)/S +1, (WF)/S +1] 根据这个深度学习课程http://cs231n.github.io/convolutional-networks/#conv ,它说如果有一个输入x的形状[W,W] (其中W = width = height )经过一个带过滤器的形状卷积层 [F,F]步幅 S中,第二将返回一个output具有形状[(WF)/S +1, (WF)/S +1]

However, when I'm trying to follow the tutorial of the Tensorflow: https://www.tensorflow.org/versions/r0.11/tutorials/mnist/pros/index.html . 但是,当我试图遵循Tensorflow教程时: https ://www.tensorflow.org/versions/r0.11/tutorials/mnist/pros/index.html。 There seems to have difference of the function tf.nn.conv2d(inputs, filter, stride) 函数tf.nn.conv2d(inputs, filter, stride)似乎有区别tf.nn.conv2d(inputs, filter, stride)

Whatever how do I change my filter size, conv2d will constantly return me a value with the same shape as the input. 无论如何更改我的滤镜大小, conv2d将不断返回一个与输入形状相同的值。

In my case, I am using the MNIST dataset which indicates that every image has size [28,28] (ignoring channel_num = 1 ) 就我而言,我使用MNIST数据集,表明每个图像都有大小[28,28] (忽略channel_num = 1

but after I defining the first conv1 layers, I used the conv1.get_shape() to see its output, it gives me [28,28, num_of_filters] 但是在我定义了第一个conv1图层后,我使用了conv1.get_shape()来查看它的输出,它给了我[28,28, num_of_filters]

Why is this? 为什么是这样? I thought the return value should follow the formula above. 我认为返回值应该遵循上面的公式。


Appendix: Code snippet 附录:代码段

#reshape x from 2d to 4d

x_image = tf.reshape(x, [-1, 28, 28, 1]) #[num_samples, width, height, channel_num]

## define the shape of weights and bias
w_shape = [5, 5, 1, 32] #patch_w, patch_h, in_channel, output_num(out_channel)
b_shape =          [32] #bias only need to be consistent with output_num

## init weights of conv1 layers
W_conv1 = weight_variable(w_shape)
b_conv1 = bias_variable(b_shape)

## first layer x_iamge->conv1/relu->pool1

#Our convolutions uses a stride of one 
#and are zero padded 
#so that the output is the same size as the input
h_conv1 = tf.nn.relu(
    conv2d(x_image, W_conv1) + b_conv1
                    )

print 'conv1.shape=',h_conv1.get_shape() 
## conv1.shape= (?, 28, 28, 32) 
## I thought conv1.shape should be (?, (28-5)/1+1, 24 ,32)

h_pool1 = max_pool_2x2(h_conv1) #output 32 num
print 'pool1.shape=',h_pool1.get_shape() ## pool1.shape= (?, 14, 14, 32)

Conv2d has a parameter called padding see here Conv2d有一个名为padding的参数, 请参见此处

Where if you set padding to "VALID" it will satisfy your formula. 如果您将填充设置为“有效”,它将满足您的公式。 It defaults to "SAME" which pads (same as adding a border around) the image filled with zeroes such that the output will remain the same shape as the input. 它默认为“SAME”,它填充零填充图像(与添加边框相同),使得输出将保持与输入相同的形状。

It depends on the padding parameter. 它取决于填充参数。 'SAME' will keep the output as WxW (assuming stride=1,) 'VALID' will shrink the size of the output to (W-F+1)x(W-F+1) 'SAME'将输出保持为WxW(假设stride = 1,)'VALID'将输出的大小缩小为(W-F + 1)x(W-F + 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的输入和来自 keras conv2d 的 output 形状尺寸相同? - Why is my input and output shape from keras conv2d the same dimensions? Tensorflow/Keras Conv2D 层中的输入形状错误 - Input shape error in Tensorflow/Keras Conv2D layer TensorFlow conv2d数据集形状不被接受 - TensorFlow conv2d dataset shape not accepted Keras Conv2D 输入形状 - Keras Conv2D input Shape 为什么只为第一个 Conv2D 层指定 Conv2D 层的 input_shape 属性? - Why should the input_shape property of a Conv2D layer be specified only for the first Conv2D layer? Tensorflow Conv2D 层 input_shape 配置错误:ValueError: Input 0 of layer "sequential" is in compatible with the layer: - Tensorflow Conv2D layers input_shape configuration error: ValueError: Input 0 of layer "sequential" is incompatible with the layer: 了解 Conv2d 的输入和 output 大小 - Understanding input and output size for Conv2d Tensorflow - 深度到空间后与 conv2d 不兼容的形状 - Tensorflow - incompatible shape with conv2d after depth to space 为什么在 tensorflow 中,Conv2D 的输入通道限制为 1,3,4,张量维度限制为 4-D? - Why are input channels for Conv2D limited to 1,3,4 with the tensor dimension limited to 4-D in tensorflow? Keras:为什么 Conv2D 层的 output 大小与特征 map 的预期形状不匹配? - Keras: Why the output size of a Conv2D layer doesn't match the expected shape of feature map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM