megengine.functional.nn.square_loss¶
- square_loss(pred, label, reduction='mean')[源代码]¶
Calculates the mean squared error (squared L2 norm) between each element in the pred \(x\) and label \(y\).
The mean squared error can be described as:
\[\ell(x, y) = mean\left( L \right)\]where
\[L = \{l_1,\dots,l_N\}, \quad l_n = \left( x_n - y_n \right)^2,\]\(x\) and \(y\) are tensors of arbitrary shapes with a total of \(N\) elements each. \(N\) is the batch size.
- 参数
- 返回类型
- 返回
loss value.
- Shape:
pred: \((N, *)\) where \(*\) means any number of additional dimensions.
label: \((N, *)\). Same shape as
pred
.
实际案例
import numpy as np import megengine as mge import megengine.functional as F ipt = mge.tensor(np.array([3, 3, 3, 3]).astype(np.float32)) tgt = mge.tensor(np.array([2, 8, 6, 1]).astype(np.float32)) loss = F.nn.square_loss(ipt, tgt) print(loss.numpy())
Outputs:
9.75