mindspore.nn.FractionalMaxPool2d

class mindspore.nn.FractionalMaxPool2d(kernel_size, output_size=None, output_ratio=None, return_indices=False, _random_samples=None)[source]

Applies a 2D fractional max pooling to an input signal composed of multiple input planes. The max-pooling operation is applied in kH × kW regions by a stochastic step size determined by the target output size. For any input size, the size of the specified output is H x W. The number of output features is equal to the number of input planes.

Fractional MaxPooling is described in the paper Fractional Max-Pooling.

Parameters
  • kernel_size (Union[int, tuple[int]]) – The size of kernel used to take the maximum value, is an int number that represents height and width of the kernel, or a tuple of two int numbers that represent height and width respectively. The value must be a positive integer.

  • output_size (Union[int, tuple[int]], optional) – The Shape of the target output_size, is an int number that represents height and width, or a tuple of two int numbers that represent height and width respectively. The value must be a positive integer. Default: None.

  • output_ratio (Union[float, tuple[float]], optional) – The ratio of target output shape to input shape. Specifying the size of the output tensor by using a ratio of the input size. Data type : float16, float32, double, and value is between (0, 1). Default: None.

  • return_indices (bool, optional) – If return_indices is True, the indices of max value would be output. Default: False.

  • _random_samples (Tensor, optional) – The random step of FractionalMaxPool2d, which is a 3D tensor. Tensor of data type : float16, float32, double, and value is between (0, 1). Supported shape \((N, C, 2)\). Default: None.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, C, H_{in}, W_{in})\), with float16, float32, float64, int32, int64 data type.

Outputs:
  • y (Tensor) - Has the same type as the input_x. Has the shape \((N, C, H, W)\).

  • argmax (Tensor) - The indices along with the outputs, which is a Tensor, with the same shape as the y and int64 data type. It will output only when return_indices is True.

Raises
  • TypeError – If data type of input_x is not one of the following: float16, float32, float64, int32, int64.

  • TypeError – If data type of _random_samples is not one of the following: float16, float32, float64.

  • ValueError – If kernel_size is not a number and kernel_size is not a tuple of length 2.

  • ValueError – If output_size is not a number and output_size is not a tuple of length 2.

  • ValueError – If the sum of kernel_size , output_size and -1 is larger than the corresponding dimension of input_x.

  • ValueError – If the dimension of _random_samples is not 3.

  • ValueError – if output_size and output_ratio are None at the same time.

  • ValueError – If the first dimension size of input_x and _random_samples is not equal.

  • ValueError – If the second dimension size of input_x and _random_samples is not equal.

  • ValueError – If the third dimension size of _random_samples is not 2.

Supported Platforms:

CPU

Examples

>>> # the kernel_size is an int number and the output_size is a tuple.
>>> import numpy as np
>>> from mindspore import nn
>>> from mindspore import Tensor
>>> import mindspore.common.dtype as mstype
>>> input_x = Tensor(np.array([0.3220, 0.9545, 0.7879, 0.0975, 0.3698,
...                            0.5135, 0.5740, 0.3435, 0.1895, 0.8764,
...                            0.9581, 0.4760, 0.9014, 0.8522, 0.3664,
...                            0.4980, 0.9673, 0.9879, 0.6988, 0.9022,
...                            0.9304, 0.1558, 0.0153, 0.1559, 0.9852]).reshape([1, 1, 5, 5]), mstype.float32)
>>> _random_samples = Tensor(np.array([[[0.8, 0.8]]]), mstype.float32)
>>> net = nn.FractionalMaxPool2d(kernel_size=2, output_size=(2, 2), _random_samples=_random_samples,
...                              return_indices=True)
>>> y, argmax = net(input_x)
>>> y
[[[[0.9545 0.8764]
   [0.9673 0.9852]]]]
>>> argmax
[[[[ 1  9]
   [16 24]]]]
>>> net = nn.FractionalMaxPool2d(kernel_size=2, output_ratio=(0.5, 0.5), _random_samples=_random_samples,
...                              return_indices=True)
>>> y, argmax = net(input_x)
>>> print(y)
[[[[0.9545 0.8764]
   [0.9673 0.9852]]]]
>>> print(argmax)
[[[[ 1  9]
   [16 24]]]]