fn
mse_loss
→Tensormse_loss(x: Tensor, target: Tensor, reduction: str = 'mean')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
xTensorPredicted values, any shape.
targetTensorTarget values; must be broadcast-compatible with
x.reductionstr= 'mean'"mean" (default), "sum", or "none".Returns
TensorScalar loss for "mean" / "sum", or a per-element
tensor with x's shape for "none".
Notes
Per-element loss:
With reduction:
The gradient w.r.t. x is 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)