mindspore.ops.avg_pool2d
- mindspore.ops.avg_pool2d(input_x, kernel_size=1, stride=1, padding=0, ceil_mode=False, count_include_pad=True, divisor_override=0)[source]
Applies a 2D average pooling over an input Tensor which can be regarded as a composition of 2D input planes. Typically the input is of shape \((N_{in}, C_{in}, H_{in}, W_{in})\), outputs regional average in the \((H_{in}, W_{in})\)-dimension. Given kernel size \((k_{h}, k_{w})\) and strides , the operation is as follows.
\[\text{output}(N_i, C_j, h, w) = \frac{1}{k_{h} * k_{w}} \sum_{m=0}^{k_{h}-1} \sum_{n=0}^{k_{w}-1} \text{input}(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n)\]Warning
kernel_size is in the range [1, 255]. stride is in the range [1, 63].
- Parameters
input_x (Tensor) – Tensor of shape \((N, C_{in}, H_{in}, W_{in})\).
kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the average value. It is an int number that represents height and width of the kernel, or a tuple of two int numbers that represent height and width respectively. Default:
1
.stride (Union[int, tuple[int]]) – The distance of kernel moving, an int number that represents the height and width of movement are both strides, or a tuple of two int numbers that represent height and width of movement respectively. Default:
1
.padding (Union(int, tuple[int])) – The pad value to be filled. Default: 0. If padding is an integer, the paddings of top, bottom, left and right are the same, equal to pad. If padding is a tuple of 4 integers, the padding of top, bottom, left and right equal to padding[0], padding[1], padding[2] and padding[3] correspondingly. Default:
0
.ceil_mode (bool) – If True, apply ceil instead of floor to compute the output shape. Default:
False
.count_include_pad (bool) – If True, include the zero-padding in the averaging calculation. Default:
True
.divisor_override (int) – If specified, it will be used as divisor in the averaging calculation, otherwise kernel_size will be used. Default:
0
.
- Returns
Tensor, with shape \((N, C_{out}, H_{out}, W_{out})\).
- Raises
TypeError – If input_x is not an Tensor.
TypeError – If kernel_size or stride is neither int nor tuple.
TypeError – If ceil_mode or count_include_pad is not a bool.
TypeError – If divisor_override is not an int.
ValueError – If length of shape of input_x is not equal to 4.
ValueError – If kernel_size or stride is less than 1.
ValueError – If kernel_size or stride is a tuple whose length is not equal to 2.
ValueError – If padding is not int nor a tuple whose length is equal to 4.
ValueError – If value(s) of padding is less than 0.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor, ops >>> x = Tensor(np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4), mindspore.float32) >>> output = ops.avg_pool2d(x, kernel_size=2, stride=1) >>> print(output) [[[[ 2.5 3.5 4.5] [ 6.5 7.5 8.5]] [[14.5 15.5 16.5] [18.5 19.5 20.5]] [[26.5 27.5 28.5] [30.5 31.5 32.5]]]]