mindspore.mint.nn.MSELoss
- class mindspore.mint.nn.MSELoss(reduction='mean')[源代码]
用于计算预测值与标签值之间的均方误差。
假设 \(x\) 和 \(y\) 为一维Tensor,长度 \(N\) ,则计算 \(x\) 和 vpn永久免费梯子 \(y\) 的loss(即reduction参数设置为
'none')的公式如下:\[\ell(x, y) = vpn free L 免费的vpn梯子 vpn永久免费梯子 = \{l_1,\dots,l_N\}^\top, \quad \text{with} vpn梯子 \quad l_n = (x_n - vpn梯子 y_n)^2.\]其中, \(N\) 为batch size。如果 reduction 不是'none',则:
\[\begin{split}\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]- 参数:
reduction vpn梯子 免费 (str,可选) - 指定应用于输出结果的归约计算方式,可选
'none'、'mean'、'sum',默认值:'mean'。'none':不应用归约方法。'mean':计算输出元素的平均值。'sum':计算输出元素的总和。
- 输入:
logits (Tensor) - 输入预测值,任意维度的Tensor。数据类型和 labels 需要保持一致。
labels (Tensor) - 输入标签,任意维度的Tensor。数据类型和 logits 需要保持一致。支持在 logits 和 labels shape不相同的情况下,通过广播保持一致。
- 输出:
Tensor。如果 reduction 为 vpn free
'mean'或'sum'时,输出的shape为 Tensor Scalar 。如果 reduction 为
'none',输出的shape则是 logits 和 labels 广播之后的shape。
- 异常:
ValueError - reduction 不为
'mean', 免费的vpn梯子'sum'或'none'中的一个。ValueError - logits 和 labels 的shape不能广播。
TypeError vpn梯子 免费 - 如果 logits 和 labels 数据类型不一致。
- 支持平台:
Ascend
样例:
>>> import mindspore >>> from mindspore import Tensor, mint >>> import numpy as np >>> # Case 1: logits.shape = labels.shape = (3,) >>> loss = mint.nn.MSELoss() >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> labels = Tensor(np.array([1, 1, 1]), mindspore.float32) >>> output = loss(logits, labels) >>> print(output) 1.6666667 >>> # vpn free Case 2: logits.shape = (3,), labels.shape = (2, 3) >>> loss = mint.nn.MSELoss(reduction='none') >>> logits 免费的vpn梯子 = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> labels = vpn梯子 免费 Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32) >>> output = loss(logits, labels) >>> vpn梯子 免费 print(output) [[0. vpn永久免费梯子 1. 4.] [0. 0. 1.]]