Loss Mse#

mse #

CLASS DESCRIPTION
MSELoss

Mean squared error loss with optional weighting.

FUNCTION DESCRIPTION
mse_loss

Compute mean squared error loss.

Classes#

MSELoss(reduce: Literal['sum', 'mean'] = 'mean', sign: float = 1.0, context_prefix: str | None = None, **kwargs) #

Bases: Loss

Mean squared error loss with optional weighting.

PARAMETER DESCRIPTION
reduce

Reduction method.

TYPE: Literal["sum", "mean"], optional, by default "mean" DEFAULT: 'mean'

sign

Loss sign multiplier.

TYPE: float, optional, by default 1.0 DEFAULT: 1.0

**kwargs

Additional keyword arguments.

TYPE: dict DEFAULT: {}

METHOD DESCRIPTION
forward

Compute mean squared error loss.

Source code in spectre/loss/mse.py
def __init__(
    self,
    reduce: Literal["sum", "mean"] = "mean",
    sign: float = 1.0,
    context_prefix: str | None = None,
    **kwargs,
):
    super().__init__(
        reduce=reduce, sign=sign, context_prefix=context_prefix, **kwargs
    )
Functions#
forward(context: dict, **kwargs) -> torch.Tensor #

Compute mean squared error loss.

PARAMETER DESCRIPTION
context

Context dictionary.

  • "X": torch.Tensor. Predicted values of shape (n_samples, n_features).
  • "Y": torch.Tensor. Target values of shape (n_samples, n_features).
  • "weights": torch.Tensor | None, optional. Sample weights of shape (n_samples, ).

TYPE: dict

Source code in spectre/loss/mse.py
def forward(self, context: dict, **kwargs) -> torch.Tensor:
    """
    Compute mean squared error loss.

    Parameters
    ----------
    context : dict
        Context dictionary.

        - "X": torch.Tensor. Predicted values of shape `(n_samples, n_features)`.
        - "Y": torch.Tensor. Target values of shape `(n_samples, n_features)`.
        - "weights": torch.Tensor | None, optional. Sample weights of shape
          `(n_samples, )`.
    """
    X = self._get_context(context, "X", None)
    Y = self._get_context(context, "Y", None)
    weights = self._get_context(context, "weights", None)

    if X is None or Y is None:
        raise ValueError(
            "`MSELoss` requires `X` and `Y` must be provided in the context."
        )

    if weights is None:
        weights = torch.ones(X.shape[0], device=X.device, dtype=X.dtype)

    return _mse_loss_impl(X, Y, weights, reduce=self.reduce, sign=self.sign)

Functions#

mse_loss(X: torch.Tensor, Z: torch.Tensor, weights: Optional[torch.Tensor] = None, reduce: str = 'mean', sign: float = 1.0) -> torch.Tensor #

Compute mean squared error loss.

PARAMETER DESCRIPTION
X

Predicted values.

TYPE: Tensor

Z

Target values.

TYPE: Tensor

weights

Sample weights.

TYPE: torch.Tensor | None, optional, by default None DEFAULT: None

reduce

Reduction method.

TYPE: Literal["sum", "mean"], optional, by default "mean" DEFAULT: 'mean'

sign

Loss sign multiplier.

TYPE: float, optional, by default 1.0 DEFAULT: 1.0

RETURNS DESCRIPTION
Tensor

MSE loss value.

Source code in spectre/loss/mse.py
def mse_loss(
    X: torch.Tensor,
    Z: torch.Tensor,
    weights: Optional[torch.Tensor] = None,
    reduce: str = "mean",
    sign: float = 1.0,
) -> torch.Tensor:
    """
    Compute mean squared error loss.

    Parameters
    ----------
    X : torch.Tensor
        Predicted values.

    Z : torch.Tensor
        Target values.

    weights : torch.Tensor | None, optional, by default None
        Sample weights.

    reduce : Literal["sum", "mean"], optional, by default "mean"
        Reduction method.

    sign : float, optional, by default 1.0
        Loss sign multiplier.

    Returns
    -------
    torch.Tensor
        MSE loss value.
    """
    check_same_shape(X, Z)

    if weights is not None:
        check_1d(weights)
        check_same_len(X, weights)
    else:
        weights = torch.ones(X.shape[0], device=X.device, dtype=X.dtype)

    if reduce not in ["sum", "mean"]:
        raise ValueError("Unknown reduce operation. Options: 'sum', 'mean'.")

    return _mse_loss_impl(X, Z, weights, reduce, sign)