未验证 提交 5acbba5b 编写于 作者: B Bai Yifan 提交者: GitHub

Fix MSELoss ch doc (#2339)

* fix MSELoss ch doc

* support pact demo load checkpoints

* fix example code
上级 60433a19
MSELoss
-------------------------------
.. py:function:: paddle.nn.loss.MSELoss(input,label)
.. py:function:: paddle.nn.loss.MSELoss(reduction='mean')
该OP用于计算预测值和目标值的均方差误差。
......@@ -23,13 +23,15 @@ MSELoss
Out = \operatorname{sum}((input - label)^2)
参数:
- **input** (Variable) - 预测值,维度为 :math:`[N_1, N_2, ..., N_k, D]` 的多维Tensor,其中最后一维D是类别数目。数据类型为float32或float64。
- **label** (Variable) - 目标值,维度为 :math:`[N_1, N_2, ..., N_k, D]` 的多维Tensor,其中最后一维D是类别数目。数据类型为float32或float64。
- **reduction** (str, 可选) - 约简方式,可以是 'none' | 'mean' | 'sum'。设为'none'时不使用约简,设为'mean'时返回loss的均值,设为'sum'时返回loss的和。
返回:预测值和目标值的均方差
形状:
- **input** (Tensor) - 预测值,维度为 :math:`[N_1, N_2, ..., N_k]` 的多维Tensor。数据类型为float32或float64。
- **label** (Tensor) - 目标值,维度为 :math:`[N_1, N_2, ..., N_k]` 的多维Tensor。数据类型为float32或float64。
返回:变量(Tensor), 预测值和目标值的均方差, 数值类型与输入相同
返回类型:变量(Variable)
**代码示例**:
......@@ -37,32 +39,32 @@ MSELoss
import numpy as np
import paddle
from paddle import fluid
import paddle.fluid.dygraph as dg
# static graph mode
paddle.enable_static()
mse_loss = paddle.nn.loss.MSELoss()
input = fluid.data(name="input", shape=[1])
label = fluid.data(name="label", shape=[1])
place = fluid.CPUPlace()
input = paddle.data(name="input", shape=[1])
label = paddle.data(name="label", shape=[1])
place = paddle.CPUPlace()
input_data = np.array([1.5]).astype("float32")
label_data = np.array([1.7]).astype("float32")
# declarative mode
output = mse_loss(input,label)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
output_data = exe.run(
fluid.default_main_program(),
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)]
# imperative mode
with dg.guard(place) as g:
input = dg.to_variable(input_data)
label = dg.to_variable(label_data)
output = mse_loss(input, label)
print(output.numpy())
# [0.04000002]
# dynamic graph mode
paddle.disable_static()
input = paddle.to_variable(input_data)
label = paddle.to_variable(label_data)
output = mse_loss(input, label)
print(output.numpy())
# [0.04000002]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册