-
Notifications
You must be signed in to change notification settings - Fork 390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Newmetric: NRMSE #2442
Open
SkafteNicki
wants to merge
19
commits into
master
Choose a base branch
from
newmetric/nrmse
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Newmetric: NRMSE #2442
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
aa1457f
init files
SkafteNicki 58b7577
docs
SkafteNicki 7933f61
requirements for testing
SkafteNicki ba8848a
changelog
SkafteNicki 0bf7f56
add class interface
SkafteNicki b6c7011
add tests
SkafteNicki 50e65a5
Merge branch 'master' into newmetric/nrmse
Borda 1100cda
Merge branch 'master' into newmetric/nrmse
Borda 8b962d0
Merge branch 'master' into newmetric/nrmse
SkafteNicki a95809b
Merge branch 'master' into newmetric/nrmse
Borda 9a40d9b
Merge branch 'master' into newmetric/nrmse
SkafteNicki 642dd27
Update NRMSE computation with normalization options
SkafteNicki b5a2e42
Merge branch 'master' into newmetric/nrmse
SkafteNicki 84604ad
try fixing docs
SkafteNicki 0271d91
fix mypy
SkafteNicki d279774
fix naming of file
SkafteNicki c58fe84
Merge branch 'master' into newmetric/nrmse
Borda d9b6783
Merge branch 'master' into newmetric/nrmse
Borda 42ffbea
Apply suggestions from code review
Borda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
docs/source/regression/normalized_root_mean_squared_error.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.. customcarditem:: | ||
:header: Normalized Root Mean Squared Error (NRMSE) | ||
:image: https://pl-flash-data.s3.amazonaws.com/assets/thumbnails/tabular_classification.svg | ||
:tags: Regression | ||
|
||
.. include:: ../links.rst | ||
|
||
########################################## | ||
Normalized Root Mean Squared Error (NRMSE) | ||
########################################## | ||
|
||
Module Interface | ||
________________ | ||
|
||
.. autoclass:: torchmetrics.NormalizedRootMeanSquaredError | ||
:exclude-members: update, compute | ||
|
||
Functional Interface | ||
____________________ | ||
|
||
.. autofunction:: torchmetrics.functional.normalized_root_mean_squared_error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ | |
-r classification_test.txt | ||
-r nominal_test.txt | ||
-r segmentation_test.txt | ||
-r regression_test.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
permetrics==2.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Copyright The Lightning team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Tuple, Union | ||
|
||
import torch | ||
from torch import Tensor | ||
from typing_extensions import Literal | ||
|
||
from torchmetrics.functional.regression.mse import _mean_squared_error_update | ||
|
||
|
||
def _normalized_root_mean_squared_error_update( | ||
preds: Tensor, target: Tensor, num_outputs: int, normalization: Literal["mean", "range", "std"] = "mean" | ||
) -> Tuple[Tensor, int, Tensor]: | ||
"""Updates and returns the sum of squared errors and the number of observations for NRMSE computation. | ||
|
||
Args: | ||
preds: Predicted tensor | ||
target: Ground truth tensor | ||
num_outputs: Number of outputs in multioutput setting | ||
normalization: type of normalization to be applied. Choose from "mean", "range", "std" | ||
|
||
""" | ||
sum_squared_error, num_obs = _mean_squared_error_update(preds, target, num_outputs) | ||
|
||
target = target.view(-1) if num_outputs == 1 else target | ||
if normalization == "mean": | ||
denom = torch.mean(target, dim=0) | ||
elif normalization == "range": | ||
denom = torch.max(target, dim=0).values - torch.min(target, dim=0).values | ||
elif normalization == "std": | ||
denom = torch.std(target, correction=0, dim=0) | ||
else: | ||
raise ValueError(f"Argument `normalization` should be either 'mean', 'range' or 'std', but got {normalization}") | ||
return sum_squared_error, num_obs, denom | ||
|
||
|
||
def _normalized_root_mean_squared_error_compute( | ||
sum_squared_error: Tensor, num_obs: Union[int, Tensor], denom: Tensor | ||
) -> Tensor: | ||
"""Calculates RMSE and normalizes it.""" | ||
rmse = torch.sqrt(sum_squared_error / num_obs) | ||
return rmse / denom | ||
|
||
|
||
def normalized_root_mean_squared_error( | ||
preds: Tensor, | ||
target: Tensor, | ||
normalization: Literal["mean", "range", "std"] = "mean", | ||
num_outputs: int = 1, | ||
) -> Tensor: | ||
"""Calculates the `Normalized Root Mean Squared Error`_ (NRMSE) also know as scatter index. | ||
|
||
Args: | ||
preds: estimated labels | ||
target: ground truth labels | ||
normalization: type of normalization to be applied. Choose from "mean", "range", "std" which corresponds to | ||
normalizing the RMSE by the mean of the target, the range of the target or the standard deviation of the | ||
target. | ||
num_outputs: Number of outputs in multioutput setting | ||
|
||
Return: | ||
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) | ||
|
||
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]) | ||
|
||
""" | ||
sum_squared_error, num_obs, denom = _normalized_root_mean_squared_error_update( | ||
preds, target, num_outputs=num_outputs, normalization=normalization | ||
) | ||
return _normalized_root_mean_squared_error_compute(sum_squared_error, num_obs, denom) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall this be another PR?