Shortcuts

torcheval.metrics.functional.mean_squared_error

torcheval.metrics.functional.mean_squared_error(input: Tensor, target: Tensor, *, sample_weight: Tensor | None = None, multioutput: str = 'uniform_average') Tensor

Compute Mean Squared Error, which is the mean of squared error of input and target Its class version is torcheval.metrics.MeanSquaredError.

Parameters:
  • input (Tensor) – Tensor of predicted values with shape of (n_sample, n_output).

  • target (Tensor) – Tensor of ground truth values with shape of (n_sample, n_output).

  • sample_weight (Optional) – Tensor of sample weights with shape of (n_sample, ). Defaults to None.

  • multioutput (Optional) –

    • 'uniform_average' [default]:

      Return scores of all outputs are averaged with uniform weight.

    • 'raw_values':

      Return a full set of scores.

Raises:

ValueError

  • If value of multioutput does not exist in (raw_values, uniform_average). - If the dimension of input or target is not 1D or 2D. - If the input and target do not have the same size. - If the first dimension of input, target and sample_weight are not the same.

Examples:

>>> import torch
>>> from torcheval.metrics.function import mean_squared_error
>>> input = torch.tensor([0.9, 0.5, 0.3, 0.5])
>>> target = torch.tensor([0.5, 0.8, 0.2, 0.8])
>>> mean_squared_error(input, target)
tensor(0.0875)

>>> input = torch.tensor([[0.9, 0.5], [0.3, 0.5]])
>>> target = torch.tensor([[0.5, 0.8], [0.2, 0.8]])
>>> mean_squared_error(input, target)
tensor(0.0875)

>>> input = torch.tensor([[0.9, 0.5], [0.3, 0.5]])
>>> target = torch.tensor([[0.5, 0.8], [0.2, 0.8]])
>>> mean_squared_error(input, target, multioutput="raw_values")
tensor([0.0850, 0.0900])

>>> input = torch.tensor([[0.9, 0.5], [0.3, 0.5]])
>>> target = torch.tensor([[0.5, 0.8], [0.2, 0.8]])
>>> mean_squared_error(input, target, sample_weight=torch.tensor([0.2, 0.8]))
tensor(0.0650)

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources