Skip to content
class

MSELoss

extends_Loss
MSELoss(size_average: bool | None = None, reduce: bool | None = None, reduction: Reduction = '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:

L(x,y)=1n∑i=1n(xi−yi)2\mathcal{L}(x, y) = \frac{1}{n} \sum_{i=1}^{n} (x_i - y_i)^2

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

reductionstr
The reduction mode set at construction time.

Notes

  • Input x : (∗)(*) — any shape.
  • Target y : (∗)(*) — same shape as x.
  • Output : scalar when reduction is 'mean' or 'sum'; (∗)(*) when reduction='none'.
  • Gradients scale linearly with the residual magnitude, which can make training sensitive to outliers and large-scale targets. Consider HuberLoss or L1Loss when 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

Instance methods

2
fn

extra_repr

→strfrom _Loss
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor, target: Tensor)

Compute the loss between predictions and targets.

Parameters

inputTensor
Input tensor.
targetTensor
Input tensor.

Returns

Tensor

Scalar loss (or unreduced tensor depending on reduction).