pad

paddle.fluid.layers.pad(x, paddings, pad_value=0.0, name=None)[source]

This op will pad a tensor with a constant value given by pad_value, and the padded shape is specified by paddings.

Specifically, the number of values padded before the elements of x in dimension i is indicated by paddings[2*i], and the number of values padded after the elements of x in dimension i is indicated by paddings[2*i+1].

See below for an example.

Given:
    x = [[1, 2], [3, 4]]

    paddings = [0, 1, 1, 2]

    pad_value = 0

Return:

    out = [[0, 1, 2, 0, 0]
           [0, 3, 4, 0, 0]
           [0, 0, 0, 0, 0]]
Parameters
  • x (Variable) – Tensor, data type is float32.

  • paddings (list) – A list of integers. Its elements specify the padded width before and after each dimension in turn. The length of paddings must be equal to \(rank(x) \times 2\).

  • pad_value (float) – The constant value used to pad.

  • 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

The padded tensor, with the same data type and rank as x

Return Type:

Variable

Examples

# x is a rank 2 tensor variable with shape [100, 224].
# out will be a tensor of shape [101, 227]
import paddle.fluid as fluid
x = fluid.data(name='data', shape=[100, 224], dtype='float32')
out = fluid.layers.pad(
    x=x, paddings=[0, 1, 1, 2], pad_value=0.)