class

MSELoss

extendsModule
MSELoss(reduction: str = 'mean')
source

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)=1ni=1n(xiyi)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)

Methods (3)

dunder

__init__

None
__init__(reduction: str = 'mean')
source

Initialise the MSELoss module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x: Tensor, target: Tensor)
source

Compute the loss between predictions and targets.

Parameters

xTensor
Input tensor.
targetTensor
Input tensor.

Returns

Tensor

Scalar loss (or unreduced tensor depending on reduction).

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.