From eda7ff054b8bc7a6e75b34df9740674ee47bf20a Mon Sep 17 00:00:00 2001 From: liu zhengxi <380185688@qq.com> Date: Fri, 10 Apr 2020 15:15:40 +0800 Subject: [PATCH] Refine example code in en doc (#23549) * refine example code in en doc, test=develop, test=document_fix * update shape to make it meaningful, test=develop, test=document_fix --- python/paddle/fluid/layers/nn.py | 102 ++++++++++++--------------- python/paddle/fluid/layers/tensor.py | 2 +- 2 files changed, 48 insertions(+), 56 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 9b2d1d233b2..758597c6e07 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -6396,7 +6396,6 @@ def pad(x, paddings, pad_value=0., name=None): pad_value = 0 Return: - out = [[0, 1, 2, 0, 0] [0, 3, 4, 0, 0] [0, 0, 0, 0, 0]] @@ -6421,12 +6420,10 @@ def pad(x, paddings, pad_value=0., name=None): Examples: .. code-block:: python - # x is a rank 2 tensor variable with shape [100, 224]. - # out will be a tensor of shape [101, 227] + # x is a rank 2 tensor variable 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.) + x = fluid.data(name='data', shape=[300, 300], dtype='float32') + out = fluid.layers.pad(x=x, paddings=[0, 1, 1, 2], pad_value=0.) """ helper = LayerHelper('pad', input=x, **locals()) dtype = helper.input_dtype() @@ -6464,29 +6461,34 @@ def pad_constant_like(x, y, pad_value=0., name=None): [27, 28, 29]], [[30, 31, 32], [33, 34, 35]]]] + X.shape = (2, 3, 2, 3) Y = [[[[35, 36, 37]], [[38, 39, 40]], [[41, 42, 43]]]] + Y.shape = (1, 3, 1, 3) - And - pad_value = -1, + + And + pad_value = 0. Return: Out = [[[[35, 36, 37], - [-1, -1, -1]], + [ 0, 0, 0]], [[38, 39, 40], - [-1, -1, -1]], + [ 0, 0, 0]], [[41, 42, 43], - [-1, -1, -1]]], - [[[-1, -1, -1], - [-1, -1, -1]], - [[-1, -1, -1], - [-1, -1, -1]], - [[-1, -1, -1], - [-1, -1, -1]]]] - Out.shape = (2, 3, 2, 3) + [ 0, 0, 0]]], + [[[ 0, 0, 0], + [ 0, 0, 0]], + [[ 0, 0, 0], + [ 0, 0, 0]], + [[ 0, 0, 0], + [ 0, 0, 0]]]] + + Out.shape = [2, 3, 2, 3] + Args: x (Variable): Tensor, its shape specifies the shape of output. @@ -8743,47 +8745,37 @@ def pad2d(input, Examples: .. code-block:: text - Given that X is a channel of image from input: - - X = [[1, 2, 3], - [4, 5, 6]] - - Case 0: - - paddings = [0, 1, 2, 3], - mode = 'constant' - pad_value = 0 - - Out = [[0, 0, 1, 2, 3, 0, 0, 0] - [0, 0, 4, 5, 6, 0, 0, 0] - [0, 0, 0, 0, 0, 0, 0, 0]] - - Case 1: - - paddings = [0, 1, 2, 1], - mode = 'reflect' - - Out = [[3, 2, 1, 2, 3, 2] - [6, 5, 4, 5, 6, 5] - [3, 2, 1, 2, 3, 2]] - - Case 2: - - paddings = [0, 1, 2, 1], - mode = 'edge' - - Out = [[1, 1, 1, 2, 3, 3] - [4, 4, 4, 5, 6, 6] - [4, 4, 4, 5, 6, 6]] + Input = [[[[1., 2., 3.], + [4., 5., 6.]]]] + + Case 0: + paddings = [0, 1, 2, 3], + mode = 'constant' + pad_value = 0 + Out = [[[[0., 0., 1., 2., 3., 0., 0., 0.], + [0., 0., 4., 5., 6., 0., 0., 0.], + [0., 0., 0., 0., 0., 0., 0., 0.]]]] + + Case 1: + paddings = [0, 1, 2, 1], + mode = 'reflect' + Out = [[[[3., 2., 1., 2., 3., 2.], + [6., 5., 4., 5., 6., 5.], + [3., 2., 1., 2., 3., 2.]]]] + + Case 2: + paddings = [0, 1, 2, 1], + mode = 'edge' + Out = [[[[1., 1., 1., 2., 3., 3.], + [4., 4., 4., 5., 6., 6.], + [4., 4., 4., 5., 6., 6.]]]] Code Examples: .. code-block:: python - import paddle.fluid as fluid - data = fluid.data(name='data', shape=[None, 3, 32, 32], - dtype='float32') - result = fluid.layers.pad2d(input=data, paddings=[1, 2, 3, 4], - mode='reflect') + import paddle.fluid as fluid + data = fluid.data(name='data', shape=[None, 3, 32, 32], dtype='float32') + result = fluid.layers.pad2d(input=data, paddings=[0, 1, 2, 3], mode='reflect') """ if in_dygraph_mode(): diff --git a/python/paddle/fluid/layers/tensor.py b/python/paddle/fluid/layers/tensor.py index 0027a253995..e705534c99e 100644 --- a/python/paddle/fluid/layers/tensor.py +++ b/python/paddle/fluid/layers/tensor.py @@ -138,7 +138,7 @@ def create_global_var(shape, import paddle.fluid as fluid import paddle.fluid.layers as layers var = layers.create_global_var(shape=[2,3], value=1.0, dtype='float32', - persistable=True, force_cpu=True, name='new_var') + persistable=True, force_cpu=True, name='new_var') """ helper = LayerHelper("global_var", **locals()) var = helper.create_global_variable( -- GitLab