max_pool2d

paddle.nn.functional. max_pool2d ( x, kernel_size, stride=None, padding=0, return_mask=False, ceil_mode=False, data_format='NCHW', name=None ) [source]

This API implements max pooling 2d operation. See more details in api_nn_pooling_MaxPool2d .

Args:
x (Tensor): The input tensor of pooling operator which is a 4-D tensor with

shape [N, C, H, W]. The format of input tensor is “NCHW” or “NHWC”, where N is batch size, C is the number of channels, H is the height of the feature, and W is the width of the feature. The data type if float32 or float64.

kernel_size (int|list|tuple): The pool kernel size. If pool kernel size is a tuple or list,

it must contain two integers, (kernel_size_Height, kernel_size_Width). Otherwise, the pool kernel size will be a square of an int.

stride (int|list|tuple): The pool stride size. If pool stride size is a tuple or list,

it must contain two integers, (stride_Height, stride_Width). Otherwise, the pool stride size will be a square of an int.

padding (string|int|list|tuple): The padding size. Padding could be in one of the following forms.
  1. A string in [‘valid’, ‘same’].

  2. An int, which means the feature map is zero padded by size of padding on every sides.

  3. A list[int] or tuple(int) whose length is 2, [pad_height, pad_weight] whose value means the padding size of each dimension.

  4. A list[int] or tuple(int) whose length is 4. [pad_height_top, pad_height_bottom, pad_width_left, pad_width_right] whose value means the padding size of each side.

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/nn/functional/pooling.py:docstring of paddle.nn.functional.pooling.max_pool2d, line 21)

Enumerated list ends without a blank line; unexpected unindent.

5. A list or tuple of pairs of integers. It has the form [[pad_before, pad_after], [pad_before, pad_after], …]. Note that, the batch dimension and channel dimension should be [0,0] or (0,0). The default value is 0.

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/nn/functional/pooling.py:docstring of paddle.nn.functional.pooling.max_pool2d, line 23)

Definition list ends without a blank line; unexpected unindent.

ceil_mode (bool): when True, will use ceil instead of floor to compute the output shape return_mask (bool): Whether to return the max indices along with the outputs. Default False, only support “NCHW” data format data_format (string): The data format of the input and output data. An optional string from: “NCHW”, “NHWC”.

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/nn/functional/pooling.py:docstring of paddle.nn.functional.pooling.max_pool2d, line 26)

Unexpected indentation.

The default is “NCHW”. When it is “NCHW”, the data is stored in the order of: [batch_size, input_channels, input_height, input_width].

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/nn/functional/pooling.py:docstring of paddle.nn.functional.pooling.max_pool2d, line 28)

Block quote ends without a blank line; unexpected unindent.

name(str, optional): For detailed information, please refer

to Name. Usually name is no need to set and None by default.

Returns:

Tensor: The output tensor of pooling result. The data type is same as input tensor.

Raises
  • ValueError – If padding is a string, but not “SAME” or “VALID”.

  • ValueError – If padding is “VALID”, but ceil_mode is True.

  • ShapeError

    If the output’s shape calculated is not greater than 0.

    Examples:
    import paddle
    import paddle.nn.functional as F
    import numpy as np
    
    # max pool2d
    x = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32, 32]).astype(np.float32))
    out = F.max_pool2d(x,
                          kernel_size=2,
                          stride=2, padding=0)
    # output.shape [1, 3, 16, 16]
    # for return_mask=True
    out, max_indices = F.max_pool2d(x,
                                       kernel_size=2,
                                       stride=2,
                                       padding=0,
                                       return_mask=True)
    # out.shape [1, 3, 16, 16], max_indices.shape [1, 3, 16, 16],