class
MSELoss
extends
ModuleMSELoss(reduction: str = 'mean')Mean squared error (MSE) loss between each element of the prediction and the target.
This is the canonical regression loss that penalises large deviations
quadratically. With reduction='mean' the per-element squared
differences are averaged over all elements:
With reduction='sum' the sum is taken instead, and with
reduction='none' the full element-wise tensor is returned unchanged.
Parameters
reductionstr= 'mean'Specifies the reduction to apply to the output.
'none' — no reduction, element-wise output.
'mean' — average over all elements (default).
'sum' — sum over all elements.Attributes
reductionstrThe reduction mode set at construction time.
Notes
- Input
x: — any shape. - Target
y: — same shape asx. - Output : scalar when
reductionis'mean'or'sum'; whenreduction='none'.
- Gradients scale linearly with the residual magnitude, which can
make training sensitive to outliers and large-scale targets.
Consider
HuberLossorL1Losswhen outliers are present. - MSE is proportional to the negative log-likelihood under a Gaussian observation model with unit variance.
Examples
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.MSELoss()
>>> x = lucid.tensor([2.5, 0.0, 2.0, 8.0])
>>> y = lucid.tensor([3.0, -0.5, 2.0, 7.0])
>>> loss = criterion(x, y) # scalar
Element-wise output with ``reduction='none'``:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.MSELoss(reduction="none")
>>> x = lucid.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> y = lucid.tensor([[1.5, 1.5], [2.5, 4.5]])
>>> loss = criterion(x, y) # shape (2, 2)Methods (3)
dunder
__init__
→None__init__(reduction: str = 'mean')Initialise the MSELoss module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor, target: Tensor)Compute the loss between predictions and targets.
Parameters
xTensorInput tensor.
targetTensorInput tensor.
Returns
TensorScalar loss (or unreduced tensor depending on reduction).
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.