mindspore.nn.AvgPool1d

class mindspore.nn.AvgPool1d(kernel_size=1, stride=1, pad_mode='valid', padding=0, ceil_mode=False, count_include_pad=True)[source]

Applies a 1D average pooling over an input Tensor which can be regarded as a composition of 1D input planes.

Typically the input is of shape \((N_{in}, C_{in}, L_{in})\), AvgPool1d outputs regional average in the \((L_{in})\)-dimension. Given kernel_size \(l_{ker}\) and stride \(s_0\), the operation is as follows:

\[\text{output}(N_i, C_j, l) = \frac{1}{l_{ker}} \sum_{n=0}^{l_{ker}-1} \text{input}(N_i, C_j, s_0 \times l + n)\]
Parameters
  • kernel_size (int) – The size of kernel window used to take the average value, Default: 1 .

  • stride (int) – The distance of kernel moving, an int number that represents the width of movement is strides, Default: 1 .

  • pad_mode (str) –

    "pad" , case insensitive. Default: "valid" .

    • same: The width of the output is the same as the value after the input is divided by stride.

    • valid: Returns the output obtained by effective calculation without padding. The excess pixels that do not meet the calculation will be discarded.

    • pad: Performs padding on the input. Adds padding size of zeros to both ends of the input. 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 a single integer, in which case padding times or padding[0] times are padded on both sides 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 .

Inputs:
  • x (Tensor) - Tensor of shape \((N, C_{in}, L_{in})\) or \((C_{in}, L_{in})\).

Outputs:

Tensor of shape \((N, C_{out}, L_{out})\) or \((C_{out}, L_{out})\).

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

\[L_{out} = \left\lfloor \frac{L_{in} + 2 \times \text{padding} - \text{kernel_size}}{\text{stride}} + 1\right\rfloor\]
Raises
  • TypeError – If kernel_size or stride is not an int.

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

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

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

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

  • 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.AvgPool1d(kernel_size=6, stride=1)
>>> x = ms.Tensor(np.random.randint(0, 10, [1, 3, 6]), ms.float32)
>>> output = pool(x)
>>> result = output.shape
>>> print(result)
(1, 3, 1)
>>> pool2 = ms.nn.AvgPool1d(4, stride=1, ceil_mode=True, pad_mode='pad', padding=2)
>>> x1 = ms.ops.randn(6, 6, 8)
>>> output = pool2(x1)
>>> print(output.shape)
(6, 6, 9)