From e689b713f2d5e02b5484eea4cb707058de0e6cb9 Mon Sep 17 00:00:00 2001 From: ben davidson Date: Thu, 28 Apr 2022 21:16:40 +0000 Subject: [PATCH 1/7] break tests --- tests/regression/test_pearson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/regression/test_pearson.py b/tests/regression/test_pearson.py index f1517b1f1c1..4db4dc9aac8 100644 --- a/tests/regression/test_pearson.py +++ b/tests/regression/test_pearson.py @@ -51,7 +51,7 @@ def _sk_pearsonr(preds, target): ], ) class TestPearsonCorrcoef(MetricTester): - atol = 1e-2 + atol = 1e-4 @pytest.mark.parametrize("compute_on_cpu", [True, False]) @pytest.mark.parametrize("ddp", [True, False]) From b82d6d6bcd8c97640d6771c5ac1e52fdcc160c8a Mon Sep 17 00:00:00 2001 From: ben davidson Date: Thu, 28 Apr 2022 22:07:59 +0000 Subject: [PATCH 2/7] fix test --- torchmetrics/regression/pearson.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/torchmetrics/regression/pearson.py b/torchmetrics/regression/pearson.py index c655e4b0b3e..cc95eca53db 100644 --- a/torchmetrics/regression/pearson.py +++ b/torchmetrics/regression/pearson.py @@ -36,19 +36,34 @@ def _final_aggregation( mx1, my1, vx1, vy1, cxy1, n1 = means_x[0], means_y[0], vars_x[0], vars_y[0], corrs_xy[0], nbs[0] for i in range(1, len(means_x)): mx2, my2, vx2, vy2, cxy2, n2 = means_x[i], means_y[i], vars_x[i], vars_y[i], corrs_xy[i], nbs[i] - nb = n1 + n2 mean_x = (n1 * mx1 + n2 * mx2) / nb mean_y = (n1 * my1 + n2 * my2) / nb - var_x = 1 / (n1 + n2 - 1) * ((n1 - 1) * vx1 + (n2 - 1) * vx2 + ((n1 * n2) / (n1 + n2)) * (mx1 - mx2) ** 2) - var_y = 1 / (n1 + n2 - 1) * ((n1 - 1) * vy1 + (n2 - 1) * vy2 + ((n1 * n2) / (n1 + n2)) * (my1 - my2) ** 2) - corr1 = n1 * cxy1 + n1 * (mx1 - mean_x) * (my1 - mean_y) - corr2 = n2 * cxy2 + n2 * (mx2 - mean_x) * (my2 - mean_y) - corr_xy = (corr1 + corr2) / (n1 + n2) + # var_x + magic_element_x1 = (n1 + 1) * mean_x - n1 * mx1 + vx1 += (magic_element_x1 - mx1) * (magic_element_x1 - mean_x) - (magic_element_x1 - mean_x) ** 2 + magic_element_x2 = (n2 + 1) * mean_x - n2 * mx2 + vx2 += (magic_element_x2 - mx2) * (magic_element_x2 - mean_x) - (magic_element_x2 - mean_x) ** 2 + var_x = vx1 + vx2 + + # var_y + magic_element_y1 = (n1 + 1) * mean_y - n1 * my1 + vy1 += (magic_element_y1 - my1) * (magic_element_y1 - mean_y) - (magic_element_y1 - mean_y) ** 2 + magic_element_y2 = (n2 + 1) * mean_y - n2 * my2 + vy2 += (magic_element_y2 - my2) * (magic_element_y2 - mean_y) - (magic_element_y2 - mean_y) ** 2 + var_y = vy1 + vy2 + + # corr + cxy1 += (magic_element_x1 - mx1) * (magic_element_y1 - mean_y) - (magic_element_x1 - mean_x) * ( + magic_element_y1 - mean_y + ) + cxy2 += (magic_element_x2 - mx2) * (magic_element_y2 - mean_y) - (magic_element_x2 - mean_x) * ( + magic_element_y2 - mean_y + ) + corr_xy = cxy1 + cxy2 mx1, my1, vx1, vy1, cxy1, n1 = mean_x, mean_y, var_x, var_y, corr_xy, nb - return var_x, var_y, corr_xy, nb @@ -123,5 +138,4 @@ def compute(self) -> Tensor: var_y = self.var_y corr_xy = self.corr_xy n_total = self.n_total - return _pearson_corrcoef_compute(var_x, var_y, corr_xy, n_total) From 9580b2174a6dbb45d182f6f49d81f84376671889 Mon Sep 17 00:00:00 2001 From: ben davidson Date: Thu, 28 Apr 2022 22:08:43 +0000 Subject: [PATCH 3/7] reduce atol --- tests/regression/test_pearson.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/regression/test_pearson.py b/tests/regression/test_pearson.py index 4db4dc9aac8..e50d786a6b5 100644 --- a/tests/regression/test_pearson.py +++ b/tests/regression/test_pearson.py @@ -51,7 +51,7 @@ def _sk_pearsonr(preds, target): ], ) class TestPearsonCorrcoef(MetricTester): - atol = 1e-4 + atol = 1e-8 @pytest.mark.parametrize("compute_on_cpu", [True, False]) @pytest.mark.parametrize("ddp", [True, False]) From 0bdc4b8dafeedcde21cc171a3905fd8fc88a4452 Mon Sep 17 00:00:00 2001 From: Nicki Skafte Detlefsen Date: Fri, 29 Apr 2022 08:45:57 +0200 Subject: [PATCH 4/7] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5153c66c055..47a2c76650f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- +- Fixed multi device aggregation in `PearsonCorrCoef` ([#998](https://github.com/PyTorchLightning/metrics/pull/998)) - From e4a97ad8ef754a2675546f6ed20eec68f4bccf09 Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Fri, 29 Apr 2022 14:18:11 +0200 Subject: [PATCH 5/7] Apply suggestions from code review Co-authored-by: Nicki Skafte Detlefsen --- tests/regression/test_pearson.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/regression/test_pearson.py b/tests/regression/test_pearson.py index e50d786a6b5..7d71675cbce 100644 --- a/tests/regression/test_pearson.py +++ b/tests/regression/test_pearson.py @@ -51,7 +51,6 @@ def _sk_pearsonr(preds, target): ], ) class TestPearsonCorrcoef(MetricTester): - atol = 1e-8 @pytest.mark.parametrize("compute_on_cpu", [True, False]) @pytest.mark.parametrize("ddp", [True, False]) From 1fb0a34d045a42e531b2314f0b5bbfab50545cb1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Apr 2022 12:21:19 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/regression/test_pearson.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/regression/test_pearson.py b/tests/regression/test_pearson.py index 7d71675cbce..d8ce95a7866 100644 --- a/tests/regression/test_pearson.py +++ b/tests/regression/test_pearson.py @@ -51,7 +51,6 @@ def _sk_pearsonr(preds, target): ], ) class TestPearsonCorrcoef(MetricTester): - @pytest.mark.parametrize("compute_on_cpu", [True, False]) @pytest.mark.parametrize("ddp", [True, False]) def test_pearson_corrcoef(self, preds, target, compute_on_cpu, ddp): From ccf86e192c20382e2ebc30ab7a41c7237d3d1b89 Mon Sep 17 00:00:00 2001 From: Jirka Date: Fri, 29 Apr 2022 18:39:50 +0200 Subject: [PATCH 7/7] magic_ --- torchmetrics/regression/pearson.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/torchmetrics/regression/pearson.py b/torchmetrics/regression/pearson.py index cc95eca53db..832c7390670 100644 --- a/torchmetrics/regression/pearson.py +++ b/torchmetrics/regression/pearson.py @@ -41,26 +41,22 @@ def _final_aggregation( mean_y = (n1 * my1 + n2 * my2) / nb # var_x - magic_element_x1 = (n1 + 1) * mean_x - n1 * mx1 - vx1 += (magic_element_x1 - mx1) * (magic_element_x1 - mean_x) - (magic_element_x1 - mean_x) ** 2 - magic_element_x2 = (n2 + 1) * mean_x - n2 * mx2 - vx2 += (magic_element_x2 - mx2) * (magic_element_x2 - mean_x) - (magic_element_x2 - mean_x) ** 2 + element_x1 = (n1 + 1) * mean_x - n1 * mx1 + vx1 += (element_x1 - mx1) * (element_x1 - mean_x) - (element_x1 - mean_x) ** 2 + element_x2 = (n2 + 1) * mean_x - n2 * mx2 + vx2 += (element_x2 - mx2) * (element_x2 - mean_x) - (element_x2 - mean_x) ** 2 var_x = vx1 + vx2 # var_y - magic_element_y1 = (n1 + 1) * mean_y - n1 * my1 - vy1 += (magic_element_y1 - my1) * (magic_element_y1 - mean_y) - (magic_element_y1 - mean_y) ** 2 - magic_element_y2 = (n2 + 1) * mean_y - n2 * my2 - vy2 += (magic_element_y2 - my2) * (magic_element_y2 - mean_y) - (magic_element_y2 - mean_y) ** 2 + element_y1 = (n1 + 1) * mean_y - n1 * my1 + vy1 += (element_y1 - my1) * (element_y1 - mean_y) - (element_y1 - mean_y) ** 2 + element_y2 = (n2 + 1) * mean_y - n2 * my2 + vy2 += (element_y2 - my2) * (element_y2 - mean_y) - (element_y2 - mean_y) ** 2 var_y = vy1 + vy2 # corr - cxy1 += (magic_element_x1 - mx1) * (magic_element_y1 - mean_y) - (magic_element_x1 - mean_x) * ( - magic_element_y1 - mean_y - ) - cxy2 += (magic_element_x2 - mx2) * (magic_element_y2 - mean_y) - (magic_element_x2 - mean_x) * ( - magic_element_y2 - mean_y - ) + cxy1 += (element_x1 - mx1) * (element_y1 - mean_y) - (element_x1 - mean_x) * (element_y1 - mean_y) + cxy2 += (element_x2 - mx2) * (element_y2 - mean_y) - (element_x2 - mean_x) * (element_y2 - mean_y) corr_xy = cxy1 + cxy2 mx1, my1, vx1, vy1, cxy1, n1 = mean_x, mean_y, var_x, var_y, corr_xy, nb