![](/img/trans.png)
[英]Why is my input and output shape from keras conv2d the same dimensions?
[英]Why conv2d in tensorflow gives an output has the same shape as input
根據這個深度學習課程http://cs231n.github.io/convolutional-networks/#conv ,它說如果有一個輸入x
的形狀[W,W]
(其中W = width = height
)經過一個帶過濾器的形狀卷積層 [F,F]
和步幅 S
中,第二層將返回一個output
具有形狀[(WF)/S +1, (WF)/S +1]
但是,當我試圖遵循Tensorflow教程時: https ://www.tensorflow.org/versions/r0.11/tutorials/mnist/pros/index.html。 函數tf.nn.conv2d(inputs, filter, stride)
似乎有區別tf.nn.conv2d(inputs, filter, stride)
無論如何更改我的濾鏡大小, conv2d
將不斷返回一個與輸入形狀相同的值。
就我而言,我使用MNIST
數據集,表明每個圖像都有大小[28,28]
(忽略channel_num = 1
)
但是在我定義了第一個conv1
圖層后,我使用了conv1.get_shape()
來查看它的輸出,它給了我[28,28, num_of_filters]
為什么是這樣? 我認為返回值應該遵循上面的公式。
附錄:代碼段
#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有一個名為padding的參數, 請參見此處
如果您將填充設置為“有效”,它將滿足您的公式。 它默認為“SAME”,它填充零填充圖像(與添加邊框相同),使得輸出將保持與輸入相同的形狀。
它取決於填充參數。 'SAME'將輸出保持為WxW(假設stride = 1,)'VALID'將輸出的大小縮小為(W-F + 1)x(W-F + 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.