【转载】 Tensorflow中padding的两种类型SAME和VALID

原文地址:

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:

down vote

If you like ascii art:

  • "VALID" = without padding:

    1. inputs: 1 2 3 4 5 6 7 8 9 10 11 (12 13)
    2. |________________| dropped
    3. |_________________|
  • "SAME" = with zero padding:

    1. pad| |pad
    2. inputs : 0 |1 2 3 4 5 6 7 8 9 10 11 12 13|0 0
    3. |________________|
    4. |_________________|
    5. |________________|

In this example:

  • Input width = 13
  • Filter width = 6
  • Stride = 5

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

"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

猜你喜欢

转载自www.cnblogs.com/devilmaycry812839668/p/10930405.html