mindspore.mint.nn.functional.pad
- mindspore.mint.nn.functional.pad(input, pad, mode='constant', value=0.0)[source]
Pads the input tensor according to the pad.
Warning
circular mode has poor performance and is not recommended.
- 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.
Example: to pad only the last dimension of the input tensor, then
pad
has the form \((\text{padding_left}, \text{padding_right})\);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})\);
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 tomindspore.nn.ConstantPad1d
as an example to understand this filling pattern and extend the padding pattern to n dimensions.For
'reflect'
mode, please refer tomindspore.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 tomindspore.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.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:0.0
.
- Returns
Tensor, the tensor after padding.
- Raises
TypeError – If pad is not an int of tuple or int of list.
TypeError – If input is not a Tensor.
ValueError – If length of pad is not even.
ValueError – If length of pad is greater than 6.
ValueError – If mode is not
'constant'
and value notNone
.
- Supported Platforms:
Ascend
Examples
>>> 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.]]]]