Skip to content
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

PearsonCorrcoef will break when single sample is passed at a time. #227

Closed
ramonemiliani93 opened this issue May 4, 2021 · 2 comments · Fixed by #229
Closed

PearsonCorrcoef will break when single sample is passed at a time. #227

ramonemiliani93 opened this issue May 4, 2021 · 2 comments · Fixed by #229
Labels
bug / fix Something isn't working help wanted Extra attention is needed

Comments

@ramonemiliani93
Copy link
Contributor

🐛 Bug

When passing a single sample at a time to the PearsonCorrcoef metric it will crash. I am not sure if this is the intended behavior but it causes problems for big models training with batch size of one.

To Reproduce

import torchmetrics
correlation = torchmetrics.PearsonCorrcoef()

# Works
correlation.update(torch.tensor([0.3, 0.7, 0.9]), torch.tensor([0.4, 0.5, 0.4]))
correlation.compute()

# Crashes
correlation.update(torch.tensor([0.3]), torch.tensor([0.4]))
correlation.update(torch.tensor([0.7]), torch.tensor([0.5]))
correlation.update(torch.tensor([0.9]), torch.tensor([0.4]))
correlation.compute()

Error Message:
RuntimeError: zero-dimensional tensor (at position 0) cannot be concatenated

@ramonemiliani93 ramonemiliani93 added bug / fix Something isn't working help wanted Extra attention is needed labels May 4, 2021
@github-actions
Copy link

github-actions bot commented May 4, 2021

Hi! thanks for your contribution!, great first issue!

@raimis
Copy link

raimis commented May 18, 2021

@Borda #229 is in v3.2.0 (https://github.com/PyTorchLightning/metrics/commits/v0.3.2), but the issue isn't fixed:

import torch
import torchmetrics

print(torch.__version__)
print(torchmetrics.__version__)

correlation = torchmetrics.PearsonCorrcoef()

correlation.update(torch.tensor([0.3]), torch.tensor([0.4]))
correlation.update(torch.tensor([0.7]), torch.tensor([0.5]))
correlation.update(torch.tensor([0.9]), torch.tensor([0.4]))
correlation.compute()

Output:

1.8.0
0.3.2
/shared/raimis/opt/miniconda/envs/tmp/lib/python3.8/site-packages/torchmetrics/utilities/prints.py:36: UserWarning: Metric `PearsonCorrcoef` will save all targets and predictions in buffer. For large datasets this may lead to large memory footprint.
  warnings.warn(*args, **kwargs)
Traceback (most recent call last):
  File "bug.py", line 12, in <module>
    correlation.compute()
  File "/shared/raimis/opt/miniconda/envs/tmp/lib/python3.8/site-packages/torchmetrics/metric.py", line 251, in wrapped_func
    self._computed = compute(*args, **kwargs)
  File "/shared/raimis/opt/miniconda/envs/tmp/lib/python3.8/site-packages/torchmetrics/regression/pearson.py", line 95, in compute
    preds = torch.cat(self.preds, dim=0)
RuntimeError: zero-dimensional tensor (at position 0) cannot be concatenated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants