adaptive_avg_pool2d

paddle.nn.functional. adaptive_avg_pool2d ( x: Tensor, output_size: Size2, data_format: DataLayout2D = 'NCHW', name: str | None = None ) Tensor [source]

Applies 2D adaptive avg pooling on input tensor. The h and w dimensions of the output tensor are determined by the parameter output_size.

For avg adaptive pool2d:

\[\begin{split}hstart &= floor(i * H_{in} / H_{out}) \\ hend &= ceil((i + 1) * H_{in} / H_{out}) \\ wstart &= floor(j * W_{in} / W_{out}) \\ wend &= ceil((j + 1) * W_{in} / W_{out}) \\ Output(i ,j) &= \frac{\sum Input[hstart:hend, wstart:wend]}{(hend - hstart) * (wend - wstart)}\end{split}\]
Parameters
  • x (Tensor) – The input tensor of adaptive avg pool2d operator, which is a 4-D tensor. The data type can be float32 or float64.

  • output_size (int|list|tuple) – The pool kernel size. If pool kernel size is a tuple or list, it must contain two element, (H, W). H and W can be either a int, or None which means the size will be the same as that of the input.

  • data_format (str, optional) – The data format of the input and output data. An optional string from: “NCHW”, “NHWC”. The default is “NCHW”. When it is “NCHW”, the data is stored in the order of: [batch_size, input_channels, input_height, input_width].

  • name (str|None, optional) – For detailed information, please refer to api_guide_Name. Usually name is no need to set and None by default.

Returns

Tensor, The output tensor of avg adaptive pool2d result. The data type is same as input tensor.

Examples

>>> # adaptive avg pool2d
>>> # suppose input data in shape of [N, C, H, W], `output_size` is [m, n],
>>> # output shape is [N, C, m, n], adaptive pool divide H and W dimensions
>>> # of input data into m * n grids averagely and performs poolings in each
>>> # grid to get output.
>>> # adaptive avg pool performs calculations as follow:
>>> #
>>> #     for i in range(m):
>>> #         for j in range(n):
>>> #             hstart = floor(i * H / m)
>>> #             hend = ceil((i + 1) * H / m)
>>> #             wstart = floor(i * W / n)
>>> #             wend = ceil((i + 1) * W / n)
>>> #             output[:, :, i, j] = avg(input[:, :, hstart: hend, wstart: wend])
>>> #
>>> import paddle

>>> x = paddle.rand([2, 3, 32, 32])
>>> # x.shape is [2, 3, 32, 32]
>>> out = paddle.nn.functional.adaptive_avg_pool2d(x = x,
...                                                output_size=[3, 3])
>>> print(out.shape)
[2, 3, 3, 3]