adaptive_avg_pool1d

paddle.nn.functional. adaptive_avg_pool1d ( x, output_size, name=None ) [source]

This API implements adaptive average pooling 1d operation. See more details in api_nn_pooling_AdaptiveAvgPool1d .

Parameters
  • x (Tensor) – The input tensor of pooling operator, which is a 3-D tensor with shape [N, C, L]. The format of input tensor is NCL, where N is batch size, C is the number of channels, L is the length of the feature. The data type is float32 or float64.

  • 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.

Returns

The output tensor of adaptive average pooling result. The data type is same

as input tensor.

Return type

Tensor

Raises

ValueError – ‘output_size’ should be an integer.

Examples

# 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])/(lstart - lend)
#
import paddle
import paddle.nn.functional as F
import numpy as np

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