【TensorFlow】池化层max_pool中两种paddding操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014636245/article/details/83618447

max_pool()padding参数有两种模式validsame模式。
Tensorflow的padding和卷积层一样也有padding操作,两种不同的操作输出的结果有区别:

函数原型max_pool(value, ksize, strides, padding, data_format="NHWC", name=None)

这一解释除了tf.nn.max_pool,还适用于tf.nn.conv2dtf.layer.*下的相关函数

if padding = "SAME": 
    output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i])
if padding = "VALID": 
    output_spatial_shape[i] = ceil((input_spatial_shape[i] - (window_shape[i] - 1) * dilation_rate[i]) / strides[i]).

也就是说

“VALID” 模式,在剩余行列数小于池化窗口大小时,将最右边和最下面的列或行抛弃,只保留有效值;
“SAME” 模式则是在剩余行列数不足时补充0来满足池化窗口的大小,保持窗口被池化区域相同;
所以输出尺寸不是池化窗口的整数倍时,same模式的输出要比valid的大。

#我们来看看函数的定义
#source code:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/nn_ops.py  line:2116
@tf_export("nn.max_pool")
def max_pool(value, ksize, strides, padding, data_format="NHWC", name=None):
  """Performs the max pooling on the input.
  Args:
    value: A 4-D `Tensor` of the format specified by `data_format`.
    ksize: A list or tuple of 4 ints. The size of the window for each dimension
      of the input tensor.
    strides: A list or tuple of 4 ints. The stride of the sliding window for
      each dimension of the input tensor.
    padding: A string, either `'VALID'` or `'SAME'`. The padding algorithm.
      See the "returns" section of `tf.nn.convolution` for details.
    data_format: A string. 'NHWC', 'NCHW' and 'NCHW_VECT_C' are supported.
    name: Optional name for the operation.
  Returns:
    A `Tensor` of format specified by `data_format`.
    The max pooled output tensor.
  """
  with ops.name_scope(name, "MaxPool", [value]) as name:
    value = ops.convert_to_tensor(value, name="input")
    return gen_nn_ops.max_pool(
        value,
        ksize=ksize,
        strides=strides,
        padding=padding,
        data_format=data_format,
        name=name)

#其中pool()函数 padding的代码如下所示
#dilation_rate 默认为1的序列Defaults to [1]*N
#nn_ops.py line876
    If padding = "SAME":
      output_spatial_shape[i] = ceil(input_spatial_shape[i] / strides[i])
    #'same'方法输出尺寸直接为 input_size/strides_size
    
    If padding = "VALID":
      output_spatial_shape[i] =
        ceil((input_spatial_shape[i] - (window_shape[i] - 1) * dilation_rate[i])
             / strides[i]).
	#'valid'方法输出尺寸为 [input_size-(pooling_win_size-1)*1]/stride_size

用例子解释一下:
1.@MiniQuark

#"VALID" = without padding:
   inputs:         1  2  3  4  5  6  7  8  9  10 11 (12 13)
                  |________________|                dropped
                                 |_________________|
#"SAME" = with zero padding:
               pad|                                      |pad
   inputs:      0 |1  2  3  4  5  6  7  8  9  10 11 12 13|0  0
               |________________|
                              |_________________|
                                             |________________|

2.@Olivier Moindrot

x = tf.constant([[1., 2., 3.],
                 [4., 5., 6.]])

x = tf.reshape(x, [1, 2, 3, 1])  				#先转为张量输入

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.]       #3,6那一列被丢掉了
|1 2    ...3|
|4 5    ...6|

在这里插入图片描述

same_pad.get_shape() == [1, 1, 2, 1]   # same_pad is  [5., 6.]      #加上了第四列,第二个窗口的max是6
|1 2 3 0|
|4 5 6 0|     ->|5,6|

在这里插入图片描述

在这里插入图片描述
icon from easyicon


ref:
api:https://www.tensorflow.org/api_docs/python/tf/nn/max_pool
blog:https://blog.csdn.net/accumulate_zhang/article/details/78359856
blog:https://blog.csdn.net/wuzqChom/article/details/74785643
stackflow:https://stackoverflow.com/questions/37674306/what-is-the-difference-between-same-and-valid-padding-in-tf-nn-max-pool-of-t
code:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/nn_ops.py
gen_nn_ops.max_pool如何得到:
build:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/BUILD#L273
gen_nn.ops:https://stackoverflow.com/questions/41147734/looking-for-source-code-of-from-gen-nn-ops-in-tensorflow

猜你喜欢

转载自blog.csdn.net/u014636245/article/details/83618447
今日推荐