zeropad2d

paddle.nn.functional. zeropad2d ( x, padding, data_format='NCHW', name=None ) [source]

Pads the input tensor boundaries with zero according to ‘pad’.

Parameters
  • x (Tensor) – The input tensor with data type float16/float32/float64/int32/int64.

  • padding (int | Tensor | List[int] | Tuple[int]) – The padding size with data type int. The input dimension should be 4 and pad has the form (pad_left, pad_right, pad_top, pad_bottom).

  • data_format (str, optional) – An string from: “NHWC”, “NCHW”. Specify the data format of the input data. Default: “NCHW”.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

Tensor, padded with 0 according to pad and data type is same as input.

Examples

>>> import paddle
>>> import paddle.nn.functional as F

>>> x_shape = paddle.to_tensor([1, 1, 2, 3])
>>> x = paddle.arange(paddle.prod(x_shape), dtype="float32").reshape(x_shape) + 1
>>> y = F.zeropad2d(x, [1, 2, 1, 1])
>>> print(y)
Tensor(shape=[1, 1, 4, 6], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[[0., 0., 0., 0., 0., 0.],
   [0., 1., 2., 3., 0., 0.],
   [0., 4., 5., 6., 0., 0.],
   [0., 0., 0., 0., 0., 0.]]]])