-
Notifications
You must be signed in to change notification settings - Fork 464
Weighted pearson #3134
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matsumotosan
wants to merge
20
commits into
Lightning-AI:master
Choose a base branch
from
matsumotosan:1235-weighted-pearson
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
Weighted pearson #3134
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d741dac
separate module for weighted pearson
matsumotosan b34f32f
Merge branch 'master' into 1235-weighted-pearson
Borda f612464
add links
matsumotosan af73759
modify metric tester
matsumotosan 9b48765
partially working weighted pearson test suite
matsumotosan 5096e03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 60aec9a
Merge branch 'master' into 1235-weighted-pearson
matsumotosan 69f28e3
Merge branch 'master' into 1235-weighted-pearson
Borda cfba6f4
Merge branch 'master' into 1235-weighted-pearson
Borda e8aa00f
Merge branch 'Lightning-AI:master' into 1235-weighted-pearson
matsumotosan 2d842a7
Merge branch 'master' into 1235-weighted-pearson
Borda 499f626
Merge branch 'master' into 1235-weighted-pearson
matsumotosan 03d52bd
Merge branch 'master' into 1235-weighted-pearson
matsumotosan 6d49083
iterate over kwargs_update values if value is tensor
matsumotosan dfb3912
wip: individual batches working, final aggregation failing
matsumotosan 55109d4
fix doctests
matsumotosan 6209e06
class test working. differentiability test failing.
matsumotosan 5d6730f
differentiability test should use kwargs_update instead of metric_arg…
matsumotosan c5eae56
functional doctests
matsumotosan 4007a25
Merge branch 'master' into 1235-weighted-pearson
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 hidden or 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 hidden or 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 hidden or 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
181 changes: 181 additions & 0 deletions
181
src/torchmetrics/functional/regression/weighted_pearson.py
This file contains hidden or 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,181 @@ | ||
# 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. | ||
import math | ||
|
||
import torch | ||
from torch import Tensor | ||
|
||
from torchmetrics.functional.regression.utils import _check_data_shape_to_num_outputs, _check_data_shape_to_weights | ||
from torchmetrics.utilities import rank_zero_warn | ||
from torchmetrics.utilities.checks import _check_same_shape | ||
|
||
|
||
def _weighted_pearson_corrcoef_update( | ||
preds: Tensor, | ||
target: Tensor, | ||
weights: Tensor, | ||
mean_x: Tensor, | ||
mean_y: Tensor, | ||
var_x: Tensor, | ||
var_y: Tensor, | ||
cov_xy: Tensor, | ||
weights_prior: Tensor, | ||
num_outputs: int, | ||
) -> tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]: | ||
"""Update and returns variables required to compute weighted Pearson Correlation Coefficient. | ||
|
||
Check for same shape of input tensors. | ||
|
||
Updates are based on `Algorithms for calculating variance`_. Specifically, `online weighted variance`_ and | ||
`online weighted covariance`_. | ||
|
||
Variance intentionally not divided by sum of weights in `update` step as it is computed as necessary in | ||
the `compute` step. | ||
|
||
Args: | ||
preds: estimated scores | ||
target: ground truth scores | ||
weights: weight associated with scores | ||
mean_x: current mean estimate of x tensor | ||
mean_y: current mean estimate of y tensor | ||
var_x: current variance estimate of x tensor | ||
var_y: current variance estimate of y tensor | ||
cov_xy: current covariance estimate between x and y tensor | ||
weights_prior: current sum of weights | ||
num_outputs: number of outputs in multioutput setting | ||
|
||
""" | ||
# Data checking | ||
_check_same_shape(preds, target) | ||
_check_data_shape_to_num_outputs(preds, target, num_outputs) | ||
_check_data_shape_to_weights(preds, weights) | ||
|
||
if preds.ndim == 2: | ||
weights = weights.unsqueeze(1) # singleton dimension for broadcasting | ||
|
||
weights_sum = weights.sum() | ||
weights_new = weights_prior + weights_sum | ||
|
||
if weights_prior > 0: # True if prior observations exist | ||
mx_new = mean_x + (weights * (preds - mean_x)).sum(0) / weights_new | ||
my_new = mean_y + (weights * (target - mean_y)).sum(0) / weights_new | ||
|
||
var_x += (weights * (preds - mx_new) * (preds - mean_x)).sum(0) | ||
var_y += (weights * (target - my_new) * (target - mean_y)).sum(0) | ||
else: | ||
mx_new = ((weights * preds).sum(0) / weights_sum).to(mean_x.dtype) | ||
my_new = ((weights * target).sum(0) / weights_sum).to(mean_y.dtype) | ||
|
||
var_x = (weights * (preds - mx_new) ** 2).sum(0) | ||
var_y = (weights * (target - my_new) ** 2).sum(0) | ||
|
||
# cov_xy += (weights * (preds - mx_new) * (target - my_new)).sum(0) | ||
cov_xy += (weights * (preds - mx_new) * (target - mean_y)).sum(0) | ||
|
||
return mx_new, my_new, var_x, var_y, cov_xy, weights_new | ||
|
||
|
||
def _weighted_pearson_corrcoef_compute( | ||
var_x: Tensor, | ||
var_y: Tensor, | ||
cov_xy: Tensor, | ||
weights_sum: Tensor, | ||
) -> Tensor: | ||
"""Compute the final weighted Pearson correlation based on accumulated statistics. | ||
|
||
Args: | ||
var_x: variance estimate of x tensor | ||
var_y: variance estimate of y tensor | ||
cov_xy: covariance estimate between x and y tensor | ||
weights_sum: sum of weights | ||
|
||
""" | ||
# prevent overwrite the inputs | ||
var_x = var_x / weights_sum | ||
var_y = var_y / weights_sum | ||
cov_xy = cov_xy / weights_sum | ||
|
||
# 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"): | ||
var_x = var_x.bfloat16() | ||
var_y = var_y.bfloat16() | ||
|
||
bound = math.sqrt(torch.finfo(var_x.dtype).eps) | ||
if (var_x < bound).any() or (var_y < bound).any(): | ||
rank_zero_warn( | ||
"The variance of predictions or target is close to zero. This can cause instability in Pearson correlation" | ||
"coefficient, leading to wrong results. Consider re-scaling the input if possible or computing using a" | ||
f"larger dtype (currently using {var_x.dtype}). Setting the correlation coefficient to nan.", | ||
UserWarning, | ||
) | ||
|
||
zero_var_mask = (var_x < bound) | (var_y < bound) | ||
corrcoef = torch.full_like(cov_xy, float("nan"), device=cov_xy.device, dtype=cov_xy.dtype) | ||
valid_mask = ~zero_var_mask | ||
|
||
if valid_mask.any(): | ||
corrcoef[valid_mask] = ( | ||
(cov_xy[valid_mask] / (var_x[valid_mask] * var_y[valid_mask]).sqrt()).squeeze().to(corrcoef.dtype) | ||
) | ||
corrcoef = torch.clamp(corrcoef, -1.0, 1.0) | ||
|
||
return corrcoef.squeeze() | ||
|
||
|
||
def weighted_pearson_corrcoef(preds: Tensor, target: Tensor, weights: Tensor) -> Tensor: | ||
"""Compute weighted Pearson correlation coefficient. | ||
|
||
Args: | ||
preds: torch.Tensor of shape (n_samples,) or (n_samples, n_outputs) | ||
Estimate scores | ||
target: torch.Tensor of shape (n_samples,) or (n_samples, n_outputs) | ||
Ground truth scores | ||
weights: torch.Tensor of shape (n_samples,) | ||
Sample weights | ||
|
||
Example (single output weighted regression): | ||
>>> from torchmetrics.functional.regression import weighted_pearson_corrcoef | ||
>>> target = torch.tensor([3, -0.5, 2, 7]) | ||
>>> preds = torch.tensor([2.5, 0.0, 2, 8]) | ||
>>> weights = torch.tensor([0.1, 0.2, 0.5, 0.1]) | ||
>>> weighted_pearson_corrcoef(preds, target, weights) | ||
tensor(0.9837) | ||
|
||
Example (multi output weighted regression): | ||
>>> from torchmetrics.functional.regression import weighted_pearson_corrcoef | ||
>>> target = torch.tensor([[3, -0.5], [2, 7], [-1, 1.5]]) | ||
>>> preds = torch.tensor([[2.5, 0.0], [2, 8], [0.0, 1.3]]) | ||
>>> weights = torch.tensor([0.3, 0.2, 0.5]) | ||
>>> weighted_pearson_corrcoef(preds, target, weights) | ||
tensor([0.9992, 0.9902]) | ||
|
||
""" | ||
d = preds.shape[1] if preds.ndim == 2 else 1 | ||
_temp = torch.zeros(d, dtype=preds.dtype, device=preds.device) | ||
mean_x, mean_y, var_x = _temp.clone(), _temp.clone(), _temp.clone() | ||
var_y, corr_xy, weights_sum = _temp.clone(), _temp.clone(), _temp.clone().sum() | ||
_, _, var_x, var_y, corr_xy, weights_sum = _weighted_pearson_corrcoef_update( | ||
preds, | ||
target, | ||
weights, | ||
mean_x, | ||
mean_y, | ||
var_x, | ||
var_y, | ||
corr_xy, | ||
weights_sum, | ||
num_outputs=1 if preds.ndim == 1 else preds.shape[-1], | ||
) | ||
return _weighted_pearson_corrcoef_compute(var_x, var_y, corr_xy, weights_sum) |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.