mindspore.mint.nn.functional.pad

View Source On AtomGit
mindspore.mint.nn.functional.pad(input, pad, mode='constant', value=None)[source]

Pads the input tensor according to the pad.

Warning

circular mode is not supported on Ascend.

Parameters:
  • input (Tensor) – Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions.

  • pad (Union[tuple[int], list[int], Tensor]) –

    Filling position of pad. \(\left\lfloor\frac{\text{len(pad)}}{2}\right\rfloor\) dimensions of input will be padded.

    For example: to pad only the last dimension of the input tensor, then pad has the form \((\text{padding_left}, \text{padding_right})\);

    For example: to pad the last 2 dimensions of the input tensor, then use \((\text{padding_left}, \text{padding_right}, \text{padding_top}, \text{padding_bottom})\);

    For example: to pad the last 3 dimensions, use \((\text{padding_left}, \text{padding_right}, \text{padding_top}, \text{padding_bottom}, \text{padding_front}, \text{padding_back})\) and so on.

  • mode (str, optional) –

    Pad filling mode, 'constant' , 'reflect' , 'replicate' or 'circular' . Default: 'constant' .

    For 'constant' mode, please refer to mindspore.nn.ConstantPad1d as an example to understand this filling pattern and extend the padding pattern to n dimensions.

    For 'reflect' mode, please refer to mindspore.nn.ReflectionPad1d as an example to understand this filling pattern. The reflect mode is used to pad the last three dimensions of 4D or 5D input, the last two dimensions of 3D or 4D input, or the last dimension of 2D or 3D input.

    For 'replicate' mode, please refer to mindspore.nn.ReplicationPad1d as an example to understand this filling pattern. The replicate mode is used to pad the last three dimensions of 4D or 5D input, the last two dimensions of 3D or 4D input, or the last dimension of 2D or 3D input.

    For 'circular' mode, the pixels from one edge of the image are wrapped around to the opposite edge, such that the pixel on the right edge of the image is replaced with the pixel on the left edge, and the pixel on the bottom edge is replaced with the pixel on the top edge. The circular mode is used to pad the last three dimensions of 4D or 5D input, the last two dimensions of 3D or 4D input, or the last dimension of 2D or 3D input. These shape patterns are currently supported on CPU.

  • value (Union[int, float, None], optional) – Valid only in 'constant' mode. Set the padding value in 'constant' mode. If the value is None, 0 is used as the default padding value. Default: None .

Note

While running on Ascend, the following scenes are currently supported:

  • 'constant' mode: constant padding on the last \(\frac{\text{len(pad)}}{2}\) dimensions. The length of pad must be even and no more than 2 * input.ndim.

  • 'reflect' mode: only 1D/2D/3D reflection padding is supported. When len(pad) is 2, the input rank must be 2 or 3; when len(pad) is 4, the input rank must be 3 or 4; when len(pad) is 6, the input rank must be 4 or 5.

  • 'replicate' mode: only 1D/2D/3D replication padding is supported. When len(pad) is 2, the input rank must be 2 or 3; when len(pad) is 4, the input rank must be 3 or 4; when len(pad) is 6, the input rank must be 4 or 5.

  • 'circular' mode is not supported on Ascend.

Returns:

Tensor, the tensor after padding.

Raises:
  • TypeError – If pad is not a tuple of int or a list of int.

  • ValueError – If length of pad is not even.

  • ValueError – If mode is 'constant' and length of pad is greater than 2 * input.ndim.

  • ValueError – If mode is 'reflect' , 'replicate' and length of pad is not 2, 4 or 6.

  • ValueError – If mode is 'reflect' or 'replicate' and the combination of pad and input rank is not supported on Ascend.

  • ValueError – If mode is not 'constant' and value is neither None nor 0.

  • RuntimeError – If mode is 'circular' while running on Ascend.

Supported Platforms:

Ascend

Examples

>>> import mindspore as ms
>>> from mindspore import mint
>>> import numpy as np
>>> x = ms.Tensor(np.arange(1 * 2 * 2 * 2).reshape((1, 2, 2, 2)), dtype=ms.float64)
>>> output = mint.nn.functional.pad(x, [1, 0, 0, 1], mode='constant', value=6.0)
>>> print(output)
[[[[6. 0. 1.]
   [6. 2. 3.]
   [6. 6. 6.]]
  [[6. 4. 5.]
   [6. 6. 7.]
   [6. 6. 6.]]]]