FractionalMaxPool2D

class paddle.nn. FractionalMaxPool2D ( output_size, kernel_size=None, random_u=None, return_mask=False, name=None ) [source]

This operation applies 2D fractional max pooling on input tensor, which is described in the paper:

[1] Ben Graham, Fractional Max-Pooling. 2015. http://arxiv.org/abs/1412.6071

The h and w dimensions of the output tensor are determined by the parameter output_size.

For each dimension, the fractional max pooling:

\[ \begin{align}\begin{aligned}\alpha &= size_{input} / size_{output}\\index_{start} &= ceil( \alpha * (i + u) - 1)\\index_{end} &= ceil( \alpha * (i + 1 + u) - 1)\\Output &= max(Input[index_{start}:index_{end}])\\where, u \in (0, 1), i = 0,1,2...size_{output}\end{aligned}\end{align} \]

The u from the formula is the parameter random_u, and subtract 1 for the index starts from 0 instead of 1 where ceil works.

For instance, giving a sequence of length 7 is [2, 4, 3, 1, 5, 2, 3], output_size is 5 and random_u is 0.3. The alpha = 7/5 = 1.4, the starts of index is [0, 1, 3, 4, 6], the ends of index is [1, 3, 4, 6, 7] and makes the random sequence in the paper is index_end - index_start = [1, 2, 1, 2, 1]. The strides and kernel_sizes are both equal to the random sequence, giving the final pooling output is [2, 4, 1, 5, 3].

Parameters
  • output_size (int|list|tuple) – The output size. If output size is a tuple or list, it must contain two element, (H, W). H and W can be either a int, or None which means the size will be the same as that of the input.

  • kernel_size (int|list|tuple, optional) – The pool kernel size. If the 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 the square of an int. Default is None, means using the non-overlapping mode.

  • random_u (float) – A random float number in range (0, 1) for the fractional pooling. Default None, means randomly generated by framework which can be fixed by paddle.seed.

  • return_mask (bool, optional) – If true, the index of max pooling point will be returned along with outputs. Default False.

  • name (str, optional) – For detailed information, please refer to Name. Usually name is no need to set and None by default.

Shape:
  • x(Tensor): The input tensor of fractional max pool2d operator, which is a 4-D tensor. The data type can be float16, bfloat16, float32, float64.

  • output(Tensor): The output tensor of fractional max pool2d operator, which is a 4-D tensor. The data type is same as input x.

Returns

A callable object of FractionalMaxPool2D.

Examples

>>> # fractional max pool2d
>>> # suppose input data in shape of [N, C, H, W], `output_size` is [m, n],
>>> # output shape is [N, C, m, n], fractional pool divide H and W dimensions
>>> # of input data into m * n grids and performs poolings in each
>>> # grid to get output.

>>> import paddle

>>> x = paddle.rand([2, 3, 32, 32])

>>> # disjoint: without `kernel_size`
>>> fractional_max_pool = paddle.nn.FractionalMaxPool2D(output_size=3)
>>> pool_out = fractional_max_pool(x=x)
>>> print(pool_out.shape)
[2, 3, 3, 3]

>>> # overlapping: with `kernel_size`
>>> fractional_max_pool = paddle.nn.FractionalMaxPool2D(kernel_size=2, output_size=3)
>>> pool_out = fractional_max_pool(x=x)
>>> print(pool_out.shape)
[2, 3, 3, 3]

>>> fractional_max_pool = paddle.nn.FractionalMaxPool2D(output_size=[2, 3], return_mask=True)
>>> pool_out, indices = fractional_max_pool(x=x)
>>> print(pool_out.shape)
[2, 3, 2, 3]
>>> print(indices.shape)
[2, 3, 2, 3]
forward ( x )

forward

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

Parameters
  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments

extra_repr ( )

extra_repr

Extra representation of this layer, you can have custom implementation of your own layer.