原文地址:
https://blog.csdn.net/jasonzzj/article/details/53930074
---------------------------------------------------------------------------------------
SAME means that the output feature map has the same spatial dimensions as the input feature map. Zero padding is introduced to make the shapes match as needed, equally on every side of the input map.
VALID means no padding.
Padding could be used in convolution and pooling operations.
Here, take pooling for example:
If you like ascii art:
In this example:
Notes:
|
"VALID"
only ever drops the right-most columns (or bottom-most rows).
"SAME"
tries to pad evenly left and right, but if the amount of columns to be added is odd, it will add the extra column to the right, as is the case in this example (the same logic applies vertically: there may be an extra row of zeros at the bottom).
不同的padding方式,VALID是采用丢弃的方式,比如上述的input_width=
13
,只允许滑动
2
次,多余的元素全部丢掉。
SAME的方式,采用的是补全的方式,对于上述的情况,允许滑动
3
次,但是需要补
3
个元素,左奇右偶,在左边补一个
0
,右边补
2
个
0 。
The TensorFlow Convolution example gives an overview about the difference between SAME
and VALID
:
For the
SAME
padding, the output height and width are computed as:out_height = ceil(float(in_height) / float(strides[1]))
out_width = ceil(float(in_width) / float(strides[2]))
And
For the
VALID
padding, the output height and width are computed as:out_height = ceil(float(in_height - filter_height + 1) / float(strides[1]))
out_width = ceil(float(in_width - filter_width + 1) / float(strides[2]))
备注
#SAME 向上取整
#VALID 向下取整
x = tf.constant([[
1
.,
2
.,
3
.],
[
4
.,
5
.,
6
.]])
x = tf.reshape(x, [
1
,
2
,
3
,
1
]) # give a shape accepted by tf.nn.max_pool
valid_pad = tf.nn.max_pool(x, [
1
,
2
,
2
,
1
], [
1
,
2
,
2
,
1
], padding=
'VALID'
)
same_pad = tf.nn.max_pool(x, [
1
,
2
,
2
,
1
], [
1
,
2
,
2
,
1
], padding=
'SAME'
)
valid_pad.get_shape() == [
1
,
1
,
1
,
1
] # valid_pad is [
5
.]
same_pad.get_shape() == [
1
,
1
,
2
,
1
] # same_pad is [
5
.,
6
.]
----------------------------------------------------------------------------------------------------------------------------------
参考地址:
https://www.2cto.com/kf/201708/673033.html
https://www.tensorflow.org/versions/r0.9/api_docs/python/nn.html