未验证 提交 655d5eb1 编写于 作者: B Bai Yifan 提交者: GitHub

fix code example (#28636)

* fix code example, test=document_fix
上级 8b853b30
...@@ -329,10 +329,15 @@ def square_error_cost(input, label): ...@@ -329,10 +329,15 @@ def square_error_cost(input, label):
input = paddle.to_tensor([1.1, 1.9]) input = paddle.to_tensor([1.1, 1.9])
label = paddle.to_tensor([1.0, 2.0]) label = paddle.to_tensor([1.0, 2.0])
output = paddle.nn.functional.square_error_cost(input, label) output = paddle.nn.functional.square_error_cost(input, label)
print(output.numpy()) print(output)
# [0.01, 0.01] # [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'], check_variable_and_dtype(input, "input", ['float32', 'float64'],
'square_error_cost') 'square_error_cost')
check_variable_and_dtype(label, "label", ['float32', 'float64'], check_variable_and_dtype(label, "label", ['float32', 'float64'],
......
...@@ -5698,16 +5698,18 @@ def row_conv(input, future_context_size, param_attr=None, act=None): ...@@ -5698,16 +5698,18 @@ def row_conv(input, future_context_size, param_attr=None, act=None):
${out_comment}. ${out_comment}.
Examples: Examples:
>>> # for LodTensor inputs
>>> import paddle.fluid as fluid .. code-block:: python
>>> import paddle
>>> paddle.enable_static() # for LodTensor inputs
>>> x = fluid.data(name='x', shape=[9, 16], import paddle
>>> dtype='float32', lod_level=1) paddle.enable_static()
>>> out = fluid.layers.row_conv(input=x, future_context_size=2) x = paddle.static.data(name='x', shape=[9, 16],
>>> # for Tensor inputs dtype='float32', lod_level=1)
>>> x = fluid.data(name='x', shape=[9, 4, 16], dtype='float32') out = paddle.static.nn.row_conv(input=x, future_context_size=2)
>>> out = fluid.layers.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()) helper = LayerHelper('row_conv', **locals())
check_variable_and_dtype(input, 'input', ['float32'], 'row_conv') check_variable_and_dtype(input, 'input', ['float32'], 'row_conv')
......
...@@ -989,32 +989,11 @@ def mse_loss(input, label, reduction='mean', name=None): ...@@ -989,32 +989,11 @@ def mse_loss(input, label, reduction='mean', name=None):
.. code-block:: python .. code-block:: python
import paddle import paddle
# static graph mode
paddle.enable_static()
mse_loss = paddle.nn.loss.MSELoss() 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) input = paddle.to_tensor(1.5)
label = paddle.to_tensor(1.7) label = paddle.to_tensor(1.7)
output = mse_loss(input, label) output = mse_loss(input, label)
print(output.numpy()) print(output)
# [0.04000002] # [0.04000002]
""" """
......
...@@ -887,6 +887,7 @@ def adaptive_avg_pool1d(x, output_size, name=None): ...@@ -887,6 +887,7 @@ def adaptive_avg_pool1d(x, output_size, name=None):
ValueError: 'output_size' should be an integer. ValueError: 'output_size' should be an integer.
Examples: Examples:
.. code-block:: python .. code-block:: python
# average adaptive pool1d # average adaptive pool1d
# suppose input data in shape of [N, C, L], `output_size` is m or [m], # 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 # output shape is [N, C, m], adaptive pool divide L dimension
...@@ -961,6 +962,7 @@ def adaptive_avg_pool2d(x, output_size, data_format='NCHW', name=None): ...@@ -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". ValueError: If `data_format` is not "NCHW" or "NHWC".
Examples: Examples:
.. code-block:: python .. code-block:: python
# adaptive avg pool2d # adaptive avg pool2d
# suppose input data in shape of [N, C, H, W], `output_size` is [m, n], # 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 # output shape is [N, C, m, n], adaptive pool divide H and W dimensions
...@@ -1062,6 +1064,7 @@ def adaptive_avg_pool3d(x, output_size, data_format='NCDHW', name=None): ...@@ -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". ValueError: If `data_format` is not "NCDHW" or "NDHWC".
Examples: Examples:
.. code-block:: python .. code-block:: python
# adaptive avg pool3d # adaptive avg pool3d
# suppose input data in shape of [N, C, D, H, W], `output_size` is [l, m, n], # 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 # output shape is [N, C, l, m, n], adaptive pool divide D, H and W dimensions
...@@ -1082,7 +1085,6 @@ def adaptive_avg_pool3d(x, output_size, data_format='NCDHW', name=None): ...@@ -1082,7 +1085,6 @@ def adaptive_avg_pool3d(x, output_size, data_format='NCDHW', name=None):
# avg(input[:, :, dstart:dend, hstart: hend, wstart: wend]) # avg(input[:, :, dstart:dend, hstart: hend, wstart: wend])
import paddle import paddle
import numpy as np import numpy as np
input_data = np.random.rand(2, 3, 8, 32, 32) input_data = np.random.rand(2, 3, 8, 32, 32)
x = paddle.to_tensor(input_data) x = paddle.to_tensor(input_data)
# x.shape is [2, 3, 8, 32, 32] # x.shape is [2, 3, 8, 32, 32]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册