prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Pearson Correlation Coefficient raises error when 2D tensor but single task
  ## 🐛 Bug
  
  I have a regression based modelling repository where the predictions can be multi-output or single-output based on configuration. My network outputs `[n_samples, n_tasks]` where `n_task` varies according to the task. If `n_task` is 1 then trying, `torchmetrics.functional.pearson_corrcoef(predictions, targets)` gives the error,
  
  ```bash
  ValueError: Expected argument `num_outputs` to match the second dimension of input, but got 1 and 1
  ```
  
  Changing the output shape for a single task specifically just so as to fit the metric function does not seem like a good solution. I think a simple change should be able to fix it.
  My current workout around,
  ```python
  import torchmetrics.functional as Fm
  
  # predictions are [n, 1] for single task/output
  Fm.pearson_corrcoef(predictions, targets) if predictions.shape[1] > 1 else Fm.pearson_corrcoef(predictions[:, 0], targets[:, 0])
  ```
  
  There are other metrics that handle this,
  ```python
          metrics = {
              "mse": Fm.mean_squared_error(predictions, targets, squared=True),
              "rmse": Fm.mean_squared_error(predictions, targets, squared=False),
              "mae": Fm.mean_absolute_error(predictions, targets),
              "r2": Fm.r2_score(predictions, targets, multioutput="raw_values"),
              "mape": Fm.mean_absolute_percentage_error(predictions, targets),
              # TODO: Raise issue on torchmetrics
              "pcc": (
                  Fm.pearson_corrcoef(predictions, targets) if predictions.shape[1] > 1 else
                  Fm.pearson_corrcoef(predictions[:, 0], targets[:, 0])
              ),
          }
  ```
  
  
  <!-- A clear and concise description of what the bug is. -->
  
  ### To Reproduce
  
  Steps to reproduce the behavior...
  
  <!-- If you have a code sample, error messages, stack traces, please provide it here as well -->
  
  <details>
    <summary>Code sample</summary>
  
  <!-- Ideally attach a minimal code sample to reproduce the decried issue.
  Minimal means having the shortest code but still preserving the bug. -->
  
  </details>
  
  ### Expected behavior
  
  <!-- A clear and concise description of what you expected to happen. -->
  
  ### Environment
  
  - TorchMetrics version (and how you installed TM, e.g. `conda`, `pip`, build from source):
  - Python & PyTorch Version (e.g., 1.0):
  - Any other relevant information such as OS (e.g., Linux):
  
  ### Additional context
  
  <!-- Add any other context about the problem here. -->
  
  
  </issue>
  <code>
  [start of src/torchmetrics/functional/regression/utils.py]
  1 # Copyright The Lightning team.
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # you may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at
  6 #
  7 #     http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 # Unless required by applicable law or agreed to in writing, software
  10 # distributed under the License is distributed on an "AS IS" BASIS,
  11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 # See the License for the specific language governing permissions and
  13 # limitations under the License.
  14 from torch import Tensor
  15 
  16 
  17 def _check_data_shape_to_num_outputs(preds: Tensor, target: Tensor, num_outputs: int) -> None:
  18     """Check that predictions and target have the correct shape, else raise error."""
  19     if preds.ndim > 2 or target.ndim > 2:
  20         raise ValueError(
  21             f"Expected both predictions and target to be either 1- or 2-dimensional tensors,"
  22             f" but got {target.ndim} and {preds.ndim}."
  23         )
  24     if (num_outputs == 1 and preds.ndim != 1) or (num_outputs > 1 and num_outputs != preds.shape[1]):
  25         raise ValueError(
  26             f"Expected argument `num_outputs` to match the second dimension of input, but got {num_outputs}"
  27             f" and {preds.shape[1]}."
  28         )
  29
  [end of src/torchmetrics/functional/regression/utils.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/src/torchmetrics/functional/regression/utils.py b/src/torchmetrics/functional/regression/utils.py
  --- a/src/torchmetrics/functional/regression/utils.py
  +++ b/src/torchmetrics/functional/regression/utils.py
  @@ -21,7 +21,9 @@
               f"Expected both predictions and target to be either 1- or 2-dimensional tensors,"
               f" but got {target.ndim} and {preds.ndim}."
           )
  -    if (num_outputs == 1 and preds.ndim != 1) or (num_outputs > 1 and num_outputs != preds.shape[1]):
  +    cond1 = num_outputs == 1 and not (preds.ndim == 1 or preds.shape[1] == 1)
  +    cond2 = num_outputs > 1 and num_outputs != preds.shape[1]
  +    if cond1 or cond2:
           raise ValueError(
               f"Expected argument `num_outputs` to match the second dimension of input, but got {num_outputs}"
               f" and {preds.shape[1]}."
  
verification_info: {"golden_diff": "diff --git a/src/torchmetrics/functional/regression/utils.py b/src/torchmetrics/functional/regression/utils.py\n--- a/src/torchmetrics/functional/regression/utils.py\n+++ b/src/torchmetrics/functional/regression/utils.py\n@@ -21,7 +21,9 @@\n             f\"Expected both predictions and target to be either 1- or 2-dimensional tensors,\"\n             f\" but got {target.ndim} and {preds.ndim}.\"\n         )\n-    if (num_outputs == 1 and preds.ndim != 1) or (num_outputs > 1 and num_outputs != preds.shape[1]):\n+    cond1 = num_outputs == 1 and not (preds.ndim == 1 or preds.shape[1] == 1)\n+    cond2 = num_outputs > 1 and num_outputs != preds.shape[1]\n+    if cond1 or cond2:\n         raise ValueError(\n             f\"Expected argument `num_outputs` to match the second dimension of input, but got {num_outputs}\"\n             f\" and {preds.shape[1]}.\"\n", "issue": "Pearson Correlation Coefficient raises error when 2D tensor but single task\n## \ud83d\udc1b Bug\r\n\r\nI have a regression based modelling repository where the predictions can be multi-output or single-output based on configuration. My network outputs `[n_samples, n_tasks]` where `n_task` varies according to the task. If `n_task` is 1 then trying, `torchmetrics.functional.pearson_corrcoef(predictions, targets)` gives the error,\r\n\r\n```bash\r\nValueError: Expected argument `num_outputs` to match the second dimension of input, but got 1 and 1\r\n```\r\n\r\nChanging the output shape for a single task specifically just so as to fit the metric function does not seem like a good solution. I think a simple change should be able to fix it.\r\nMy current workout around,\r\n```python\r\nimport torchmetrics.functional as Fm\r\n\r\n# predictions are [n, 1] for single task/output\r\nFm.pearson_corrcoef(predictions, targets) if predictions.shape[1] > 1 else Fm.pearson_corrcoef(predictions[:, 0], targets[:, 0])\r\n```\r\n\r\nThere are other metrics that handle this,\r\n```python\r\n        metrics = {\r\n            \"mse\": Fm.mean_squared_error(predictions, targets, squared=True),\r\n            \"rmse\": Fm.mean_squared_error(predictions, targets, squared=False),\r\n            \"mae\": Fm.mean_absolute_error(predictions, targets),\r\n            \"r2\": Fm.r2_score(predictions, targets, multioutput=\"raw_values\"),\r\n            \"mape\": Fm.mean_absolute_percentage_error(predictions, targets),\r\n            # TODO: Raise issue on torchmetrics\r\n            \"pcc\": (\r\n                Fm.pearson_corrcoef(predictions, targets) if predictions.shape[1] > 1 else\r\n                Fm.pearson_corrcoef(predictions[:, 0], targets[:, 0])\r\n            ),\r\n        }\r\n```\r\n\r\n\r\n<!-- A clear and concise description of what the bug is. -->\r\n\r\n### To Reproduce\r\n\r\nSteps to reproduce the behavior...\r\n\r\n<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->\r\n\r\n<details>\r\n  <summary>Code sample</summary>\r\n\r\n<!-- Ideally attach a minimal code sample to reproduce the decried issue.\r\nMinimal means having the shortest code but still preserving the bug. -->\r\n\r\n</details>\r\n\r\n### Expected behavior\r\n\r\n<!-- A clear and concise description of what you expected to happen. -->\r\n\r\n### Environment\r\n\r\n- TorchMetrics version (and how you installed TM, e.g. `conda`, `pip`, build from source):\r\n- Python & PyTorch Version (e.g., 1.0):\r\n- Any other relevant information such as OS (e.g., Linux):\r\n\r\n### Additional context\r\n\r\n<!-- Add any other context about the problem here. -->\r\n\n", "before_files": [{"content": "# Copyright The Lightning team.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nfrom torch import Tensor\n\n\ndef _check_data_shape_to_num_outputs(preds: Tensor, target: Tensor, num_outputs: int) -> None:\n    \"\"\"Check that predictions and target have the correct shape, else raise error.\"\"\"\n    if preds.ndim > 2 or target.ndim > 2:\n        raise ValueError(\n            f\"Expected both predictions and target to be either 1- or 2-dimensional tensors,\"\n            f\" but got {target.ndim} and {preds.ndim}.\"\n        )\n    if (num_outputs == 1 and preds.ndim != 1) or (num_outputs > 1 and num_outputs != preds.shape[1]):\n        raise ValueError(\n            f\"Expected argument `num_outputs` to match the second dimension of input, but got {num_outputs}\"\n            f\" and {preds.shape[1]}.\"\n        )\n", "path": "src/torchmetrics/functional/regression/utils.py"}]}