池化层max_pool中两种paddding操作

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

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

这一解释除了tf.nn.max_pool,还适用于tf.nn.conv2d和tf.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.

#"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.

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|

å¨è¿éæå¥å¾çæè¿°

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
------------------------------------------ 
原文:https://blog.csdn.net/u014636245/article/details/83618447 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的内容,可以使用tooltip的formatter属性来自定义echarts叠加柱状图的tooltip样式。具体的设置如下: ```javascript tooltip: { formatter: (params) => { let str = ''; str = ` <div class="ehover" style="width:80px;background: #FFFFFF;font-family: Microsoft YaHei;"> <h3 style="height:14px;font-size:14px;font-weight:400;line-height:14px;color:#333333;"> ${params\[0\].name} </h3> <ul style="margin:0;paddding:0;list-style:none;color: #333333;font-size: 14px;"> `; for (let i = params.length - 1; i > 0; i--) { str += ` <li style="display:flex;justify-content:space-between;margin-top:6px;height: 15px;"> <p>${params\[i\].marker} ${params\[i\].seriesName}</p> <p style="font-weight: 400;">${params\[i\].value}</p> </li> `; } str += ` </ul> </div> `; return str; }, padding: 20, extraCssText: 'box-shadow:-2px 0px 9px 2px rgba(61,126,255,0.45)', color: '#333333', } ``` 以上代码,formatter函数用于自定义tooltip的内容和样式。通过遍历params数组,可以获取到每个series的名称、标记和数值,并将其以自定义的样式展示在tooltippadding属性用于设置tooltip的内边距,extraCssText属性用于设置tooltip的阴影效果,color属性用于设置文字颜色。 同时,根据引用\[3\]的内容,还可以使用trigger和axisPointer属性来设置tooltip的触发方式和指示器样式。例如,可以将trigger设置为'axis',axisPointer设置为'shadow',以实现在鼠标悬停在柱状图上时显示tooltip,并且tooltip的指示器为阴影效果。 此外,根据引用\[3\]的内容,还可以通过设置xAxis和yAxis属性来配置x轴和y轴的相关设置,通过设置series属性来配置柱状图的数据系列。 综上所述,以上代码提供了一种设置echarts叠加柱状图tooltip的方法。 #### 引用[.reference_title] - *1* *2* [echarts 堆叠柱状图 tooltip倒序展示数据](https://blog.csdn.net/m0_48571414/article/details/127734130)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [echarts 柱状图 tooltip 加单位](https://blog.csdn.net/qq_43780814/article/details/120762738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值