class paddle.nn. AdaptiveAvgPool1D ( output_size, name=None ) [source]

This operation applies a 1D adaptive average pooling over an input signal composed of several input planes, based on the input, output_size, return_mask parameters. Input(X) and output(Out) are in NCL format, where N is batch size, C is the number of channels, L is the length of the feature. The output tensor shape will be [N, C, output_size].

For average adaptive pool1d:

\[ \begin{align}\begin{aligned}lstart &= floor(i * L_{in} / L_{out})\\lend &= ceil((i + 1) * L_{in} / L_{out})\\Output(i) &= \frac{ \sum Input[lstart:lend]}{lend - lstart}\end{aligned}\end{align} \]
  • output_size (int) – The target output size. It must be an integer.

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


A callable object of AdaptiveAvgPool1D.


ValueError – ‘output_size’ should be an integer.

  • x(Tensor): 3-D tensor. The input tensor of adaptive avg pool1d operator, which is a 3-D tensor. The data type can be float32, float64.

  • output(Tensor): 3-D tensor. The output tensor of adaptive avg pool1d operator, which is a 3-D tensor. The data type is same as input x.


# average adaptive pool1d
# suppose input data in shape of [N, C, L], `output_size` is m or [m],
# output shape is [N, C, m], adaptive pool divide L dimension
# of input data into m grids averagely and performs poolings in each
# grid to get output.
# adaptive max pool performs calculations as follow:
#     for i in range(m):
#         lstart = floor(i * L / m)
#         lend = ceil((i + 1) * L / m)
#         output[:, :, i] = sum(input[:, :, lstart: lend])/(lend - lstart)
import paddle
import paddle.nn as nn
import numpy as np

data = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32]).astype(np.float32))
AdaptiveAvgPool1D = nn.AdaptiveAvgPool1D(output_size=16)
pool_out = AdaptiveAvgPool1D(data)
# pool_out shape: [1, 3, 16]
forward ( input )

Defines the computation performed at every call. Should be overridden by all subclasses.

  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments

extra_repr ( )

Extra representation of this layer, you can have custom implementation of your own layer.