AdaptiveAvgPool1D

class paddle.nn. AdaptiveAvgPool1D ( output_size, name=None ) [源代码]

根据 output_size 对一个输入 Tensor 计算 1D 的自适应平均池化。输入和输出都是以 NCL 格式表示的 3-D Tensor,其中 N 是批大小,C 是通道数而 L 是特征的长度。输出的形状是 \([N, C, output\_size]\)

计算公式为

\[ \begin{align}\begin{aligned}lstart &= \lfloor i * L_{in} / L_{out}\rfloor,\\lend &= \lceil(i + 1) * L_{in} / L_{out}\rceil,\\Output(i) &= \frac{\sum Input[lstart:lend]}{lend - lstart}.\end{aligned}\end{align} \]

参数

  • output_size (int) - 输出特征的长度,数据类型为 int。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

用于计算 1D 自适应平均池化的可调用对象。

代码示例

# 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

data = paddle.uniform([1, 3, 32], dtype="float32", min=-1, max=1)
AdaptiveAvgPool1D = nn.AdaptiveAvgPool1D(output_size=16)
pool_out = AdaptiveAvgPool1D(data)
# pool_out shape: [1, 3, 16]