Pad2D
- class paddle.nn. Pad2D ( padding: Tensor | Sequence[int] | int, mode: _PaddingTensorMode = 'constant', value: float = 0.0, data_format: DataLayout2D = 'NCHW', name: str | None = None ) [source]
-
This interface is used to construct a callable object of the
Pad2Dclass. Pad tensor according topad,modeandvalue. If mode is'reflect', pad[0] and pad[1] must be no greater than width-1. The height dimension has the same condition.- Parameters
-
padding (Tensor | Sequence[int] | int) – The padding size. If padding is an int, the same padding is applied to all four sides (left, right, top, bottom). If padding is a list or tuple of four ints, it is interpreted as (pad_left, pad_right, pad_top, pad_bottom).
mode (str, optional) –
Four modes:
'constant'(default),'reflect','replicate','circular'. Default:'constant'.’constant’ mode, uses a constant value to pad the input tensor.
’reflect’ mode, uses reflection of the input boundaries to pad the input tensor.
’replicate’ mode, uses input boundaries to pad the input tensor.
’circular’ mode, uses circular input to pad the input tensor.
value (float, optional) – The value to fill the padded areas. Default is \(0.0\).
data_format (str, optional) – An string from:
'NCHW','NHWC'. Specify the data format of the input data. Default:'NCHW'.name (str|None, optional) – For details, please refer to api_guide_Name. Generally, no setting is required. Default:
None.
- Returns
-
The padded tensor.
- Return type
-
Tensor
Examples
>>> import paddle >>> import paddle.nn as nn >>> input_shape = (1, 1, 2, 3) >>> pad = [1, 0, 1, 2] >>> data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1 >>> my_pad = nn.Pad2D(padding=pad, mode="constant") >>> result = my_pad(data) >>> print(result) Tensor(shape=[1, 1, 5, 4], dtype=float32, place=Place(cpu), stop_gradient=True, [[[[0., 0., 0., 0.], [0., 1., 2., 3.], [0., 4., 5., 6.], [0., 0., 0., 0.], [0., 0., 0., 0.]]]])
