class AvgPool2d(kernel_size, stride=None, padding=0, mode='average_count_exclude_padding', **kwargs)[源代码]


For instance, given an input of the size \((N, C, H_{\text{in}}, W_{\text{in}})\) and kernel_size \((kH, kW)\), this layer generates the output of the size \((N, C, H_{\text{out}}, W_{\text{out}})\) through a process described as:

\[out(N_i, C_j, h, w) = \frac{1}{kH * kW} \sum_{m=0}^{kH-1} \sum_{n=0}^{kW-1} input(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)\]

padding 非零, 则输入数据会被隐式地在两边用零值进行填充(pad) padding 个点。

  • kernel_size (Union[int, Tuple[int, int]]) – 窗的大小。

  • stride (Union[int, Tuple[int, int], None]) – the stride of the window. Default value is kernel_size.

  • padding (Union[int, Tuple[int, int]]) – implicit zero padding to be added on both sides.Default: 0.

  • mode (str) – whether to include the padding values while calculating the average, set to “average” will do counting. Default: “average_count_exclude_padding”


>>> import numpy as np
>>> m = M.AvgPool2d(kernel_size=2, stride=2, padding=[1,0], mode="average")
>>> inp = mge.tensor(np.arange(1 * 1 * 3 * 4).astype(np.float32).reshape(1, 1, 3, 4))
>>> output = m(inp)
>>> output
Tensor([[[[0.25 1.25]
          [6.5  8.5 ]]]], device=xpux:0)