Normalized Root Mean Squared Error (NRMSE)¶
Module Interface¶
- class torchmetrics.NormalizedRootMeanSquaredError(normalization='mean', num_outputs=1, **kwargs)[source]¶
Calculates the Normalized Root Mean Squared Error (NRMSE) also know as scatter index.
The metric is defined as:
\[\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
andupdate
the metric accepts the following input:As output of
forward
andcompute
the metric returns the following output:nrmse
(Tensor
): A tensor with the mean squared error
- Parameters:
normalization¶ (
Literal
['mean'
,'range'
,'std'
,'l2'
]) – 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¶ (
int
) – Number of outputs in multioutput settingkwargs¶ (
Any
) – Additional keyword arguments, see Advanced metric settings 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])
- plot(val=None, ax=None)[source]¶
Plot a single or multiple values from the metric.
- Parameters:
val¶ (
Union
[Tensor
,Sequence
[Tensor
],None
]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.ax¶ (
Optional
[Axes
]) – An matplotlib axis object. If provided will add plot to that axis
- Return type:
- Returns:
Figure and Axes object
- Raises:
ModuleNotFoundError – If matplotlib is not installed
>>> from torch import randn >>> # Example plotting a single value >>> from torchmetrics.regression import NormalizedRootMeanSquaredError >>> metric = NormalizedRootMeanSquaredError() >>> metric.update(randn(10,), randn(10,)) >>> fig_, ax_ = metric.plot()
>>> from torch import randn >>> # Example plotting multiple values >>> from torchmetrics.regression import NormalizedRootMeanSquaredError >>> metric = NormalizedRootMeanSquaredError() >>> values = [] >>> for _ in range(10): ... values.append(metric(randn(10,), randn(10,))) >>> fig, ax = metric.plot(values)
Functional Interface¶
- torchmetrics.functional.normalized_root_mean_squared_error(preds, target, normalization='mean', num_outputs=1)[source]¶
Calculates the Normalized Root Mean Squared Error (NRMSE) also know as scatter index.
- Parameters:
normalization¶ (
Literal
['mean'
,'range'
,'std'
,'l2'
]) – 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¶ (
int
) – Number of outputs in multioutput setting
- Return type:
- Returns:
Tensor with the NRMSE score
Example
>>> import torch >>> from torchmetrics.functional.regression import normalized_root_mean_squared_error >>> preds = torch.tensor([0., 1, 2, 3]) >>> target = torch.tensor([0., 1, 2, 2]) >>> normalized_root_mean_squared_error(preds, target, normalization="mean") tensor(0.4000) >>> normalized_root_mean_squared_error(preds, target, normalization="range") tensor(0.2500) >>> normalized_root_mean_squared_error(preds, target, normalization="std") tensor(0.6030) >>> normalized_root_mean_squared_error(preds, target, normalization="l2") tensor(0.1667)
- Example (multioutput):
>>> import torch >>> from torchmetrics.functional.regression import normalized_root_mean_squared_error >>> preds = torch.tensor([[0., 1], [2, 3], [4, 5], [6, 7]]) >>> target = torch.tensor([[0., 1], [3, 3], [4, 5], [8, 9]]) >>> normalized_root_mean_squared_error(preds, target, normalization="mean", num_outputs=2) tensor([0.2981, 0.2222])