【问题标题】:Why conv2d in tensorflow gives an output has the same shape as input为什么张量流中的conv2d给出的输出与输入具有相同的形状
【发布时间】:2017-03-10 15:25:05
【问题描述】:

根据这门深度学习课程http://cs231n.github.io/convolutional-networks/#conv,它说如果有一个形状为[W,W](其中W = width = height)的输入x通过一个卷积层filter 形状 [F,F]stride SLayer 将返回一个 output 形状 [(W-F)/S +1, (W-F)/S +1]

但是,当我尝试遵循 Tensorflow 的教程时:https://www.tensorflow.org/versions/r0.11/tutorials/mnist/pros/index.htmltf.nn.conv2d(inputs, filter, stride)的功能似乎有区别

无论我如何更改过滤器大小,conv2d 都会不断地返回一个与输入形状相同的值。

就我而言,我使用的是MNIST 数据集,它表明每个图像的大小为[28,28](忽略channel_num = 1

但是在我定义了第一个conv1 层之后,我使用conv1.get_shape() 查看它的输出,它给了我[28,28, num_of_filters]

这是为什么?我认为返回值应该遵循上面的公式。


附录:代码sn-p

#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)

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    这取决于填充参数。 'SAME' 将输出保持为 WxW(假设 stride=1,)'VALID' 会将输出的大小缩小到 (W-F+1)x(W-F+1)

    【讨论】:

    • 您好,非常感谢,您的回答对我很有帮助。
    【解决方案2】:

    Conv2d 有一个参数叫 padding see here

    如果您将填充设置为“有效”,它将满足您的公式。它默认为“SAME”,它用零填充图像(与添加边框相同),这样输出将保持与输入相同的形状。

    【讨论】:

    • 您的意思是“有效”填充算法将添加一些零填充作为补充以使输出保持相同的形状?
    • 如在其他答案中所见,“VALID”不会对图像进行零填充,因此会降低输出维度。
    猜你喜欢
    • 2019-10-29
    • 1970-01-01
    • 2019-02-28
    • 2019-11-30
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2020-12-10
    • 2019-04-08
    相关资源
    最近更新 更多