mindspore.ops.Pad

class mindspore.ops.Pad(paddings)[source]

Pads the input tensor according to the paddings. For example, to pad only the last dimension of the input tensor, then pad has the form (padding_left,padding_right); to pad the last 2 dimensions of the input tensor, then use (padding_left,padding_right, padding_top,padding_bottom); to pad the last 3 dimensions, use (padding_left,padding_right, padding_top,padding_bottom padding_front,padding_back).

\[\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

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 ahead of the input tensor in the D th dimension, and paddings[D, 1] indicates how many sizes to be extended behind the input tensor in the D th dimension.

Inputs:
  • input_x (Tensor) - Tensor of shape \((N, *)\), where \(*\) means, any number of additional dimensions.

Outputs:

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).

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)
>>> pad_op = ops.Pad(((1, 2), (2, 1)))
>>> output = pad_op(input_x)
>>> 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. ]]