Skip to content

Commit 4c8eef5

Browse files
committed
Add python wrapper for pad_op
1 parent 6cfc0c1 commit 4c8eef5

File tree

1 file changed

+58
-0
lines changed
  • python/paddle/fluid/layers

1 file changed

+58
-0
lines changed

python/paddle/fluid/layers/nn.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
'autoincreased_step_counter',
7676
'lod_reset',
7777
'lrn',
78+
'pad',
7879
]
7980

8081

@@ -3482,3 +3483,60 @@ def lrn(input, n=5, k=1.0, alpha=1e-4, beta=0.75, name=None):
34823483
"beta": beta})
34833484

34843485
return lrn_out
3486+
3487+
3488+
def pad(x, paddings, pad_value=0., name=None):
3489+
"""
3490+
Pads a tensor with a constant value given by :attr:pad_value, and the
3491+
padded width is specified by :attr:paddings.
3492+
3493+
Specifically, the number of values padded before each dimension
3494+
:attr:i is indicated by :attr:paddings[i], and the number of values padded
3495+
after each dimension :attr:i is indicated by :attr:paddings[i+1].
3496+
3497+
See below for an example.
3498+
3499+
.. code-block:: text
3500+
3501+
Given:
3502+
x = [[1, 2], [3, 4]]
3503+
3504+
paddings = [0, 1, 1, 2]
3505+
3506+
pad_value = 0
3507+
3508+
Return:
3509+
3510+
out = [[0, 1, 2, 0, 0]
3511+
[0, 3, 4, 0, 0]
3512+
[0, 0, 0, 0, 0]]
3513+
3514+
Args:
3515+
x (Variable): The input tensor variable.
3516+
paddings (list): A list of integers. Its elements specify the padded
3517+
width before and after for each dimension in turn.
3518+
The length of :attr:paddings must be
3519+
:math:`rank(x) \\times 2`.
3520+
pad_value (float): The constant value used to pad.
3521+
name(str|None): A name for this layer(optional). If set None, the layer
3522+
will be named automatically.
3523+
3524+
Returns:
3525+
Variable: The padded tensor variable.
3526+
3527+
Examples:
3528+
.. code-block:: python
3529+
# x is a rank 2 tensor variable.
3530+
out = fluid.layers.pad(
3531+
x=x, paddings=[0, 1, 1, 2], pad_value=0.)
3532+
"""
3533+
helper = LayerHelper('pad', input=x, **locals())
3534+
dtype = helper.input_dtype()
3535+
out = helper.create_tmp_variable(dtype)
3536+
helper.append_op(
3537+
type='pad',
3538+
inputs={'X': x},
3539+
outputs={'Out': out},
3540+
attrs={'paddings': paddings,
3541+
'pad_value': float(pad_value)})
3542+
return out

0 commit comments

Comments
 (0)