pad¶
- paddle.nn.functional. 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[int]|tuple[int]) – The padding size with data type int. If mode is ‘constant’ and length of pad is twice as length of x dimension, then x will be padded from the first dimension to the last dimension. Else: 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, optional) – - Four modes: ‘constant’ (default), ‘reflect’, ‘replicate’, ‘circular’. Default is ‘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 in ‘constant’ mode . Default is \(0.0\), 
- data_format (str, optional) – An string from: “NCL”, “NLC”, NHWC”, “NCHW”, “NCDHW”, “NDHWC”. Specify the data format of the input data. Default is “NCHW”, 
- name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None. 
 
- Returns
- 
           Tensor, a Tensor padded according to pad and mode and data type is same as input. 
 Example x = [[[[[1., 2., 3.], [4., 5., 6.]]]]] Case 0: pad = [0, 0, 0, 0, 0, 0, 1, 1, 0, 0], mode = 'constant' value = 0 Out = [[[[[0., 0., 0.], [1., 2., 3.], [4., 5., 6.], [0., 0., 0.]]]]] Case 1: 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 2: 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 3: 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 4: 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.]]]]]Examples import paddle import paddle.nn.functional as F # example 1 x_shape = (1, 1, 3) x = paddle.arange(paddle.prod(paddle.to_tensor(x_shape)), dtype="float32").reshape(x_shape) + 1 y = F.pad(x, [0, 0, 0, 0, 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, 3) x = paddle.arange(paddle.prod(paddle.to_tensor(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 3 x_shape = (1, 1, 2, 3) x = paddle.arange(paddle.prod(paddle.to_tensor(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.]]]] 
