mindspore.ops.Pad

View Source On Gitee
class mindspore.ops.Pad(paddings)[source]

Pads the input tensor according to the paddings.

Refer to mindspore.ops.pad() for more details. Use mindspore.ops.pad() instead if paddings has negative values.

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 to be padded. It has 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

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> 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. ]]