Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.nn.AvgPool2d

class mindspore.nn.AvgPool2d(kernel_size=1, stride=1, pad_mode='valid', padding=0, ceil_mode=False, count_include_pad=True, divisor_override=None, data_format='NCHW')[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 (Nin,Cin,Hin,Win), AvgPool2d outputs regional average in the (Hin,Win)-dimension. Given kernel size ks=(hker,wker) and stride s=(s0,s1), the operation is as follows:

output(Ni,Cj,h,w)=1hkerwkerm=0hker1n=0wker1input(Ni,Cj,s0×h+m,s1×w+n)

Note

This interface currently does not support Atlas A2 training series products.

Parameters
  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the average value. The data type of kernel_size must be int or a single element tuple and the value represents the height and width, 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 or a single element tuple 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 .

  • pad_mode (str, optional) –

    Specifies the padding mode with a padding value of 0. It can be set to: "same" , "valid" or "pad" . Default: "valid" .

    • "same": Pad the input around its edges so that the shape of input and output are the same when stride is set to 1. The amount of padding to is calculated by the operator internally, If the amount is even, it is uniformly distributed around the input, if it is odd, the excess amount goes to the right/bottom side. If this mode is set, padding must be 0.

    • "valid": No padding is applied to the input, and the output returns the maximum possible height and width. Extra pixels that could not complete a full stride will be discarded. If this mode is set, padding must be 0.

    • "pad": Pad the input with a specified amount. In this mode, the amount of padding in the height and width directions is determined by the padding parameter. If this mode is set, padding must be greater than or equal to 0.

  • padding (Union(int, tuple[int], list[int])) – Pooling padding value, only "pad" mode can be set to non-zero. Default: 0 . padding can only be an integer or a tuple/list containing one or two integers. If padding is an integer or a tuple/list containing one integer, it will be padded padding times in the four directions of the input. If padding is a tuple/list containing two integers, it will be padded padding[0] times in the up-down direction of the input and padding[1] times in the left-right direction of the input.

  • ceil_mode (bool) – If True , use ceil to compute the output shape instead of floor. Default: False .

  • count_include_pad (bool) – If True , averaging calculation will include the zero-padding. Default: True .

  • divisor_override (int) – If it is specified as a non-zero parameter, this parameter will be used as the divisor in the average calculation. Otherwise, kernel_size will be used as the divisor. Default: None .

  • data_format (str) – The optional value for data format, is 'NHWC' or 'NCHW' . Default: 'NCHW' .

Inputs:
  • x (Tensor) - Tensor of shape (N,Cin,Hin,Win) or (Cin,Hin,Win).

Outputs:

Tensor of shape (N,Cout,Hout,Wout) or (Cout,Hout,Wout).

If pad_mode is in pad mode, the output shape calculation formula is as follows:

Hout=Hin+2×padding[0]kernel_size[0]stride[0]+1
Wout=Win+2×padding[1]kernel_size[1]stride[1]+1
Raises
  • TypeError – If kernel_size or strides is neither int nor tuple.

  • ValueError – If pad_mode is not "valid" , "same" or "pad" with not case sensitive.

  • ValueError – If data_format is neither 'NCHW' nor 'NHWC'.

  • ValueError – If padding, ceil_mode, count_include_pad, or divisor_override is used or pad_mode is "pad" when data_format is 'NHWC'.

  • ValueError – If kernel_size or strides is less than 1.

  • ValueError – If length of padding tuple/list is not 1 or 2.

  • ValueError – If length of shape of x is not equal to 3 or 4.

  • ValueError – If divisor_override is less than or equal to 0.

  • ValueError – If padding is non-zero when pad_mode is not "pad".

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore as ms
>>> import numpy as np
>>> pool = ms.nn.AvgPool2d(kernel_size=3, stride=1)
>>> x = ms.Tensor(np.random.randint(0, 10, [1, 2, 4, 4]), ms.float32)
>>> output = pool(x)
>>> print(output.shape)
(1, 2, 2, 2)
>>> x = ms.ops.randn(6, 6, 8, 8)
>>> pool2 = ms.nn.AvgPool2d(4, stride=1, pad_mode="pad", padding=2, divisor_override=5)
>>> output2 = pool2(x)
>>> print(output2.shape)
(6, 6, 9, 9)