mindspore.ops.pad

mindspore.ops.pad(input_x, paddings)[source]

Pads the input tensor according to the paddings.

The formula to calculate the shape of the output tensor is as follows,

\[\begin{split}\begin{aligned} &\text{ input_x_shape} = (N_{1},N_{2},...,N_{n}) \\ &\begin{aligned} \text{output_shape = }(&N_{1}+paddings[0,0]+paddings[0,1], \\ & N_{2}+paddings[1,0]+paddings[1,1], \\ &... , \\ & N_{n}+paddings[n-1,0]+paddings[n-1,1]) \end{aligned} \end{aligned}\end{split}\]
Parameters
  • input_x (Tensor) – Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions.

  • paddings (tuple) – The shape of parameter paddings is (N, 2). N is the rank of input data. All elements of paddings are int type. For the input in D th dimension, paddings[D, 0] indicates how many sizes to be extended(if this value > 0) or clipped(if this value < 0) ahead of the input tensor in the D th dimension, and paddings[D, 1] indicates how many sizes to be extended(if this value > 0) or clipped(if this value < 0) behind the input tensor in the D th dimension.

Returns

Tensor, the tensor after padding.

Raises
  • TypeError – If paddings is not a tuple.

  • TypeError – If input_x is not a Tensor.

  • ValueError – If shape of paddings is not \((N, 2)\).

  • ValueError – If paddings.size is not equal to 2 * len(input_x).

  • ValueError – If the calculated output shape contains zero or negative dimension.

Supported Platforms:

Ascend GPU CPU

Examples

>>> input_x = Tensor(np.array([[-0.1, 0.3, 3.6], [0.4, 0.5, -3.2]]), mindspore.float32)
>>> paddings = ((1, 2), (2, 1))
>>> output = ops.pad(input_x, paddings)
>>> print(output)
[[ 0.   0.   0.   0.   0.   0. ]
 [ 0.   0.  -0.1  0.3  3.6  0. ]
 [ 0.   0.   0.4  0.5 -3.2  0. ]
 [ 0.   0.   0.   0.   0.   0. ]
 [ 0.   0.   0.   0.   0.   0. ]]