pad

paddle.nn.functional.common. pad ( x, pad, mode='constant', value=0, data_format='NCHW', name=None ) [source]

Pad tensor according to ‘pad’ and ‘mode’. If mode is ‘constant’ and length of pad is twice as length of x dimension, then the padding will be started from the first dimension and moved back onto x according to ‘pad’ and ‘value’. If mode is ‘reflect’, pad[0] and pad[1] must be no greater than width-1. The height and depth dimension has the same condition.

Parameters
  • x (Tensor) – The input tensor with data type float32/double/int32/int64_t.

  • pad (Tensor | List[int32]) – The padding size with data type int32. [len(padding)/2] dimensions of input will be padded. 1. If input dimension is 3, then the pad has the form (pad_left, pad_right). 2. If the input dimension is 4, then the pad has the form (pad_left, pad_right, pad_top, pad_bottom). 3. If the input dimension is 5, then the pad has the form (pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_back).

  • mode (str) – Four modes: ‘constant’ (default), ‘reflect’, ‘replicate’, ‘circular’. When in ‘constant’ mode, this op uses a constant value to pad the input tensor. When in ‘reflect’ mode, uses reflection of the input boundaries to pad the input tensor. When in ‘replicate’ mode, uses input boundaries to pad the input tensor. When in ‘circular’ mode, uses circular input to pad the input tensor. Default is ‘constant’

  • value (float32) – The value to fill the padded areas in ‘constant’ mode . Default is 0.0

  • data_format (str) – An string from: “NCL”, “NLC”, NHWC”, “NCHW”, “NCDHW”, “NDHWC”. Specify the data format of the input data. Default is “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: a Tensor padded according to pad and mode and data type is same as input. Return Type: Tensor

Examples

x = [[[[[1., 2., 3.],
        [4., 5., 6.]]]]]

Case 0:
    pad = [2, 2, 1, 1, 0, 0],
    mode = 'constant'
    value = 0
    Out = [[[[[0. 0. 0. 0. 0. 0. 0.]
              [0. 0. 1. 2. 3. 0. 0.]
              [0. 0. 4. 5. 6. 0. 0.]
              [0. 0. 0. 0. 0. 0. 0.]]]]]

Case 1:
    pad = [2, 2, 1, 1, 0, 0],
    mode = 'reflect'
    Out = [[[[[6. 5. 4. 5. 6. 5. 4.]
              [3. 2. 1. 2. 3. 2. 1.]
              [6. 5. 4. 5. 6. 5. 4.]
              [3. 2. 1. 2. 3. 2. 1.]]]]]

Case 2:
    pad = [2, 2, 1, 1, 0, 0],
    mode = 'replicate'
    Out = [[[[[1. 1. 1. 2. 3. 3. 3.]
              [1. 1. 1. 2. 3. 3. 3.]
              [4. 4. 4. 5. 6. 6. 6.]
              [4. 4. 4. 5. 6. 6. 6.]]]]]

Case 3:
    pad = [2, 2, 1, 1, 0, 0],
    mode = 'circular'
    Out = [[[[[5. 6. 4. 5. 6. 4. 5.]
              [2. 3. 1. 2. 3. 1. 2.]
              [5. 6. 4. 5. 6. 4. 5.]
              [2. 3. 1. 2. 3. 1. 2.]]]]]
Code Examples:
import numpy as np
import paddle
import paddle.nn.functional as F

# example 1
x_shape = (1, 1, 3)
x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1
y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL")
print(y)
# [[[1. 1. 1. 2. 3. 1. 1. 1.]]]

# example 2
x_shape = (1, 1, 2, 3)
x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1
y = F.pad(x, [1, 2, 1, 1], value=1, mode='circular')
print(y)
# [[[[6. 4. 5. 6. 4. 5.]
#    [3. 1. 2. 3. 1. 2.]
#    [6. 4. 5. 6. 4. 5.]
#    [3. 1. 2. 3. 1. 2.]]]]