Skip to content

MeanSquaredError

minnt.metrics.MeanSquaredError

Bases: Mean

Mean squared error metric implementation.

Source code in minnt/metrics/mean_squared_error.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class MeanSquaredError(Mean):
    """Mean squared error metric implementation."""

    def __init__(self) -> None:
        """Create the MeanSquaredError metric object."""
        super().__init__()
        self._mse_loss = losses.MeanSquaredError(reduction="none")

    def update(
        self, y: torch.Tensor, y_true: torch.Tensor, sample_weights: torch.Tensor | None = None,
    ) -> None:
        """Update the accumulated mean squared error by introducing new values.

        Optional sample weight might be provided; if not, all values are weighted with 1.

        Parameters:
          y: The predicted outputs. Their shape either has to be exactly the same as `y_true` (no broadcasting),
            or can contain an additional single dimension of size 1.
          y_true: The ground-truth targets.
          sample_weights: Optional sample weights. If provided, their shape must be broadcastable
            to a prefix of a shape of `y_true`, and the loss for each sample is weighted accordingly.
        """
        super().update(self._mse_loss(y, y_true), sample_weights=sample_weights)

__init__

__init__() -> None

Create the MeanSquaredError metric object.

Source code in minnt/metrics/mean_squared_error.py
15
16
17
18
def __init__(self) -> None:
    """Create the MeanSquaredError metric object."""
    super().__init__()
    self._mse_loss = losses.MeanSquaredError(reduction="none")

update

update(y: Tensor, y_true: Tensor, sample_weights: Tensor | None = None) -> None

Update the accumulated mean squared error by introducing new values.

Optional sample weight might be provided; if not, all values are weighted with 1.

Parameters:

  • y (Tensor) –

    The predicted outputs. Their shape either has to be exactly the same as y_true (no broadcasting), or can contain an additional single dimension of size 1.

  • y_true (Tensor) –

    The ground-truth targets.

  • sample_weights (Tensor | None, default: None ) –

    Optional sample weights. If provided, their shape must be broadcastable to a prefix of a shape of y_true, and the loss for each sample is weighted accordingly.

Source code in minnt/metrics/mean_squared_error.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def update(
    self, y: torch.Tensor, y_true: torch.Tensor, sample_weights: torch.Tensor | None = None,
) -> None:
    """Update the accumulated mean squared error by introducing new values.

    Optional sample weight might be provided; if not, all values are weighted with 1.

    Parameters:
      y: The predicted outputs. Their shape either has to be exactly the same as `y_true` (no broadcasting),
        or can contain an additional single dimension of size 1.
      y_true: The ground-truth targets.
      sample_weights: Optional sample weights. If provided, their shape must be broadcastable
        to a prefix of a shape of `y_true`, and the loss for each sample is weighted accordingly.
    """
    super().update(self._mse_loss(y, y_true), sample_weights=sample_weights)