normalizedrootmeansquarederror
Metric
NormalizedRootMeanSquaredErrorfromtorchmetrics(torchmetrics.NormalizedRootMeanSquaredError)
When to invoke this skill
The user has predictions + ground truth and asks to evaluate with NormalizedRootMeanSquaredError, or
mentions torchmetrics.NormalizedRootMeanSquaredError directly, or wants the standard torchmetrics implementation.
Reference signature
from torchmetrics import NormalizedRootMeanSquaredError
# NormalizedRootMeanSquaredError(normalization: Literal['mean', 'range', 'std', 'l2'] = 'mean', num_outputs: int = 1, **kwargs: Any) -> None
Library docstring
Calculates the `Normalized Root Mean Squared Error`_ (NRMSE) also know as scatter index.
The metric is defined as:
.. math::
\text{NRMSE} = \frac{\text{RMSE}}{\text{denom}}
where RMSE is the root mean squared error and `denom` is the normalization factor. The normalization factor can be
either be the mean, range, standard deviation or L2 norm of the target, which can be set using the `normalization`
argument.
As input to ``forward`` and ``update`` the metric accepts the following input:
- ``preds`` (:class:`~torch.Tensor`): Predictions from model
- ``target`` (:class:`~torch.Tensor`): Ground truth values
As output of ``forward`` and ``compute`` the metric returns the following output:
- ``nrmse`` (:class:`~torch.Tensor`): A tensor with the mean squared error
Args:
normalization: type of normalization to be applied. Choose from "mean", "range", "std", "l2" which corresponds
to normalizing the RMSE by the mean of the target, the range of the target, the standard deviation of the
target or the L2 norm of the target.
num_outputs: Number of outputs in multioutput setting
kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
Example::
Single output normalized root mean squared error computation:
>>> import torch
>>> from torchmetrics import NormalizedRootMeanSquaredError
>>> target = torch.tensor([2.5, 5.0, 4.0, 8.0])
>>> preds = torch.tensor([3.0, 5.0, 2.5, 7.0])
>>> nrmse = NormalizedRootMeanSquaredError(normalization="mean")
>>> nrmse(preds, target)
tensor(0.1919)
>>> nrmse = NormalizedRootMeanSquaredError(normalization="range")
>>> nrmse(preds, target)
tensor(0.1701)
Example::
Multioutput normalized root mean squared error computation:
>>> import torch
>>> from torchmetrics import NormalizedRootMeanSquaredError
>>> preds = torch.tensor([[0., 1], [2, 3], [4, 5], [6, 7]])
>>> target = torch.tensor([[0., 1], [3, 3], [4, 5], [8, 9]])
>>> nrmse = NormalizedRootMeanSquaredError(num_outputs=2)
>>> nrmse(preds, target)
tensor([0.2981, 0.2222])
Quick recipe
import torchmetrics as _m
score = _m.NormalizedRootMeanSquaredError(y_true, y_pred)
Don'ts
- Don't reimplement when the library version handles edge cases (NaN, ties, empty inputs) better than a hand-rolled formula.
- Always check the library version's argument order — sklearn is
(y_true, y_pred)while torchmetrics is(preds, target).