ConstantPad1D
- class paddle.nn. ConstantPad1D ( padding: Tensor | Sequence[int] | int, value: float, data_format: DataLayout1D = 'NCL', name: str | None = None ) [source]
-
This interface is used to construct a callable object of the
ConstantPad1Dclass. Pads the input tensor boundaries with a constant value.- 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).
value (float) – The value to fill the padded areas.
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.
- Shape:
-
x(Tensor): The input tensor of constantpad1d operator, which is a 3-D tensor. The data type can be float32, float64.
output(Tensor): The output tensor of constantpad1d operator, which is a 3-D tensor. The data type is same as input x.
- 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.ConstantPad1D(padding=pad, value=0.5) >>> result = my_pad(data) >>> print(result) Tensor(shape=[1, 2, 6], dtype=float32, place=Place(cpu), stop_gradient=True, [[[0.5, 1. , 2. , 3. , 0.5, 0.5], [0.5, 4. , 5. , 6. , 0.5, 0.5]]])
