AdaptiveAvgPool1d_cn.rst 2.3 KB
Newer Older
B
Bai Yifan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
.. _cn_api_nn_AdaptiveAvgPool1d:


AdaptiveAvgPool1d
-------------------------------

.. py:function:: paddle.nn.AdaptiveAvgPool1d(output_size, name=None)

该算子根据输入 `x` , `output_size` 等参数对一个输入Tensor计算1D的自适应平均池化。输入和输出都是3-D Tensor,
默认是以 `NCL` 格式表示的,其中 `N` 是 batch size, `C` 是通道数, `L` 是输入特征的长度.

计算公式如下:

..  math::

    lstart &= floor(i * L_{in} / L_{out})

    lend &= ceil((i + 1) * L_{in} / L_{out})

    Output(i) &= \frac{sum(Input[lstart:lend])}{(lstart - lend)}


参数
:::::::::
    - **output_size** (int|list|tuple): 算子输出特征图的长度,其数据类型为int,list或tuple。
    - **name** (str,可选): 操作的名称(可选,默认值为None)。更多信息请参见 :ref:`api_guide_Name`。

形状
:::::::::
    - **x** (Tensor): 默认形状为(批大小,通道数,输出特征长度),即NCL格式的3-D Tensor。 其数据类型为float32或者float64。
    - **output** (Tensor): 默认形状为(批大小,通道数,输出特征长度),即NCL格式的3-D Tensor。 其数据类型与输入x相同。

返回
:::::::::
计算AdaptiveAvgPool1d的可调用对象

抛出异常
:::::::::
    - ``ValueError`` - ``output_size`` 应是一个整数或长度为1的list,tuple

代码示例
:::::::::

.. code-block:: python

        # 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 avg 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 as nn
        import numpy as np
        paddle.disable_static()
        
        data = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32]).astype(np.float32))
        AdaptiveAvgPool1d = nn.layer.AdaptiveAvgPool1d(output_size=16)
        pool_out = AdaptiveAvgPool1d(data)
        # pool_out shape: [1, 3, 16]