|
76 | 76 | 'reshape',
|
77 | 77 | 'lod_reset',
|
78 | 78 | 'lrn',
|
| 79 | + 'pad', |
79 | 80 | ]
|
80 | 81 |
|
81 | 82 |
|
@@ -3379,6 +3380,7 @@ def reshape(x, shape, actual_shape=None, act=None, inplace=True, name=None):
|
3379 | 3380 |
|
3380 | 3381 | Examples:
|
3381 | 3382 | .. code-block:: python
|
| 3383 | +
|
3382 | 3384 | data = fluid.layers.data(
|
3383 | 3385 | name='data', shape=[2, 4, 6], dtype='float32')
|
3384 | 3386 | reshaped = fluid.layers.reshape(
|
@@ -3580,3 +3582,62 @@ def lrn(input, n=5, k=1.0, alpha=1e-4, beta=0.75, name=None):
|
3580 | 3582 | "beta": beta})
|
3581 | 3583 |
|
3582 | 3584 | 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