Skip to content

Commit

Permalink
fix code example (PaddlePaddle#28636)
Browse files Browse the repository at this point in the history
* fix code example, test=document_fix
  • Loading branch information
baiyfbupt authored Nov 20, 2020
1 parent 8b853b3 commit 655d5eb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
7 changes: 6 additions & 1 deletion python/paddle/fluid/layers/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,15 @@ def square_error_cost(input, label):
input = paddle.to_tensor([1.1, 1.9])
label = paddle.to_tensor([1.0, 2.0])
output = paddle.nn.functional.square_error_cost(input, label)
print(output.numpy())
print(output)
# [0.01, 0.01]
"""
if in_dygraph_mode():
minus_out = core.ops.elementwise_sub(input, label)
square_out = core.ops.square(minus_out)
return square_out

check_variable_and_dtype(input, "input", ['float32', 'float64'],
'square_error_cost')
check_variable_and_dtype(label, "label", ['float32', 'float64'],
Expand Down
22 changes: 12 additions & 10 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5698,16 +5698,18 @@ def row_conv(input, future_context_size, param_attr=None, act=None):
${out_comment}.

Examples:
>>> # for LodTensor inputs
>>> import paddle.fluid as fluid
>>> import paddle
>>> paddle.enable_static()
>>> x = fluid.data(name='x', shape=[9, 16],
>>> dtype='float32', lod_level=1)
>>> out = fluid.layers.row_conv(input=x, future_context_size=2)
>>> # for Tensor inputs
>>> x = fluid.data(name='x', shape=[9, 4, 16], dtype='float32')
>>> out = fluid.layers.row_conv(input=x, future_context_size=2)

.. code-block:: python

# for LodTensor inputs
import paddle
paddle.enable_static()
x = paddle.static.data(name='x', shape=[9, 16],
dtype='float32', lod_level=1)
out = paddle.static.nn.row_conv(input=x, future_context_size=2)
# for Tensor inputs
x = paddle.static.data(name='x', shape=[9, 4, 16], dtype='float32')
out = paddle.static.nn.row_conv(input=x, future_context_size=2)
"""
helper = LayerHelper('row_conv', **locals())
check_variable_and_dtype(input, 'input', ['float32'], 'row_conv')
Expand Down
23 changes: 1 addition & 22 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,32 +989,11 @@ def mse_loss(input, label, reduction='mean', name=None):
.. code-block:: python
import paddle
# static graph mode
paddle.enable_static()
mse_loss = paddle.nn.loss.MSELoss()
input = paddle.fluid.data(name="input", shape=[1])
label = paddle.fluid.data(name="label", shape=[1])
place = paddle.CPUPlace()
output = mse_loss(input,label)
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
output_data = exe.run(
paddle.static.default_main_program(),
feed={"input":input_data, "label":label_data},
fetch_list=[output],
return_numpy=True)
print(output_data)
# [array([0.04000002], dtype=float32)]
# dynamic graph mode
paddle.disable_static()
input = paddle.to_tensor(1.5)
label = paddle.to_tensor(1.7)
output = mse_loss(input, label)
print(output.numpy())
print(output)
# [0.04000002]
"""
Expand Down
4 changes: 3 additions & 1 deletion python/paddle/nn/functional/pooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ def adaptive_avg_pool1d(x, output_size, name=None):
ValueError: 'output_size' should be an integer.
Examples:
.. code-block:: python
# average adaptive pool1d
# suppose input data in shape of [N, C, L], `output_size` is m or [m],
# output shape is [N, C, m], adaptive pool divide L dimension
Expand Down Expand Up @@ -961,6 +962,7 @@ def adaptive_avg_pool2d(x, output_size, data_format='NCHW', name=None):
ValueError: If `data_format` is not "NCHW" or "NHWC".
Examples:
.. code-block:: python
# adaptive avg pool2d
# suppose input data in shape of [N, C, H, W], `output_size` is [m, n],
# output shape is [N, C, m, n], adaptive pool divide H and W dimensions
Expand Down Expand Up @@ -1062,6 +1064,7 @@ def adaptive_avg_pool3d(x, output_size, data_format='NCDHW', name=None):
ValueError: If `data_format` is not "NCDHW" or "NDHWC".
Examples:
.. code-block:: python
# adaptive avg pool3d
# suppose input data in shape of [N, C, D, H, W], `output_size` is [l, m, n],
# output shape is [N, C, l, m, n], adaptive pool divide D, H and W dimensions
Expand All @@ -1082,7 +1085,6 @@ def adaptive_avg_pool3d(x, output_size, data_format='NCDHW', name=None):
# avg(input[:, :, dstart:dend, hstart: hend, wstart: wend])
import paddle
import numpy as np
input_data = np.random.rand(2, 3, 8, 32, 32)
x = paddle.to_tensor(input_data)
# x.shape is [2, 3, 8, 32, 32]
Expand Down

0 comments on commit 655d5eb

Please sign in to comment.