mindspore.mint.nn.functional.adaptive_avg_pool2d

View Source On AtomGit
mindspore.mint.nn.functional.adaptive_avg_pool2d(input, output_size)[source]

Performs 2D adaptive average pooling on a multi-plane input signal. That is, for any input size, the size of the specified output is H x W. The number of output features is equal to the number of input features.

The input and output data format can be "NCHW" and "CHW". N is the batch size, C is the number of channels, H is the feature height, and W is the feature width.

For adaptive average pooling for 2D:

\[\begin{split}\begin{align} h_{start} &= floor(i * H_{in} / H_{out})\\ h_{end} &= ceil((i + 1) * H_{in} / H_{out})\\ w_{start} &= floor(j * W_{in} / W_{out})\\ w_{end} &= ceil((j + 1) * W_{in} / W_{out})\\ Output(i,j) &= \frac{\sum Input[h_{start}:h_{end}, w_{start}:w_{end}]}{(h_{end}- h_{start}) * (w_{end}- w_{start})} \end{align}\end{split}\]
Parameters
  • input (Tensor) – The input of adaptive_avg_pool2d, which is a 3D or 4D tensor.

  • output_size (Union[int, tuple]) – The target output size. output_size can be a tuple \((H, W)\), or an int H for \((H, H)\). \(H\) and \(W\) can be int or None. If it is None, it means the output size is the same as the input size.

Returns

Tensor, with the same type as the input.

Shape of the output is input_shape[:len(input_shape) - len(out_shape)] + out_shape.

\[\begin{split}out\_shape = \begin{cases} input\_shape[-2] + output\_size[1], & \text{if } output\_size \text{ is (None, w);}\\ output\_size[0] + input\_shape[-1], & \text{if } output\_size \text{ is (h, None);}\\ input\_shape[-2:], & \text{if } output\_size \text{ is (None, None);}\\ (h, h), & \text{if } output\_size \text{ is h;}\\ (h, w), & \text{if } output\_size \text{ is (h, w)} \end{cases}\end{split}\]
Raises
  • ValueError – If output_size is a tuple and the length of output_size is not 2.

  • ValueError – If the dimension of input is less than or equal to the dimension of output_size.

  • TypeError – If input is not a Tensor.

Supported Platforms:

Ascend

Examples

>>> import mindspore
>>> import numpy as np
>>> # case 1: output_size=(3, 2)
>>> input = mindspore.Tensor(np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],
...                            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]],
...                            [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]), mindspore.float32)
>>> output = mindspore.mint.nn.functional.adaptive_avg_pool2d(input, (3, 2))
>>> print(output)
[[[1.5 2.5]
 [4.5 5.5]
 [7.5 8.5]]
[[1.5 2.5]
 [4.5 5.5]
 [7.5 8.5]]
[[1.5 2.5]
 [4.5 5.5]
 [7.5 8.5]]]