mindspore.nn.ZeroPad2d

View Source On AtomGit
class mindspore.nn.ZeroPad2d(padding)[source]

Pads the last two dimensions of input tensor with zero.

Warning

Starting after version 2.9.0, the forward input x will be renamed to input.

Parameters:

padding (Union[int, tuple]) – The padding size to pad the last two dimensions of input tensor. If an int, the same padding is applied to both boundaries of the input's last two dimensions. If a tuple of length 4, uses (padding_0, padding_1, padding_2, padding_3) to pad. If the input is x, the size of last dimension of output is \(padding\_0 + x.shape[-1] + padding\_1\). The size of penultimate dimension of output is \(padding\_2 + x.shape[-2] + padding\_3\). The remaining dimensions of the output are consistent with those of the input. Only support non-negative value while running in Ascend.

Inputs:
  • x (Tensor) - shape is \((N, *)\), where \(*\) means, any number of additional dimensions. It is not supported that the size of dimensions is greater than 5 while running in Ascend.

Returns:

Tensor, the tensor after padding.

Raises:
  • TypeError – If padding is not a tuple or int.

  • ValueError – If the length of padding is greater than 4 or not a multiple of 2.

  • ValueError – If the output shape after padding is not positive.

  • ValueError – If the rank of x is greater than 5 while running in Ascend.

  • ValueError – If padding contains negative values while running in Ascend.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> import mindspore as ms
>>> x = np.ones(shape=(1, 2, 3, 4)).astype(np.float32)
>>> x = ms.Tensor(x)
>>> padding = (1, 1, 0, 1)
>>> pad = ms.nn.ZeroPad2d(padding)
>>> out = pad(x)
>>> print(out)
[[[[0. 1. 1. 1. 1. 0.]
   [0. 1. 1. 1. 1. 0.]
   [0. 1. 1. 1. 1. 0.]
   [0. 0. 0. 0. 0. 0.]]
  [[0. 1. 1. 1. 1. 0.]
   [0. 1. 1. 1. 1. 0.]
   [0. 1. 1. 1. 1. 0.]
   [0. 0. 0. 0. 0. 0.]]]]
>>> print(out.shape)
(1, 2, 4, 6)