From 655d5eb1db6caa598b0850a3b16d4a819fb4ab67 Mon Sep 17 00:00:00 2001 From: Bai Yifan Date: Fri, 20 Nov 2020 16:18:55 +0800 Subject: [PATCH] fix code example (#28636) * fix code example, test=document_fix --- python/paddle/fluid/layers/loss.py | 7 ++++++- python/paddle/fluid/layers/nn.py | 22 ++++++++++++---------- python/paddle/nn/functional/loss.py | 23 +---------------------- python/paddle/nn/functional/pooling.py | 4 +++- 4 files changed, 22 insertions(+), 34 deletions(-) diff --git a/python/paddle/fluid/layers/loss.py b/python/paddle/fluid/layers/loss.py index b363c37f64b..99801514f47 100644 --- a/python/paddle/fluid/layers/loss.py +++ b/python/paddle/fluid/layers/loss.py @@ -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'], diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 93a0ff4287c..755356ac4c9 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -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') diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index c701274dbd0..4539ceb6c76 100644 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -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] """ diff --git a/python/paddle/nn/functional/pooling.py b/python/paddle/nn/functional/pooling.py index 69608afc6e0..0278a22e6f1 100755 --- a/python/paddle/nn/functional/pooling.py +++ b/python/paddle/nn/functional/pooling.py @@ -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 @@ -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 @@ -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 @@ -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] -- GitLab