# Pool2D¶

class paddle.fluid.dygraph.nn. Pool2D ( pool_size=- 1, pool_type='max', pool_stride=1, pool_padding=0, global_pooling=False, use_cudnn=True, ceil_mode=False, exclusive=True, data_format='NCHW' ) [source]

This interface is used to construct a callable object of the Pool2D class. For more details, refer to code examples. The pooling2d operation calculates the output based on the input, pool_type and pool_size, pool_stride, pool_padding parameters.Input and output are in NCHW format, where N is batch size, C is the number of feature map, H is the height of the feature map, and W is the width of the feature map. Parameters(ksize, strides, paddings) are two elements. These two elements represent height and width, respectively. The input(X) size and output(Out) size may be different.

Example

• Input:

Input shape: $$(N, C, H_{in}, W_{in})$$

• Output:

Output shape: $$(N, C, H_{out}, W_{out})$$

If ceil_mode = False:

$\begin{split}H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1 \\\\ W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1])}{strides[1]} + 1\end{split}$

If ceil_mode = True:

$\begin{split}H_{out} = \\frac{(H_{in} - ksize[0] + 2 * paddings[0] + strides[0] - 1)}{strides[0]} + 1 \\\\ W_{out} = \\frac{(W_{in} - ksize[1] + 2 * paddings[1] + strides[1] - 1)}{strides[1]} + 1\end{split}$

If exclusive = False:

$\begin{split}hstart &= i * strides[0] - paddings[0] \\\\ hend &= hstart + ksize[0] \\\\ wstart &= j * strides[1] - paddings[1] \\\\ wend &= wstart + ksize[1] \\\\ Output(i ,j) &= \\frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]}\end{split}$

If exclusive = True:

$\begin{split}hstart &= max(0, i * strides[0] - paddings[0])\\\\ hend &= min(H, hstart + ksize[0]) \\\\ wstart &= max(0, j * strides[1] - paddings[1]) \\\\ wend & = min(W, wstart + ksize[1]) \\\\ Output(i ,j) & = \\frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)}\end{split}$
Parameters
• pool_size (int or list or tuple, optional) – The pool kernel size. If pool kernel size is a tuple or list, it must contain two integers, (pool_size_Height, pool_size_Width). Otherwise, the pool kernel size will be a square of an int. Default: -1.

• pool_type (str, optional) – The pooling type, can be “max” for max-pooling and “avg” for average-pooling. Default: max.

• pool_stride (int or list or tuple, optional) – The pool stride size. If pool stride size is a tuple or list, it must contain two integers, (pool_stride_Height, pool_stride_Width). Otherwise, the pool stride size will be a square of an int. Default: 1.

• pool_padding (int or list or tuple, optional) – The padding size for pooling operation. If pool_padding is a tuple, it must contain two integers, (pool_padding_on_Height, pool_padding_on_Width). Otherwise, the padding size for pooling operation will be a square of an int. Default: 0.

• global_pooling (bool, optional) – Whether to use the global pooling. If global_pooling = true, kernel size and paddings will be ignored. Default: False.

• use_cudnn (bool, optional) – Only used in cudnn kernel, need install cudnn. Default: True.

• ceil_mode (bool, optional) – Whether to use the ceil function to calculate output height and width. False is the default. If it is set to False, the floor function will be used. Default: False.

• exclusive (bool, optional) – Whether to exclude padding points in average pooling mode. Default: True.

• data_format (string) – The data format of the input and output data. An optional string from: “NCHW”, “NHWC”. The default is “NCHW”. When it is “NCHW”, the data is stored in the order of: [batch_size, input_channels, input_height, input_width]. When it is “NHWC”, the data is stored in the order of: [batch_size, input_height, input_width, input_channels]

Returns

None

Raises
• ValueError – If pool_type is not “max” nor “avg”.

• ValueError – If global_pooling is False and pool_size is -1.

• ValueError – If use_cudnn is not a bool value.

• ValueError – If data_format is not “NCHW” nor “NHWC”.

Examples

import paddle.fluid as fluid
import numpy as np

with fluid.dygraph.guard():
data = numpy.random.random((3, 32, 32, 5)).astype('float32')
pool2d = fluid.dygraph.Pool2D(pool_size=2,
pool_type='max',
pool_stride=1,
global_pooling=False)
pool2d_res = pool2d(to_variable(data))

forward ( input )

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

Parameters
• *inputs (tuple) – unpacked tuple arguments

• **kwargs (dict) – unpacked dict arguments