未验证 提交 eda7ff05 编写于 作者: L liu zhengxi 提交者: GitHub

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
上级 f9e2a279
...@@ -6396,7 +6396,6 @@ def pad(x, paddings, pad_value=0., name=None): ...@@ -6396,7 +6396,6 @@ def pad(x, paddings, pad_value=0., name=None):
pad_value = 0 pad_value = 0
Return: Return:
out = [[0, 1, 2, 0, 0] out = [[0, 1, 2, 0, 0]
[0, 3, 4, 0, 0] [0, 3, 4, 0, 0]
[0, 0, 0, 0, 0]] [0, 0, 0, 0, 0]]
...@@ -6421,12 +6420,10 @@ def pad(x, paddings, pad_value=0., name=None): ...@@ -6421,12 +6420,10 @@ def pad(x, paddings, pad_value=0., name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
# x is a rank 2 tensor variable with shape [100, 224]. # x is a rank 2 tensor variable
# out will be a tensor of shape [101, 227]
import paddle.fluid as fluid import paddle.fluid as fluid
x = fluid.data(name='data', shape=[100, 224], dtype='float32') x = fluid.data(name='data', shape=[300, 300], dtype='float32')
out = fluid.layers.pad( out = fluid.layers.pad(x=x, paddings=[0, 1, 1, 2], pad_value=0.)
x=x, paddings=[0, 1, 1, 2], pad_value=0.)
""" """
helper = LayerHelper('pad', input=x, **locals()) helper = LayerHelper('pad', input=x, **locals())
dtype = helper.input_dtype() dtype = helper.input_dtype()
...@@ -6464,29 +6461,34 @@ def pad_constant_like(x, y, pad_value=0., name=None): ...@@ -6464,29 +6461,34 @@ def pad_constant_like(x, y, pad_value=0., name=None):
[27, 28, 29]], [27, 28, 29]],
[[30, 31, 32], [[30, 31, 32],
[33, 34, 35]]]] [33, 34, 35]]]]
X.shape = (2, 3, 2, 3) X.shape = (2, 3, 2, 3)
Y = [[[[35, 36, 37]], Y = [[[[35, 36, 37]],
[[38, 39, 40]], [[38, 39, 40]],
[[41, 42, 43]]]] [[41, 42, 43]]]]
Y.shape = (1, 3, 1, 3) Y.shape = (1, 3, 1, 3)
And
pad_value = -1, And
pad_value = 0.
Return: Return:
Out = [[[[35, 36, 37], Out = [[[[35, 36, 37],
[-1, -1, -1]], [ 0, 0, 0]],
[[38, 39, 40], [[38, 39, 40],
[-1, -1, -1]], [ 0, 0, 0]],
[[41, 42, 43], [[41, 42, 43],
[-1, -1, -1]]], [ 0, 0, 0]]],
[[[-1, -1, -1], [[[ 0, 0, 0],
[-1, -1, -1]], [ 0, 0, 0]],
[[-1, -1, -1], [[ 0, 0, 0],
[-1, -1, -1]], [ 0, 0, 0]],
[[-1, -1, -1], [[ 0, 0, 0],
[-1, -1, -1]]]] [ 0, 0, 0]]]]
Out.shape = (2, 3, 2, 3)
Out.shape = [2, 3, 2, 3]
Args: Args:
x (Variable): Tensor, its shape specifies the shape of output. x (Variable): Tensor, its shape specifies the shape of output.
...@@ -8743,47 +8745,37 @@ def pad2d(input, ...@@ -8743,47 +8745,37 @@ def pad2d(input,
Examples: Examples:
.. code-block:: text .. code-block:: text
Given that X is a channel of image from input: Input = [[[[1., 2., 3.],
[4., 5., 6.]]]]
X = [[1, 2, 3],
[4, 5, 6]] Case 0:
paddings = [0, 1, 2, 3],
Case 0: mode = 'constant'
pad_value = 0
paddings = [0, 1, 2, 3], Out = [[[[0., 0., 1., 2., 3., 0., 0., 0.],
mode = 'constant' [0., 0., 4., 5., 6., 0., 0., 0.],
pad_value = 0 [0., 0., 0., 0., 0., 0., 0., 0.]]]]
Out = [[0, 0, 1, 2, 3, 0, 0, 0] Case 1:
[0, 0, 4, 5, 6, 0, 0, 0] paddings = [0, 1, 2, 1],
[0, 0, 0, 0, 0, 0, 0, 0]] mode = 'reflect'
Out = [[[[3., 2., 1., 2., 3., 2.],
Case 1: [6., 5., 4., 5., 6., 5.],
[3., 2., 1., 2., 3., 2.]]]]
paddings = [0, 1, 2, 1],
mode = 'reflect' Case 2:
paddings = [0, 1, 2, 1],
Out = [[3, 2, 1, 2, 3, 2] mode = 'edge'
[6, 5, 4, 5, 6, 5] Out = [[[[1., 1., 1., 2., 3., 3.],
[3, 2, 1, 2, 3, 2]] [4., 4., 4., 5., 6., 6.],
[4., 4., 4., 5., 6., 6.]]]]
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 Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
data = fluid.data(name='data', shape=[None, 3, 32, 32], data = fluid.data(name='data', shape=[None, 3, 32, 32], dtype='float32')
dtype='float32') result = fluid.layers.pad2d(input=data, paddings=[0, 1, 2, 3], mode='reflect')
result = fluid.layers.pad2d(input=data, paddings=[1, 2, 3, 4],
mode='reflect')
""" """
if in_dygraph_mode(): if in_dygraph_mode():
......
...@@ -138,7 +138,7 @@ def create_global_var(shape, ...@@ -138,7 +138,7 @@ def create_global_var(shape,
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.layers as layers import paddle.fluid.layers as layers
var = layers.create_global_var(shape=[2,3], value=1.0, dtype='float32', 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()) helper = LayerHelper("global_var", **locals())
var = helper.create_global_variable( var = helper.create_global_variable(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册