未验证 提交 ddcdd4a7 编写于 作者: Y Yang Zhang 提交者: GitHub

Minor fix to `MSELoss` docstring (#24077)

* Indent MSELoss example docs

* Point out input tensors should be of same shape

test=develop

* Document `MSELoss` input and return parameters

test=develop,test=document_fix
上级 3d744162
...@@ -155,48 +155,58 @@ class MSELoss(fluid.dygraph.layers.Layer): ...@@ -155,48 +155,58 @@ class MSELoss(fluid.dygraph.layers.Layer):
.. math:: .. math::
Out = \operatorname{sum}((input - label)^2) Out = \operatorname{sum}((input - label)^2)
where `input` and `label` are `float32` tensors of arbitrary shapes. where `input` and `label` are `float32` tensors of same shape.
Parameters: Parameters:
input (Variable): Input tensor, the data type is float32,
label (Variable): Label tensor, the data type is float32,
reduction (string, optional): The reduction method for the output, reduction (string, optional): The reduction method for the output,
could be 'none' | 'mean' | 'sum'. could be 'none' | 'mean' | 'sum'.
'none': no reduction will be applied If :attr:`reduction` is ``'mean'``, the reduced mean loss is returned.
'mean': the output will be averaged If :attr:`size_average` is ``'sum'``, the reduced sum loss is returned.
'sum': the output will be summed If :attr:`reduction` is ``'none'``, the unreduced loss is returned.
Default is ``'mean'``.
Returns:
The tensor variable storing the MSE loss of input and label.
Return type:
Variable.
Examples: Examples:
.. code-block:: python .. code-block:: python
import numpy as np
import paddle import numpy as np
from paddle import fluid import paddle
import paddle.fluid.dygraph as dg from paddle import fluid
import paddle.fluid.dygraph as dg
mse_loss = paddle.nn.loss.MSELoss()
input = fluid.data(name="input", shape=[1]) mse_loss = paddle.nn.loss.MSELoss()
label = fluid.data(name="label", shape=[1]) input = fluid.data(name="input", shape=[1])
place = fluid.CPUPlace() label = fluid.data(name="label", shape=[1])
input_data = np.array([1.5]).astype("float32") place = fluid.CPUPlace()
label_data = np.array([1.7]).astype("float32") input_data = np.array([1.5]).astype("float32")
label_data = np.array([1.7]).astype("float32")
# declarative mode
output = mse_loss(input,label) # declarative mode
exe = fluid.Executor(place) output = mse_loss(input,label)
exe.run(fluid.default_startup_program()) exe = fluid.Executor(place)
output_data = exe.run( exe.run(fluid.default_startup_program())
fluid.default_main_program(), output_data = exe.run(
feed={"input":input_data, "label":label_data}, fluid.default_main_program(),
fetch_list=[output], feed={"input":input_data, "label":label_data},
return_numpy=True) fetch_list=[output],
print(output_data) return_numpy=True)
# [array([0.04000002], dtype=float32)] print(output_data)
# [array([0.04000002], dtype=float32)]
# imperative mode
with dg.guard(place) as g: # imperative mode
input = dg.to_variable(input_data) with dg.guard(place) as g:
label = dg.to_variable(label_data) input = dg.to_variable(input_data)
output = mse_loss(input, label) label = dg.to_variable(label_data)
print(output.numpy()) output = mse_loss(input, label)
# [0.04000002] print(output.numpy())
# [0.04000002]
""" """
def __init__(self, reduction='mean'): def __init__(self, reduction='mean'):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册