Pad1D
- class paddle.nn. Pad1D ( padding: Tensor | Sequence[int] | int, mode: _PaddingTensorMode = 'constant', value: float = 0.0, data_format: DataLayout1D = 'NCL', name: str | None = None ) [source]
-
This interface is used to construct a callable object of the
Pad1Dclass. Pad tensor according topad,modeandvalue. If mode isreflect, pad[0] and pad[1] must be no greater than width-1.- Parameters
-
padding (Tensor | Sequence[int] | int) – The padding size. If padding is an int, the same padding is applied to both the left and right side. If padding is a list or tuple of two ints, it is interpreted as (pad_left, pad_right).
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:
'NCL','NLC'. Specify the data format of the input data. Default:'NCL'.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, 2, 3) >>> pad = [1, 2] >>> data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1 >>> my_pad = nn.Pad1D(padding=pad, mode="constant") >>> result = my_pad(data) >>> print(result) Tensor(shape=[1, 2, 6], dtype=float32, place=Place(cpu), stop_gradient=True, [[[0., 1., 2., 3., 0., 0.], [0., 4., 5., 6., 0., 0.]]])
