From 0f8138411360e65c9fbb4192a355205f40126cf1 Mon Sep 17 00:00:00 2001 From: pranjaldatta Date: Fri, 14 May 2021 18:26:59 +0530 Subject: [PATCH 01/20] initial code, test failing --- tests/regression/test_mean_error.py | 20 ++++++- torchmetrics/functional/__init__.py | 3 + .../functional/regression/__init__.py | 3 + .../mean_absolute_percentage_error.py | 53 +++++++++++++++++ torchmetrics/regression/__init__.py | 1 + .../mean_absolute_percentage_error.py | 58 +++++++++++++++++++ 6 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 torchmetrics/functional/regression/mean_absolute_percentage_error.py create mode 100644 torchmetrics/regression/mean_absolute_percentage_error.py diff --git a/tests/regression/test_mean_error.py b/tests/regression/test_mean_error.py index 7009d4fb71b..673f4e1cb18 100644 --- a/tests/regression/test_mean_error.py +++ b/tests/regression/test_mean_error.py @@ -19,11 +19,22 @@ from sklearn.metrics import mean_absolute_error as sk_mean_absolute_error from sklearn.metrics import mean_squared_error as sk_mean_squared_error from sklearn.metrics import mean_squared_log_error as sk_mean_squared_log_error +from sklearn.metrics import mean_absolute_percentage_error as sk_mean_abs_percentage_error from tests.helpers import seed_all from tests.helpers.testers import BATCH_SIZE, NUM_BATCHES, MetricTester -from torchmetrics.functional import mean_absolute_error, mean_squared_error, mean_squared_log_error -from torchmetrics.regression import MeanAbsoluteError, MeanSquaredError, MeanSquaredLogError +from torchmetrics.functional import ( + mean_absolute_error, + mean_squared_error, + mean_squared_log_error, + mean_absolute_percentage_error, +) +from torchmetrics.regression import ( + MeanAbsoluteError, + MeanSquaredError, + MeanSquaredLogError, + MeanAbsolutePercentageError, +) from torchmetrics.utilities.imports import _TORCH_GREATER_EQUAL_1_6 seed_all(42) @@ -68,6 +79,7 @@ def _multi_target_sk_metric(preds, target, sk_fn=mean_squared_error): (MeanSquaredError, mean_squared_error, sk_mean_squared_error), (MeanAbsoluteError, mean_absolute_error, sk_mean_absolute_error), (MeanSquaredLogError, mean_squared_log_error, sk_mean_squared_log_error), + (MeanAbsolutePercentageError, mean_absolute_percentage_error, sk_mean_abs_percentage_error), ], ) class TestMeanError(MetricTester): @@ -115,7 +127,9 @@ def test_mean_error_half_gpu(self, preds, target, sk_metric, metric_class, metri self.run_precision_test_gpu(preds, target, metric_class, metric_functional) -@pytest.mark.parametrize("metric_class", [MeanSquaredError, MeanAbsoluteError, MeanSquaredLogError]) +@pytest.mark.parametrize( + "metric_class", [MeanSquaredError, MeanAbsoluteError, MeanSquaredLogError, MeanAbsolutePercentageError] +) def test_error_on_different_shape(metric_class): metric = metric_class() with pytest.raises(RuntimeError, match='Predictions and targets are expected to have the same shape'): diff --git a/torchmetrics/functional/__init__.py b/torchmetrics/functional/__init__.py index d88936b2635..dda75a7583b 100644 --- a/torchmetrics/functional/__init__.py +++ b/torchmetrics/functional/__init__.py @@ -40,6 +40,9 @@ from torchmetrics.functional.regression.r2score import r2score # noqa: F401 from torchmetrics.functional.regression.spearman import spearman_corrcoef # noqa: F401 from torchmetrics.functional.regression.ssim import ssim # noqa: F401 +from torchmetrics.functional.regression.mean_absolute_percentage_error import ( + mean_absolute_percentage_error, +) # noqa: F401 from torchmetrics.functional.retrieval.average_precision import retrieval_average_precision # noqa: F401 from torchmetrics.functional.retrieval.fall_out import retrieval_fall_out # noqa: F401 from torchmetrics.functional.retrieval.ndcg import retrieval_normalized_dcg # noqa: F401 diff --git a/torchmetrics/functional/regression/__init__.py b/torchmetrics/functional/regression/__init__.py index 28aabb6ec62..80695760692 100644 --- a/torchmetrics/functional/regression/__init__.py +++ b/torchmetrics/functional/regression/__init__.py @@ -20,3 +20,6 @@ from torchmetrics.functional.regression.r2score import r2score # noqa: F401 from torchmetrics.functional.regression.spearman import spearman_corrcoef # noqa: F401 from torchmetrics.functional.regression.ssim import ssim # noqa: F401 +from torchmetrics.functional.regression.mean_absolute_percentage_error import ( + mean_absolute_percentage_error, +) # noqa: F401 diff --git a/torchmetrics/functional/regression/mean_absolute_percentage_error.py b/torchmetrics/functional/regression/mean_absolute_percentage_error.py new file mode 100644 index 00000000000..630c6bf3dfc --- /dev/null +++ b/torchmetrics/functional/regression/mean_absolute_percentage_error.py @@ -0,0 +1,53 @@ +# Copyright The PyTorch 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 +import numpy as np + +import torch +from torch import Tensor +from torch import tensor +from torch._C import dtype + +from torchmetrics.utilities.checks import _check_same_shape + + +def _mean_absolute_percentage_error_update(preds: torch.Tensor, target: torch.Tensor, eps: torch.Tensor) -> Tuple[Tensor, int]: + + _check_same_shape(preds, target) + + #eps = torch.tensor(1.17e-07)# torch.tensor(np.finfo(np.float32).eps) + + abs_diff = torch.abs(preds - target) + abs_per_error = abs_diff / torch.max(eps, torch.abs(target)) + + sum_abs_per_error = torch.sum(abs_per_error) + + num_obs = target.numel() + + return sum_abs_per_error, num_obs + + +def _mean_absolute_percentage_error_compute(sum_abs_per_error: Tensor, num_obs: int) -> Tensor: + + return sum_abs_per_error / num_obs + + +def mean_absolute_percentage_error(preds: torch.Tensor, target: torch.Tensor, eps: float= 1.17e-07) -> Tensor: + """something""" + + eps = torch.tensor(eps) + sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target, eps) + mean_ape = _mean_absolute_percentage_error_compute(sum_abs_per_error, num_obs) + + return mean_ape diff --git a/torchmetrics/regression/__init__.py b/torchmetrics/regression/__init__.py index d10c35ae864..14d4592df13 100644 --- a/torchmetrics/regression/__init__.py +++ b/torchmetrics/regression/__init__.py @@ -20,3 +20,4 @@ from torchmetrics.regression.r2score import R2Score # noqa: F401 from torchmetrics.regression.spearman import SpearmanCorrcoef # noqa: F401 from torchmetrics.regression.ssim import SSIM # noqa: F401 +from torchmetrics.regression.mean_absolute_percentage_error import MeanAbsolutePercentageError # noqa: F401 diff --git a/torchmetrics/regression/mean_absolute_percentage_error.py b/torchmetrics/regression/mean_absolute_percentage_error.py new file mode 100644 index 00000000000..7cf39f0926e --- /dev/null +++ b/torchmetrics/regression/mean_absolute_percentage_error.py @@ -0,0 +1,58 @@ +# Copyright The PyTorch 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 Any, Callable, Optional + +import torch +from torch import Tensor, tensor + +from torchmetrics.functional.regression.mean_absolute_percentage_error import ( + _mean_absolute_percentage_error_compute, + _mean_absolute_percentage_error_update, +) +from torchmetrics.metric import Metric + + +class MeanAbsolutePercentageError(Metric): + def __init__( + self, + eps: float = 1.17e-07, + compute_on_step: bool = True, + dist_sync_on_step: bool = False, + process_group: Optional[Any] = None, + dist_sync_fn: Callable = None, + ): + super().__init__( + compute_on_step=compute_on_step, + dist_sync_on_step=dist_sync_on_step, + process_group=process_group, + dist_sync_fn=dist_sync_fn, + ) + + self.add_state("sum_abs_per_error", default=tensor(0.0), dist_reduce_fx="sum") + self.add_state("total", default=tensor(0.0), dist_reduce_fx="sum") + self.eps = torch.tensor(eps) + + def update(self, preds: Tensor, target: Tensor) -> None: + + sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target, eps=self.eps) + + self.sum_abs_per_error += sum_abs_per_error + self.total += num_obs + + def compute(self): + return _mean_absolute_percentage_error_compute(self.sum_abs_per_error, self.total) + + @property + def is_differentiable(self): + return False From 844fc9e0af08ab4af20ec8cddc4829ffe22335cd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 May 2021 12:59:45 +0000 Subject: [PATCH 02/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/regression/test_mean_error.py | 6 +++--- torchmetrics/functional/__init__.py | 6 +++--- torchmetrics/functional/regression/__init__.py | 6 +++--- .../functional/regression/mean_absolute_percentage_error.py | 5 ++--- torchmetrics/regression/__init__.py | 2 +- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/regression/test_mean_error.py b/tests/regression/test_mean_error.py index 673f4e1cb18..aafca48a235 100644 --- a/tests/regression/test_mean_error.py +++ b/tests/regression/test_mean_error.py @@ -17,23 +17,23 @@ import pytest import torch from sklearn.metrics import mean_absolute_error as sk_mean_absolute_error +from sklearn.metrics import mean_absolute_percentage_error as sk_mean_abs_percentage_error from sklearn.metrics import mean_squared_error as sk_mean_squared_error from sklearn.metrics import mean_squared_log_error as sk_mean_squared_log_error -from sklearn.metrics import mean_absolute_percentage_error as sk_mean_abs_percentage_error from tests.helpers import seed_all from tests.helpers.testers import BATCH_SIZE, NUM_BATCHES, MetricTester from torchmetrics.functional import ( mean_absolute_error, + mean_absolute_percentage_error, mean_squared_error, mean_squared_log_error, - mean_absolute_percentage_error, ) from torchmetrics.regression import ( MeanAbsoluteError, + MeanAbsolutePercentageError, MeanSquaredError, MeanSquaredLogError, - MeanAbsolutePercentageError, ) from torchmetrics.utilities.imports import _TORCH_GREATER_EQUAL_1_6 diff --git a/torchmetrics/functional/__init__.py b/torchmetrics/functional/__init__.py index dda75a7583b..939987268b5 100644 --- a/torchmetrics/functional/__init__.py +++ b/torchmetrics/functional/__init__.py @@ -32,6 +32,9 @@ from torchmetrics.functional.nlp import bleu_score # noqa: F401 from torchmetrics.functional.regression.explained_variance import explained_variance # noqa: F401 from torchmetrics.functional.regression.mean_absolute_error import mean_absolute_error # noqa: F401 +from torchmetrics.functional.regression.mean_absolute_percentage_error import ( # noqa: F401 + mean_absolute_percentage_error, +) from torchmetrics.functional.regression.mean_relative_error import mean_relative_error # noqa: F401 from torchmetrics.functional.regression.mean_squared_error import mean_squared_error # noqa: F401 from torchmetrics.functional.regression.mean_squared_log_error import mean_squared_log_error # noqa: F401 @@ -40,9 +43,6 @@ from torchmetrics.functional.regression.r2score import r2score # noqa: F401 from torchmetrics.functional.regression.spearman import spearman_corrcoef # noqa: F401 from torchmetrics.functional.regression.ssim import ssim # noqa: F401 -from torchmetrics.functional.regression.mean_absolute_percentage_error import ( - mean_absolute_percentage_error, -) # noqa: F401 from torchmetrics.functional.retrieval.average_precision import retrieval_average_precision # noqa: F401 from torchmetrics.functional.retrieval.fall_out import retrieval_fall_out # noqa: F401 from torchmetrics.functional.retrieval.ndcg import retrieval_normalized_dcg # noqa: F401 diff --git a/torchmetrics/functional/regression/__init__.py b/torchmetrics/functional/regression/__init__.py index 80695760692..a5c8ad5cd4e 100644 --- a/torchmetrics/functional/regression/__init__.py +++ b/torchmetrics/functional/regression/__init__.py @@ -13,6 +13,9 @@ # limitations under the License. from torchmetrics.functional.regression.explained_variance import explained_variance # noqa: F401 from torchmetrics.functional.regression.mean_absolute_error import mean_absolute_error # noqa: F401 +from torchmetrics.functional.regression.mean_absolute_percentage_error import ( # noqa: F401 + mean_absolute_percentage_error, +) from torchmetrics.functional.regression.mean_squared_error import mean_squared_error # noqa: F401 from torchmetrics.functional.regression.mean_squared_log_error import mean_squared_log_error # noqa: F401 from torchmetrics.functional.regression.pearson import pearson_corrcoef # noqa: F401 @@ -20,6 +23,3 @@ from torchmetrics.functional.regression.r2score import r2score # noqa: F401 from torchmetrics.functional.regression.spearman import spearman_corrcoef # noqa: F401 from torchmetrics.functional.regression.ssim import ssim # noqa: F401 -from torchmetrics.functional.regression.mean_absolute_percentage_error import ( - mean_absolute_percentage_error, -) # noqa: F401 diff --git a/torchmetrics/functional/regression/mean_absolute_percentage_error.py b/torchmetrics/functional/regression/mean_absolute_percentage_error.py index 630c6bf3dfc..663c12d1d4a 100644 --- a/torchmetrics/functional/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/functional/regression/mean_absolute_percentage_error.py @@ -12,11 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. from typing import Tuple -import numpy as np +import numpy as np import torch -from torch import Tensor -from torch import tensor +from torch import Tensor, tensor from torch._C import dtype from torchmetrics.utilities.checks import _check_same_shape diff --git a/torchmetrics/regression/__init__.py b/torchmetrics/regression/__init__.py index 14d4592df13..b9d804d954c 100644 --- a/torchmetrics/regression/__init__.py +++ b/torchmetrics/regression/__init__.py @@ -13,6 +13,7 @@ # limitations under the License. from torchmetrics.regression.explained_variance import ExplainedVariance # noqa: F401 from torchmetrics.regression.mean_absolute_error import MeanAbsoluteError # noqa: F401 +from torchmetrics.regression.mean_absolute_percentage_error import MeanAbsolutePercentageError # noqa: F401 from torchmetrics.regression.mean_squared_error import MeanSquaredError # noqa: F401 from torchmetrics.regression.mean_squared_log_error import MeanSquaredLogError # noqa: F401 from torchmetrics.regression.pearson import PearsonCorrcoef # noqa: F401 @@ -20,4 +21,3 @@ from torchmetrics.regression.r2score import R2Score # noqa: F401 from torchmetrics.regression.spearman import SpearmanCorrcoef # noqa: F401 from torchmetrics.regression.ssim import SSIM # noqa: F401 -from torchmetrics.regression.mean_absolute_percentage_error import MeanAbsolutePercentageError # noqa: F401 From ecccca9fe85dac686649ada45452e9e8d46c2017 Mon Sep 17 00:00:00 2001 From: Nicki Skafte Date: Sat, 15 May 2021 11:01:40 +0200 Subject: [PATCH 03/20] increase requirement --- requirements/test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/test.txt b/requirements/test.txt index c68c497aeae..a35ee5a9eee 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -14,6 +14,6 @@ mypy>=0.790 yapf>=0.29.0 cloudpickle>=1.3 -scikit-learn>0.22.1 +scikit-learn>=0.24 scikit-image>0.17.1 nltk>=3.6 From 62107cdb27d8649b3ace445916c7f3fecaba42ba Mon Sep 17 00:00:00 2001 From: Nicki Skafte Date: Sat, 15 May 2021 11:02:59 +0200 Subject: [PATCH 04/20] Apply suggestions from code review --- .../functional/regression/mean_absolute_percentage_error.py | 1 - torchmetrics/regression/mean_absolute_percentage_error.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/torchmetrics/functional/regression/mean_absolute_percentage_error.py b/torchmetrics/functional/regression/mean_absolute_percentage_error.py index 663c12d1d4a..77475f44906 100644 --- a/torchmetrics/functional/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/functional/regression/mean_absolute_percentage_error.py @@ -25,7 +25,6 @@ def _mean_absolute_percentage_error_update(preds: torch.Tensor, target: torch.Te _check_same_shape(preds, target) - #eps = torch.tensor(1.17e-07)# torch.tensor(np.finfo(np.float32).eps) abs_diff = torch.abs(preds - target) abs_per_error = abs_diff / torch.max(eps, torch.abs(target)) diff --git a/torchmetrics/regression/mean_absolute_percentage_error.py b/torchmetrics/regression/mean_absolute_percentage_error.py index 7cf39f0926e..68a1461a51b 100644 --- a/torchmetrics/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/regression/mean_absolute_percentage_error.py @@ -50,9 +50,9 @@ def update(self, preds: Tensor, target: Tensor) -> None: self.sum_abs_per_error += sum_abs_per_error self.total += num_obs - def compute(self): + def compute(self) -> Tensor: return _mean_absolute_percentage_error_compute(self.sum_abs_per_error, self.total) @property - def is_differentiable(self): + def is_differentiable(self) -> bool: return False From a61efa62b3d79da90dbf2030c0e1b08b47ffd0db Mon Sep 17 00:00:00 2001 From: pranjaldatta Date: Sun, 16 May 2021 03:08:33 +0530 Subject: [PATCH 05/20] added docs+changelog and other review changes --- CHANGELOG.md | 2 + docs/source/references/functional.rst | 7 +++ docs/source/references/modules.rst | 7 +++ torchmetrics/__init__.py | 1 + .../mean_absolute_percentage_error.py | 44 +++++++++++---- .../mean_absolute_percentage_error.py | 56 ++++++++++++++++++- 6 files changed, 103 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf3dfe9fb2b..62cc5bd2c99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added support for unnormalized scores (e.g. logits) in `Accuracy`, `Precision`, `Recall`, `FBeta`, `F1`, `StatScore`, `Hamming`, `ConfusionMatrix` metrics ([#200](https://github.com/PyTorchLightning/metrics/pull/200)) +- Added `MeanAbsolutePercentageError(MAPE)` metric. ([#248](https://github.com/PyTorchLightning/metrics/pull/248)) + ### Changed diff --git a/docs/source/references/functional.rst b/docs/source/references/functional.rst index d1ac594e17a..7de8d14c065 100644 --- a/docs/source/references/functional.rst +++ b/docs/source/references/functional.rst @@ -188,6 +188,13 @@ mean_absolute_error [func] :noindex: +mean_absolute_percentage_error [func] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. autofunction:: torchmetrics.functional.mean_absolute_percentage_error + :noindex: + + mean_squared_error [func] ~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/source/references/modules.rst b/docs/source/references/modules.rst index 9889d7a6f46..1220cc413ce 100644 --- a/docs/source/references/modules.rst +++ b/docs/source/references/modules.rst @@ -268,6 +268,13 @@ MeanAbsoluteError :noindex: +MeanAbsolutePercentageError +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. autoclass:: torchmetrics.MeanAbsolutePercentageError + :noindex: + + MeanSquaredError ~~~~~~~~~~~~~~~~ diff --git a/torchmetrics/__init__.py b/torchmetrics/__init__.py index 487ece2d45b..52a48638a6f 100644 --- a/torchmetrics/__init__.py +++ b/torchmetrics/__init__.py @@ -42,6 +42,7 @@ SSIM, ExplainedVariance, MeanAbsoluteError, + MeanAbsolutePercentageError, MeanSquaredError, MeanSquaredLogError, PearsonCorrcoef, diff --git a/torchmetrics/functional/regression/mean_absolute_percentage_error.py b/torchmetrics/functional/regression/mean_absolute_percentage_error.py index 77475f44906..cdf82317587 100644 --- a/torchmetrics/functional/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/functional/regression/mean_absolute_percentage_error.py @@ -13,21 +13,20 @@ # limitations under the License. from typing import Tuple -import numpy as np import torch -from torch import Tensor, tensor -from torch._C import dtype +from torch import Tensor from torchmetrics.utilities.checks import _check_same_shape -def _mean_absolute_percentage_error_update(preds: torch.Tensor, target: torch.Tensor, eps: torch.Tensor) -> Tuple[Tensor, int]: +def _mean_absolute_percentage_error_update( + preds: torch.Tensor, target: torch.Tensor, epsilon: torch.Tensor +) -> Tuple[Tensor, int]: _check_same_shape(preds, target) - - abs_diff = torch.abs(preds - target) - abs_per_error = abs_diff / torch.max(eps, torch.abs(target)) + abs_diff = torch.abs(target - preds) + abs_per_error = abs_diff / torch.max(epsilon, torch.abs(target)) sum_abs_per_error = torch.sum(abs_per_error) @@ -41,11 +40,34 @@ def _mean_absolute_percentage_error_compute(sum_abs_per_error: Tensor, num_obs: return sum_abs_per_error / num_obs -def mean_absolute_percentage_error(preds: torch.Tensor, target: torch.Tensor, eps: float= 1.17e-07) -> Tensor: - """something""" +def mean_absolute_percentage_error(preds: torch.Tensor, target: torch.Tensor, epsilon: float = 1.17e-07) -> Tensor: + """ + Computes mean absolute percentage error. + + Args: + preds: estimated labels + target: ground truth labels + epsilon: an arbitrary small yet strictly positive number to avoid undefined results when y is zero. + default: 1.17e-07. For more information, check the note. + + Return: + Tensor with MAPE + + Note: + The epsilon value is taken from `scikit-learn's + implementation + `_. + + Example: + >>> from torchmetrics.functional import mean_absolute_percentage_error + >>> target = torch.tensor([1, 10, 1e6]) + >>> preds = torch.tensor([0.9, 15, 1.2e6]) + >>> mean_absolute_percentage_error(preds, target) + tensor(0.2667) + """ - eps = torch.tensor(eps) - sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target, eps) + epsilon = torch.tensor(epsilon) + sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target, epsilon) mean_ape = _mean_absolute_percentage_error_compute(sum_abs_per_error, num_obs) return mean_ape diff --git a/torchmetrics/regression/mean_absolute_percentage_error.py b/torchmetrics/regression/mean_absolute_percentage_error.py index 68a1461a51b..b806285e9e7 100644 --- a/torchmetrics/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/regression/mean_absolute_percentage_error.py @@ -24,9 +24,50 @@ class MeanAbsolutePercentageError(Metric): + r""" + Computes `mean absolute percentage error `_ (MAPE): + + .. math:: \text{MAPE} = \frac{1}{n}\sum_1^n\frac{| y_i - \hat{y_i} |}{\max(\epsilon, y_i)} + + Where :math:`y` is a tensor of target values, and :math:`\hat{y}` is a tensor of predictions. + + Args: + epsilon: + an arbitrary small yet strictly positive number to avoid undefined results when y is zero. + default: 1.17e-07. For more information, check the note. + compute_on_step: + Forward only calls ``update()`` and return None if this is set to False. default: True + dist_sync_on_step: + Synchronize metric state across processes at each ``forward()`` + before returning the value at the step. default: False + process_group: + Specify the process group on which synchronization is called. default: None (which selects the entire world) + + Note: + The epsilon value is taken from `scikit-learn's implementation + `_. + + Note: + MAPE output is a non-negative floating point. Best result is 0.0 . But it is important to note that, + bad predictions, can lead to arbitarily large values. Especially when some ``target`` values are close to 0. + This implementation returns a very large number instead of ``inf``. + For more information, `read here + `_. + + Example: + >>> from torchmetrics import MeanAbsolutePercentageError + >>> target = torch.tensor([1, 10, 1e6]) + >>> preds = torch.tensor([0.9, 15, 1.2e6]) + >>> mean_abs_percentage_error = MeanAbsolutePercentageError() + >>> mean_abs_percentage_error(preds, target) + tensor(0.2667) + + + """ + def __init__( self, - eps: float = 1.17e-07, + epsilon: float = 1.17e-07, compute_on_step: bool = True, dist_sync_on_step: bool = False, process_group: Optional[Any] = None, @@ -41,16 +82,25 @@ def __init__( self.add_state("sum_abs_per_error", default=tensor(0.0), dist_reduce_fx="sum") self.add_state("total", default=tensor(0.0), dist_reduce_fx="sum") - self.eps = torch.tensor(eps) + self.epsilon = torch.tensor(epsilon) def update(self, preds: Tensor, target: Tensor) -> None: + """ + Update state with predictions and targets. - sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target, eps=self.eps) + Args: + preds: Predictions from model + target: Ground truth values + """ + sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target, epsilon=self.epsilon) self.sum_abs_per_error += sum_abs_per_error self.total += num_obs def compute(self) -> Tensor: + """ + Computes mean absolute percentage error over state. + """ return _mean_absolute_percentage_error_compute(self.sum_abs_per_error, self.total) @property From df634e35b81ed9b3e4115b5bfce8037d9bc74f98 Mon Sep 17 00:00:00 2001 From: pranjaldatta Date: Sun, 16 May 2021 11:42:06 +0530 Subject: [PATCH 06/20] consistent shorted type hints --- .../functional/regression/mean_absolute_percentage_error.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/torchmetrics/functional/regression/mean_absolute_percentage_error.py b/torchmetrics/functional/regression/mean_absolute_percentage_error.py index cdf82317587..6c543ef0b7e 100644 --- a/torchmetrics/functional/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/functional/regression/mean_absolute_percentage_error.py @@ -19,9 +19,7 @@ from torchmetrics.utilities.checks import _check_same_shape -def _mean_absolute_percentage_error_update( - preds: torch.Tensor, target: torch.Tensor, epsilon: torch.Tensor -) -> Tuple[Tensor, int]: +def _mean_absolute_percentage_error_update(preds: Tensor, target: Tensor, epsilon: Tensor) -> Tuple[Tensor, int]: _check_same_shape(preds, target) @@ -40,7 +38,7 @@ def _mean_absolute_percentage_error_compute(sum_abs_per_error: Tensor, num_obs: return sum_abs_per_error / num_obs -def mean_absolute_percentage_error(preds: torch.Tensor, target: torch.Tensor, epsilon: float = 1.17e-07) -> Tensor: +def mean_absolute_percentage_error(preds: Tensor, target: Tensor, epsilon: float = 1.17e-07) -> Tensor: """ Computes mean absolute percentage error. From 2e2d5823fec728e1bcf550856e4f95803743c6a4 Mon Sep 17 00:00:00 2001 From: Nicki Skafte Date: Tue, 18 May 2021 10:26:50 +0200 Subject: [PATCH 07/20] fix tests --- tests/regression/test_mean_error.py | 9 +++++++-- .../mean_absolute_percentage_error.py | 17 +++++++---------- .../mean_absolute_percentage_error.py | 6 ++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/regression/test_mean_error.py b/tests/regression/test_mean_error.py index aafca48a235..b599dbf88a5 100644 --- a/tests/regression/test_mean_error.py +++ b/tests/regression/test_mean_error.py @@ -57,13 +57,13 @@ def _single_target_sk_metric(preds, target, sk_fn=mean_squared_error): sk_preds = preds.view(-1).numpy() sk_target = target.view(-1).numpy() - return sk_fn(sk_preds, sk_target) + return sk_fn(sk_target, sk_preds) def _multi_target_sk_metric(preds, target, sk_fn=mean_squared_error): sk_preds = preds.view(-1, num_targets).numpy() sk_target = target.view(-1, num_targets).numpy() - return sk_fn(sk_preds, sk_target) + return sk_fn(sk_target, sk_preds) @pytest.mark.parametrize( @@ -120,6 +120,11 @@ def test_mean_error_half_cpu(self, preds, target, sk_metric, metric_class, metri if metric_class == MeanSquaredLogError: # MeanSquaredLogError half + cpu does not work due to missing support in torch.log pytest.xfail("MeanSquaredLogError metric does not support cpu + half precision") + + if metric_class == MeanAbsolutePercentageError: + # MeanSquaredPercentageError half + cpu does not work due to missing support in torch.log + pytest.xfail("MeanSquaredPercentageError metric does not support cpu + half precision") + self.run_precision_test_cpu(preds, target, metric_class, metric_functional) @pytest.mark.skipif(not torch.cuda.is_available(), reason='test requires cuda') diff --git a/torchmetrics/functional/regression/mean_absolute_percentage_error.py b/torchmetrics/functional/regression/mean_absolute_percentage_error.py index 6c543ef0b7e..d52a1e02105 100644 --- a/torchmetrics/functional/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/functional/regression/mean_absolute_percentage_error.py @@ -19,12 +19,14 @@ from torchmetrics.utilities.checks import _check_same_shape -def _mean_absolute_percentage_error_update(preds: Tensor, target: Tensor, epsilon: Tensor) -> Tuple[Tensor, int]: +def _mean_absolute_percentage_error_update( + preds: Tensor, target: Tensor, epsilon: float = 1.17e-06 +) -> Tuple[Tensor, int]: _check_same_shape(preds, target) - abs_diff = torch.abs(target - preds) - abs_per_error = abs_diff / torch.max(epsilon, torch.abs(target)) + abs_diff = torch.abs(preds - target) + abs_per_error = abs_diff / torch.clamp(torch.abs(target), min=epsilon) sum_abs_per_error = torch.sum(abs_per_error) @@ -34,19 +36,16 @@ def _mean_absolute_percentage_error_update(preds: Tensor, target: Tensor, epsilo def _mean_absolute_percentage_error_compute(sum_abs_per_error: Tensor, num_obs: int) -> Tensor: - return sum_abs_per_error / num_obs -def mean_absolute_percentage_error(preds: Tensor, target: Tensor, epsilon: float = 1.17e-07) -> Tensor: +def mean_absolute_percentage_error(preds: Tensor, target: Tensor) -> Tensor: """ Computes mean absolute percentage error. Args: preds: estimated labels target: ground truth labels - epsilon: an arbitrary small yet strictly positive number to avoid undefined results when y is zero. - default: 1.17e-07. For more information, check the note. Return: Tensor with MAPE @@ -63,9 +62,7 @@ def mean_absolute_percentage_error(preds: Tensor, target: Tensor, epsilon: float >>> mean_absolute_percentage_error(preds, target) tensor(0.2667) """ - - epsilon = torch.tensor(epsilon) - sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target, epsilon) + sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target) mean_ape = _mean_absolute_percentage_error_compute(sum_abs_per_error, num_obs) return mean_ape diff --git a/torchmetrics/regression/mean_absolute_percentage_error.py b/torchmetrics/regression/mean_absolute_percentage_error.py index b806285e9e7..727a0a1908d 100644 --- a/torchmetrics/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/regression/mean_absolute_percentage_error.py @@ -67,7 +67,6 @@ class MeanAbsolutePercentageError(Metric): def __init__( self, - epsilon: float = 1.17e-07, compute_on_step: bool = True, dist_sync_on_step: bool = False, process_group: Optional[Any] = None, @@ -82,7 +81,6 @@ def __init__( self.add_state("sum_abs_per_error", default=tensor(0.0), dist_reduce_fx="sum") self.add_state("total", default=tensor(0.0), dist_reduce_fx="sum") - self.epsilon = torch.tensor(epsilon) def update(self, preds: Tensor, target: Tensor) -> None: """ @@ -92,7 +90,7 @@ def update(self, preds: Tensor, target: Tensor) -> None: preds: Predictions from model target: Ground truth values """ - sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target, epsilon=self.epsilon) + sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(preds, target) self.sum_abs_per_error += sum_abs_per_error self.total += num_obs @@ -105,4 +103,4 @@ def compute(self) -> Tensor: @property def is_differentiable(self) -> bool: - return False + return True From 8515662726dbe40e16fb6499aab22845854a638a Mon Sep 17 00:00:00 2001 From: Nicki Skafte Date: Tue, 18 May 2021 10:28:05 +0200 Subject: [PATCH 08/20] remove arg --- torchmetrics/regression/mean_absolute_percentage_error.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/torchmetrics/regression/mean_absolute_percentage_error.py b/torchmetrics/regression/mean_absolute_percentage_error.py index 727a0a1908d..99bf33269b2 100644 --- a/torchmetrics/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/regression/mean_absolute_percentage_error.py @@ -32,9 +32,6 @@ class MeanAbsolutePercentageError(Metric): Where :math:`y` is a tensor of target values, and :math:`\hat{y}` is a tensor of predictions. Args: - epsilon: - an arbitrary small yet strictly positive number to avoid undefined results when y is zero. - default: 1.17e-07. For more information, check the note. compute_on_step: Forward only calls ``update()`` and return None if this is set to False. default: True dist_sync_on_step: @@ -61,10 +58,7 @@ class MeanAbsolutePercentageError(Metric): >>> mean_abs_percentage_error = MeanAbsolutePercentageError() >>> mean_abs_percentage_error(preds, target) tensor(0.2667) - - """ - def __init__( self, compute_on_step: bool = True, From 8b01f0bc4097c2ed57a36f8523cac00023b048f0 Mon Sep 17 00:00:00 2001 From: pranjaldatta Date: Thu, 20 May 2021 13:12:42 +0530 Subject: [PATCH 09/20] minor merge fixes --- tests/regression/test_mean_error.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/regression/test_mean_error.py b/tests/regression/test_mean_error.py index 2d38c06eb8a..cef4ec9bece 100644 --- a/tests/regression/test_mean_error.py +++ b/tests/regression/test_mean_error.py @@ -62,7 +62,6 @@ def _single_target_sk_metric(preds, target, sk_fn, metric_args): return math.sqrt(res) if (metric_args and not metric_args['squared']) else res - def _multi_target_sk_metric(preds, target, sk_fn, metric_args): sk_preds = preds.view(-1, num_targets).numpy() sk_target = target.view(-1, num_targets).numpy() @@ -70,7 +69,6 @@ def _multi_target_sk_metric(preds, target, sk_fn, metric_args): return math.sqrt(res) if (metric_args and not metric_args['squared']) else res - @pytest.mark.parametrize( "preds, target, sk_metric", [ @@ -84,7 +82,7 @@ def _multi_target_sk_metric(preds, target, sk_fn, metric_args): (MeanSquaredError, mean_squared_error, sk_mean_squared_error, {'squared': True}), (MeanSquaredError, mean_squared_error, sk_mean_squared_error, {'squared': False}), (MeanAbsoluteError, mean_absolute_error, sk_mean_absolute_error, {}), - (MeanAbsolutePercentageError, mean_absolute_percentage_error, sk_mean_abs_percentage_error, {}) + (MeanAbsolutePercentageError, mean_absolute_percentage_error, sk_mean_abs_percentage_error, {}), (MeanSquaredLogError, mean_squared_log_error, sk_mean_squared_log_error, {}), ], From dca19b5f7ffa15ad7c146fe60cf01d1c5ebc69ae Mon Sep 17 00:00:00 2001 From: pranjaldatta Date: Wed, 9 Jun 2021 20:24:39 +0530 Subject: [PATCH 10/20] dep warning added to mean_rel_err --- CHANGELOG.md | 1 + torchmetrics/functional/regression/mean_relative_error.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f69158d483f..5955815102b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deprecated +- Remove `torchmetrics.functional.mean_relative_error`([#248](https://github.com/PyTorchLightning/metrics/pull/248)) ### Removed diff --git a/torchmetrics/functional/regression/mean_relative_error.py b/torchmetrics/functional/regression/mean_relative_error.py index 286a5c9b687..a1fefd27271 100644 --- a/torchmetrics/functional/regression/mean_relative_error.py +++ b/torchmetrics/functional/regression/mean_relative_error.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from typing import Tuple +from warnings import warn import torch from torch import Tensor @@ -51,5 +52,9 @@ def mean_relative_error(preds: Tensor, target: Tensor) -> Tensor: tensor(0.1250) """ + warn( + "Function `mean_relative_error` was deprecated v0.3.3 and will be removed in v0.4." + "Use `mean_absolute_percentage_error` instead.", DeprecationWarning + ) sum_rltv_error, n_obs = _mean_relative_error_update(preds, target) return _mean_relative_error_compute(sum_rltv_error, n_obs) From 7f6d9da3f8f1891d1d473d348da03c0dd7391e1e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Jun 2021 14:55:21 +0000 Subject: [PATCH 11/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/regression/test_mean_error.py | 1 - .../functional/regression/mean_absolute_percentage_error.py | 6 +++--- torchmetrics/regression/mean_absolute_percentage_error.py | 1 + 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/regression/test_mean_error.py b/tests/regression/test_mean_error.py index ad7fff6f345..130ad3b7952 100644 --- a/tests/regression/test_mean_error.py +++ b/tests/regression/test_mean_error.py @@ -88,7 +88,6 @@ def _multi_target_sk_metric(preds, target, sk_fn, metric_args): (MeanAbsoluteError, mean_absolute_error, sk_mean_absolute_error, {}), (MeanAbsolutePercentageError, mean_absolute_percentage_error, sk_mean_abs_percentage_error, {}), (MeanSquaredLogError, mean_squared_log_error, sk_mean_squared_log_error, {}), - ], ) class TestMeanError(MetricTester): diff --git a/torchmetrics/functional/regression/mean_absolute_percentage_error.py b/torchmetrics/functional/regression/mean_absolute_percentage_error.py index d52a1e02105..61386502b76 100644 --- a/torchmetrics/functional/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/functional/regression/mean_absolute_percentage_error.py @@ -19,9 +19,9 @@ from torchmetrics.utilities.checks import _check_same_shape -def _mean_absolute_percentage_error_update( - preds: Tensor, target: Tensor, epsilon: float = 1.17e-06 -) -> Tuple[Tensor, int]: +def _mean_absolute_percentage_error_update(preds: Tensor, + target: Tensor, + epsilon: float = 1.17e-06) -> Tuple[Tensor, int]: _check_same_shape(preds, target) diff --git a/torchmetrics/regression/mean_absolute_percentage_error.py b/torchmetrics/regression/mean_absolute_percentage_error.py index 99bf33269b2..0d9adbe0225 100644 --- a/torchmetrics/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/regression/mean_absolute_percentage_error.py @@ -59,6 +59,7 @@ class MeanAbsolutePercentageError(Metric): >>> mean_abs_percentage_error(preds, target) tensor(0.2667) """ + def __init__( self, compute_on_step: bool = True, From f608e5665a1ea3b866a80991a9c73240c62f3e1b Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Wed, 9 Jun 2021 19:21:42 +0200 Subject: [PATCH 12/20] Apply suggestions from code review --- .../functional/regression/mean_absolute_percentage_error.py | 2 +- torchmetrics/functional/regression/mean_relative_error.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/torchmetrics/functional/regression/mean_absolute_percentage_error.py b/torchmetrics/functional/regression/mean_absolute_percentage_error.py index 61386502b76..6c17762b516 100644 --- a/torchmetrics/functional/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/functional/regression/mean_absolute_percentage_error.py @@ -21,7 +21,7 @@ def _mean_absolute_percentage_error_update(preds: Tensor, target: Tensor, - epsilon: float = 1.17e-06) -> Tuple[Tensor, int]: + epsilon: float = 1.17e-06,) -> Tuple[Tensor, int]: _check_same_shape(preds, target) diff --git a/torchmetrics/functional/regression/mean_relative_error.py b/torchmetrics/functional/regression/mean_relative_error.py index a1fefd27271..e46b6765575 100644 --- a/torchmetrics/functional/regression/mean_relative_error.py +++ b/torchmetrics/functional/regression/mean_relative_error.py @@ -53,7 +53,7 @@ def mean_relative_error(preds: Tensor, target: Tensor) -> Tensor: """ warn( - "Function `mean_relative_error` was deprecated v0.3.3 and will be removed in v0.4." + "Function `mean_relative_error` was deprecated v0.4 and will be removed in v0.5." "Use `mean_absolute_percentage_error` instead.", DeprecationWarning ) sum_rltv_error, n_obs = _mean_relative_error_update(preds, target) From ef336e17aefbad2e9f43e33ecfa57df351333201 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Jun 2021 17:22:06 +0000 Subject: [PATCH 13/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../regression/mean_absolute_percentage_error.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/torchmetrics/functional/regression/mean_absolute_percentage_error.py b/torchmetrics/functional/regression/mean_absolute_percentage_error.py index 6c17762b516..b3b5214c2f7 100644 --- a/torchmetrics/functional/regression/mean_absolute_percentage_error.py +++ b/torchmetrics/functional/regression/mean_absolute_percentage_error.py @@ -19,9 +19,11 @@ from torchmetrics.utilities.checks import _check_same_shape -def _mean_absolute_percentage_error_update(preds: Tensor, - target: Tensor, - epsilon: float = 1.17e-06,) -> Tuple[Tensor, int]: +def _mean_absolute_percentage_error_update( + preds: Tensor, + target: Tensor, + epsilon: float = 1.17e-06, +) -> Tuple[Tensor, int]: _check_same_shape(preds, target) From eae2eefde6f412ec3e96b7219b941064f91ed1c3 Mon Sep 17 00:00:00 2001 From: pranjaldatta Date: Thu, 10 Jun 2021 02:04:57 +0530 Subject: [PATCH 14/20] applied suggestions from code review --- .../functional/regression/mean_relative_error.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/torchmetrics/functional/regression/mean_relative_error.py b/torchmetrics/functional/regression/mean_relative_error.py index e46b6765575..ddb50c01697 100644 --- a/torchmetrics/functional/regression/mean_relative_error.py +++ b/torchmetrics/functional/regression/mean_relative_error.py @@ -17,6 +17,10 @@ import torch from torch import Tensor +from torchmetrics.functional.regression.mean_absolute_percentage_error import ( + _mean_absolute_percentage_error_compute, + _mean_absolute_percentage_error_update, +) from torchmetrics.utilities.checks import _check_same_shape @@ -54,7 +58,8 @@ def mean_relative_error(preds: Tensor, target: Tensor) -> Tensor: """ warn( "Function `mean_relative_error` was deprecated v0.4 and will be removed in v0.5." - "Use `mean_absolute_percentage_error` instead.", DeprecationWarning + "Use `mean_absolute_percentage_error` instead.", + DeprecationWarning, ) - sum_rltv_error, n_obs = _mean_relative_error_update(preds, target) - return _mean_relative_error_compute(sum_rltv_error, n_obs) + sum_rltv_error, n_obs = _mean_absolute_percentage_error_update(preds, target) + return _mean_absolute_percentage_error_compute(sum_rltv_error, n_obs) From c8709d3e9c3e8e1a2f4eae3bf839e46e4d070133 Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Thu, 10 Jun 2021 00:14:45 +0200 Subject: [PATCH 15/20] update --- .../functional/regression/mean_relative_error.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/torchmetrics/functional/regression/mean_relative_error.py b/torchmetrics/functional/regression/mean_relative_error.py index ddb50c01697..7fb66abec02 100644 --- a/torchmetrics/functional/regression/mean_relative_error.py +++ b/torchmetrics/functional/regression/mean_relative_error.py @@ -24,19 +24,6 @@ from torchmetrics.utilities.checks import _check_same_shape -def _mean_relative_error_update(preds: Tensor, target: Tensor) -> Tuple[Tensor, int]: - _check_same_shape(preds, target) - target_nz = target.clone() - target_nz[target == 0] = 1 - sum_rltv_error = torch.sum(torch.abs((preds - target) / target_nz)) - n_obs = target.numel() - return sum_rltv_error, n_obs - - -def _mean_relative_error_compute(sum_rltv_error: Tensor, n_obs: int) -> Tensor: - return sum_rltv_error / n_obs - - def mean_relative_error(preds: Tensor, target: Tensor) -> Tensor: """ Computes mean relative error @@ -54,6 +41,9 @@ def mean_relative_error(preds: Tensor, target: Tensor) -> Tensor: >>> y = torch.tensor([0., 1, 2, 2]) >>> mean_relative_error(x, y) tensor(0.1250) + + .. deprecated:: + Use :func:`torchmetrics.functional.mean_absolute_percentage_error`. Will be removed in v0.5.0. """ warn( From 13574f8a6736641418202637e60257e5fbf85422 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Jun 2021 22:15:11 +0000 Subject: [PATCH 16/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- torchmetrics/functional/regression/mean_relative_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchmetrics/functional/regression/mean_relative_error.py b/torchmetrics/functional/regression/mean_relative_error.py index 7fb66abec02..104220bd51c 100644 --- a/torchmetrics/functional/regression/mean_relative_error.py +++ b/torchmetrics/functional/regression/mean_relative_error.py @@ -41,7 +41,7 @@ def mean_relative_error(preds: Tensor, target: Tensor) -> Tensor: >>> y = torch.tensor([0., 1, 2, 2]) >>> mean_relative_error(x, y) tensor(0.1250) - + .. deprecated:: Use :func:`torchmetrics.functional.mean_absolute_percentage_error`. Will be removed in v0.5.0. From 2ffd6a413e799b76fc26e1b4fc4d17bc9b4377d0 Mon Sep 17 00:00:00 2001 From: pranjaldatta Date: Thu, 10 Jun 2021 04:27:26 +0530 Subject: [PATCH 17/20] added comments regarding test case fixes --- tests/regression/test_mean_error.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/regression/test_mean_error.py b/tests/regression/test_mean_error.py index 130ad3b7952..5c927c18ee1 100644 --- a/tests/regression/test_mean_error.py +++ b/tests/regression/test_mean_error.py @@ -58,14 +58,22 @@ def _single_target_sk_metric(preds, target, sk_fn, metric_args): sk_preds = preds.view(-1).numpy() sk_target = target.view(-1).numpy() + + # `sk_target` and `sk_preds` switched to fix failing tests. + # For more info, check https://github.com/PyTorchLightning/metrics/pull/248#issuecomment-841232277 res = sk_fn(sk_target, sk_preds) + return math.sqrt(res) if (metric_args and not metric_args['squared']) else res def _multi_target_sk_metric(preds, target, sk_fn, metric_args): sk_preds = preds.view(-1, num_targets).numpy() sk_target = target.view(-1, num_targets).numpy() + + # `sk_target` and `sk_preds` switched to fix failing tests. + # For more info, check https://github.com/PyTorchLightning/metrics/pull/248#issuecomment-841232277 res = sk_fn(sk_target, sk_preds) + return math.sqrt(res) if (metric_args and not metric_args['squared']) else res From dd860da59e55c75466ea56a29fa47569b34b988c Mon Sep 17 00:00:00 2001 From: pranjaldatta Date: Thu, 10 Jun 2021 04:36:56 +0530 Subject: [PATCH 18/20] removed unused imports --- torchmetrics/functional/regression/mean_relative_error.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/torchmetrics/functional/regression/mean_relative_error.py b/torchmetrics/functional/regression/mean_relative_error.py index 104220bd51c..cbbdb8a3441 100644 --- a/torchmetrics/functional/regression/mean_relative_error.py +++ b/torchmetrics/functional/regression/mean_relative_error.py @@ -11,7 +11,6 @@ # 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 from warnings import warn import torch @@ -21,7 +20,6 @@ _mean_absolute_percentage_error_compute, _mean_absolute_percentage_error_update, ) -from torchmetrics.utilities.checks import _check_same_shape def mean_relative_error(preds: Tensor, target: Tensor) -> Tensor: From 3f48a8d38dfb66d16f5fe60ff6fc3da60600717e Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Thu, 10 Jun 2021 01:23:05 +0200 Subject: [PATCH 19/20] , --- torchmetrics/functional/regression/mean_relative_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchmetrics/functional/regression/mean_relative_error.py b/torchmetrics/functional/regression/mean_relative_error.py index cbbdb8a3441..9a66663c3fb 100644 --- a/torchmetrics/functional/regression/mean_relative_error.py +++ b/torchmetrics/functional/regression/mean_relative_error.py @@ -47,7 +47,7 @@ def mean_relative_error(preds: Tensor, target: Tensor) -> Tensor: warn( "Function `mean_relative_error` was deprecated v0.4 and will be removed in v0.5." "Use `mean_absolute_percentage_error` instead.", - DeprecationWarning, + DeprecationWarning ) sum_rltv_error, n_obs = _mean_absolute_percentage_error_update(preds, target) return _mean_absolute_percentage_error_compute(sum_rltv_error, n_obs) From 806445301006c2ab3698b09de7855b0fd9d101aa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Jun 2021 23:23:31 +0000 Subject: [PATCH 20/20] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- torchmetrics/functional/regression/mean_relative_error.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/torchmetrics/functional/regression/mean_relative_error.py b/torchmetrics/functional/regression/mean_relative_error.py index 9a66663c3fb..86db486257f 100644 --- a/torchmetrics/functional/regression/mean_relative_error.py +++ b/torchmetrics/functional/regression/mean_relative_error.py @@ -46,8 +46,7 @@ def mean_relative_error(preds: Tensor, target: Tensor) -> Tensor: """ warn( "Function `mean_relative_error` was deprecated v0.4 and will be removed in v0.5." - "Use `mean_absolute_percentage_error` instead.", - DeprecationWarning + "Use `mean_absolute_percentage_error` instead.", DeprecationWarning ) sum_rltv_error, n_obs = _mean_absolute_percentage_error_update(preds, target) return _mean_absolute_percentage_error_compute(sum_rltv_error, n_obs)