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)Used by 1
Constructors
1Instance methods
2Return a string representation of the layer's configuration.