fn

mse_loss

Tensor
mse_loss(x: Tensor, target: Tensor, reduction: str = 'mean')
source

Mean-squared-error (L2) loss between input and target.

The workhorse loss for regression problems. Penalises large errors quadratically, which makes it sensitive to outliers but yields a well-conditioned optimisation surface — the gradient is linear in the residual, so SGD updates scale gracefully near the optimum. Compare with l1_loss (constant gradient, robust to outliers) and huber_loss (a smooth blend of the two).

Parameters

xTensor
Predicted values, any shape.
targetTensor
Target values; must be broadcast-compatible with x.
reductionstr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar loss for "mean" / "sum", or a per-element tensor with x's shape for "none".

Notes

Per-element loss:

Li=(xiyi)2L_i = (x_i - y_i)^2

With reduction:

L={1NiLi‘mean”iLi‘sum”Li‘none”L = \begin{cases} \tfrac{1}{N}\sum_i L_i & \text{`mean''} \\ \sum_i L_i & \text{`sum''} \\ L_i & \text{`none''} \end{cases}

The gradient w.r.t. x is 2(xiyi)/N2(x_i - y_i) / N under `"mean"`` reduction — proportional to the error, so updates naturally shrink near the optimum.

Examples

>>> import lucid
>>> from lucid.nn.functional import mse_loss
>>> pred = lucid.tensor([1.0, 2.0, 3.0])
>>> target = lucid.tensor([1.5, 2.5, 2.5])
>>> mse_loss(pred, target)
Tensor(0.25)