Skip to content

Commit d2f9e19

Browse files
authored
Merge pull request #9582 from guoshengCS/add-python-pad
Add python wrapper for pad_op
2 parents f121745 + 3c370ee commit d2f9e19

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

doc/fluid/api/layers.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,12 @@ reshape
494494
.. autofunction:: paddle.fluid.layers.reshape
495495
:noindex:
496496

497+
pad
498+
---
499+
500+
.. autofunction:: paddle.fluid.layers.pad
501+
:noindex:
502+
497503
scale
498504
-----
499505

python/paddle/fluid/layers/nn.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
'reshape',
7777
'lod_reset',
7878
'lrn',
79+
'pad',
7980
]
8081

8182

@@ -3379,6 +3380,7 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
33793380
33803381
Examples:
33813382
.. code-block:: python
3383+
33823384
data = fluid.layers.data(
33833385
name='data', shape=[2, 4, 6], dtype='float32')
33843386
reshaped = fluid.layers.reshape(
@@ -3580,3 +3582,62 @@ def lrn(input, n=5, k=1.0, alpha=1e-4, beta=0.75, name=None):
35803582
"beta": beta})
35813583

35823584
return lrn_out
3585+
3586+
3587+
def pad(x, paddings, pad_value=0., name=None):
3588+
"""
3589+
Pads a tensor with a constant value given by :attr:`pad_value`, and the
3590+
padded width is specified by :attr:`paddings`.
3591+
3592+
Specifically, the number of values padded before the contents of :attr:`x`
3593+
in dimension :attr:`i` is indicated by :attr:`paddings[i]`, and the number
3594+
of values padded after the contents of :attr:`x` in dimension :attr:`i` is
3595+
indicated by :attr:`paddings[i+1]`.
3596+
3597+
See below for an example.
3598+
3599+
.. code-block:: text
3600+
3601+
Given:
3602+
x = [[1, 2], [3, 4]]
3603+
3604+
paddings = [0, 1, 1, 2]
3605+
3606+
pad_value = 0
3607+
3608+
Return:
3609+
3610+
out = [[0, 1, 2, 0, 0]
3611+
[0, 3, 4, 0, 0]
3612+
[0, 0, 0, 0, 0]]
3613+
3614+
Args:
3615+
x (Variable): The input tensor variable.
3616+
paddings (list): A list of integers. Its elements specify the padded
3617+
width before and after for each dimension in turn.
3618+
The length of :attr:paddings must be
3619+
:math:`rank(x) \\times 2`.
3620+
pad_value (float): The constant value used to pad.
3621+
name(str|None): A name for this layer(optional). If set None, the layer
3622+
will be named automatically.
3623+
3624+
Returns:
3625+
Variable: The padded tensor variable.
3626+
3627+
Examples:
3628+
.. code-block:: python
3629+
3630+
# x is a rank 2 tensor variable.
3631+
out = fluid.layers.pad(
3632+
x=x, paddings=[0, 1, 1, 2], pad_value=0.)
3633+
"""
3634+
helper = LayerHelper('pad', input=x, **locals())
3635+
dtype = helper.input_dtype()
3636+
out = helper.create_tmp_variable(dtype)
3637+
helper.append_op(
3638+
type='pad',
3639+
inputs={'X': x},
3640+
outputs={'Out': out},
3641+
attrs={'paddings': paddings,
3642+
'pad_value': float(pad_value)})
3643+
return out

0 commit comments

Comments
 (0)