From fcd42e98bbd7026d2054fd3009d3aaa1cec6a8bd Mon Sep 17 00:00:00 2001 From: Jirka B Date: Tue, 1 Oct 2024 20:46:22 +0200 Subject: [PATCH 1/6] fix: pearson changes inputs --- src/torchmetrics/functional/image/rmse_sw.py | 3 ++- .../functional/regression/pearson.py | 7 ++++--- tests/unittests/regression/test_pearson.py | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/torchmetrics/functional/image/rmse_sw.py b/src/torchmetrics/functional/image/rmse_sw.py index 16f1a0af80a..a27582bd11a 100644 --- a/src/torchmetrics/functional/image/rmse_sw.py +++ b/src/torchmetrics/functional/image/rmse_sw.py @@ -104,7 +104,8 @@ def _rmse_sw_compute( """ rmse = rmse_val_sum / total_images if rmse_val_sum is not None else None if rmse_map is not None: - rmse_map /= total_images + # prevent overwrite the inputs + rmse_map = rmse_map / total_images return rmse, rmse_map diff --git a/src/torchmetrics/functional/regression/pearson.py b/src/torchmetrics/functional/regression/pearson.py index c98bc65a85c..47b26344163 100644 --- a/src/torchmetrics/functional/regression/pearson.py +++ b/src/torchmetrics/functional/regression/pearson.py @@ -92,9 +92,10 @@ def _pearson_corrcoef_compute( nb: number of observations """ - var_x /= nb - 1 - var_y /= nb - 1 - corr_xy /= nb - 1 + # prevent overwrite the inputs + var_x = var_x / (nb - 1) + var_y = var_y / (nb - 1) + corr_xy = corr_xy / (nb - 1) # if var_x, var_y is float16 and on cpu, make it bfloat16 as sqrt is not supported for float16 # on cpu, remove this after https://github.com/pytorch/pytorch/issues/54774 is fixed if var_x.dtype == torch.float16 and var_x.device == torch.device("cpu"): diff --git a/tests/unittests/regression/test_pearson.py b/tests/unittests/regression/test_pearson.py index 0d23507aeed..00d0af15377 100644 --- a/tests/unittests/regression/test_pearson.py +++ b/tests/unittests/regression/test_pearson.py @@ -164,3 +164,23 @@ def test_single_sample_update(): metric(torch.tensor([7.0]), torch.tensor([8.0])) res2 = metric.compute() assert torch.allclose(res1, res2) + +def test_overwrite_reference_inputs(): + """Test that the normalizations does not overwrite inputs. + + Variables var_x, var_y, corr_xy are references to the object variables and get incorrectly scaled down + such that when you update again and compute you get very wrong values. + """ + y = torch.randn(100) + y_pred = y + torch.randn(y.shape) / 5 + # Initialize Pearson correlation coefficient metric + pearson = PearsonCorrCoef() + # Compute the Pearson correlation coefficient + correlation = pearson(y, y_pred) + + pearson = PearsonCorrCoef() + for lower, upper in [(0, 33), (33, 66), (66, 99), (99, 100)]: + pearson.update(torch.tensor(y[lower:upper]), torch.tensor(y_pred[lower:upper])) + pearson.compute() + + assert torch.isclose(pearson.compute(), correlation) From bedc32c16b2b9b100ac44279c58f36c2a3100380 Mon Sep 17 00:00:00 2001 From: Jirka B Date: Tue, 1 Oct 2024 20:48:52 +0200 Subject: [PATCH 2/6] chlog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a9d9fc6764..5700a43a98b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- +- Fixed for Pearson changes inputs ([#2765](https://github.com/Lightning-AI/torchmetrics/pull/2765)) ## [1.4.2] - 2022-09-12 From 56dc5adf2e30e2bb8cd7f666747891550a250882 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:49:06 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/unittests/regression/test_pearson.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/unittests/regression/test_pearson.py b/tests/unittests/regression/test_pearson.py index 00d0af15377..07cbf3fd65c 100644 --- a/tests/unittests/regression/test_pearson.py +++ b/tests/unittests/regression/test_pearson.py @@ -165,11 +165,13 @@ def test_single_sample_update(): res2 = metric.compute() assert torch.allclose(res1, res2) + def test_overwrite_reference_inputs(): """Test that the normalizations does not overwrite inputs. - Variables var_x, var_y, corr_xy are references to the object variables and get incorrectly scaled down - such that when you update again and compute you get very wrong values. + Variables var_x, var_y, corr_xy are references to the object variables and get incorrectly scaled down such that + when you update again and compute you get very wrong values. + """ y = torch.randn(100) y_pred = y + torch.randn(y.shape) / 5 From 2a3f61f2fc04fdb9691b618467b3f4659db546df Mon Sep 17 00:00:00 2001 From: Jirka B Date: Tue, 1 Oct 2024 21:27:57 +0200 Subject: [PATCH 4/6] doctest --- src/torchmetrics/functional/regression/concordance.py | 2 +- src/torchmetrics/regression/concordance.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/torchmetrics/functional/regression/concordance.py b/src/torchmetrics/functional/regression/concordance.py index e18afe2f619..51c9807c898 100644 --- a/src/torchmetrics/functional/regression/concordance.py +++ b/src/torchmetrics/functional/regression/concordance.py @@ -48,7 +48,7 @@ def concordance_corrcoef(preds: Tensor, target: Tensor) -> Tensor: >>> target = torch.tensor([3, -0.5, 2, 7]) >>> preds = torch.tensor([2.5, 0.0, 2, 8]) >>> concordance_corrcoef(preds, target) - tensor([0.9777]) + tensor([0.9796]) Example (multi output regression): >>> from torchmetrics.functional.regression import concordance_corrcoef diff --git a/src/torchmetrics/regression/concordance.py b/src/torchmetrics/regression/concordance.py index 6697ccadec9..cfbd761fdeb 100644 --- a/src/torchmetrics/regression/concordance.py +++ b/src/torchmetrics/regression/concordance.py @@ -56,7 +56,7 @@ class ConcordanceCorrCoef(PearsonCorrCoef): >>> preds = tensor([2.5, 0.0, 2, 8]) >>> concordance = ConcordanceCorrCoef() >>> concordance(preds, target) - tensor(0.9777) + tensor(0.9796) Example (multi output regression): >>> from torchmetrics.regression import ConcordanceCorrCoef From 547e03a21b6920ce12d4fc6911ad93a07ed5cb94 Mon Sep 17 00:00:00 2001 From: Nicki Skafte Date: Tue, 8 Oct 2024 15:27:44 +0200 Subject: [PATCH 5/6] fix tests --- src/torchmetrics/functional/regression/concordance.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/torchmetrics/functional/regression/concordance.py b/src/torchmetrics/functional/regression/concordance.py index 51c9807c898..681cc4e0ab5 100644 --- a/src/torchmetrics/functional/regression/concordance.py +++ b/src/torchmetrics/functional/regression/concordance.py @@ -27,6 +27,8 @@ def _concordance_corrcoef_compute( ) -> Tensor: """Compute the final concordance correlation coefficient based on accumulated statistics.""" pearson = _pearson_corrcoef_compute(var_x, var_y, corr_xy, nb) + var_x = var_x / (nb - 1) + var_y = var_y / (nb - 1) return 2.0 * pearson * var_x.sqrt() * var_y.sqrt() / (var_x + var_y + (mean_x - mean_y) ** 2) From f96cdefbc82744ba75d46f00e875ce3901cf7cf0 Mon Sep 17 00:00:00 2001 From: Nicki Skafte Date: Tue, 8 Oct 2024 15:46:03 +0200 Subject: [PATCH 6/6] fix doctest --- src/torchmetrics/functional/regression/concordance.py | 2 +- src/torchmetrics/regression/concordance.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/torchmetrics/functional/regression/concordance.py b/src/torchmetrics/functional/regression/concordance.py index 681cc4e0ab5..501cf8da054 100644 --- a/src/torchmetrics/functional/regression/concordance.py +++ b/src/torchmetrics/functional/regression/concordance.py @@ -50,7 +50,7 @@ def concordance_corrcoef(preds: Tensor, target: Tensor) -> Tensor: >>> target = torch.tensor([3, -0.5, 2, 7]) >>> preds = torch.tensor([2.5, 0.0, 2, 8]) >>> concordance_corrcoef(preds, target) - tensor([0.9796]) + tensor([0.9777]) Example (multi output regression): >>> from torchmetrics.functional.regression import concordance_corrcoef diff --git a/src/torchmetrics/regression/concordance.py b/src/torchmetrics/regression/concordance.py index cfbd761fdeb..6697ccadec9 100644 --- a/src/torchmetrics/regression/concordance.py +++ b/src/torchmetrics/regression/concordance.py @@ -56,7 +56,7 @@ class ConcordanceCorrCoef(PearsonCorrCoef): >>> preds = tensor([2.5, 0.0, 2, 8]) >>> concordance = ConcordanceCorrCoef() >>> concordance(preds, target) - tensor(0.9796) + tensor(0.9777) Example (multi output regression): >>> from torchmetrics.regression import ConcordanceCorrCoef