prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  mmcls/models/losses/eval_metrics.py confusion_matrix
  confusion_matrix[target_label.long(), pred_label.long()] += 1
  I think this code is wrong, 【target_label.long(), pred_label.long()】 will list all the coordinates that need + 1, but only once + 1 will work
  it should be:
  `for t, p in zip(target_label, pred_label):
             confusion_matrix[t.long(), p.long()] += 1 `
  
  
  </issue>
  <code>
  [start of mmcls/models/losses/eval_metrics.py]
  1 import numpy as np
  2 import torch
  3 
  4 
  5 def calculate_confusion_matrix(pred, target):
  6     if isinstance(pred, np.ndarray) and isinstance(target, np.ndarray):
  7         pred = torch.from_numpy(pred)
  8         target = torch.from_numpy(target)
  9     elif not (isinstance(pred, torch.Tensor)
  10               and isinstance(target, torch.Tensor)):
  11         raise TypeError('pred and target should both be'
  12                         'torch.Tensor or np.ndarray')
  13     _, pred_label = pred.topk(1, dim=1)
  14     num_classes = pred.size(1)
  15     pred_label = pred_label.view(-1)
  16     target_label = target.view(-1)
  17     assert len(pred_label) == len(target_label)
  18     confusion_matrix = torch.zeros(num_classes, num_classes)
  19     with torch.no_grad():
  20         confusion_matrix[target_label.long(), pred_label.long()] += 1
  21     return confusion_matrix
  22 
  23 
  24 def precision(pred, target):
  25     """Calculate macro-averaged precision according to the prediction and target
  26 
  27     Args:
  28         pred (torch.Tensor | np.array): The model prediction.
  29         target (torch.Tensor | np.array): The target of each prediction.
  30 
  31     Returns:
  32         float: The function will return a single float as precision.
  33     """
  34     confusion_matrix = calculate_confusion_matrix(pred, target)
  35     with torch.no_grad():
  36         res = confusion_matrix.diag() / torch.clamp(
  37             confusion_matrix.sum(0), min=1)
  38         res = res.mean().item() * 100
  39     return res
  40 
  41 
  42 def recall(pred, target):
  43     """Calculate macro-averaged recall according to the prediction and target
  44 
  45     Args:
  46         pred (torch.Tensor | np.array): The model prediction.
  47         target (torch.Tensor | np.array): The target of each prediction.
  48 
  49     Returns:
  50         float: The function will return a single float as recall.
  51     """
  52     confusion_matrix = calculate_confusion_matrix(pred, target)
  53     with torch.no_grad():
  54         res = confusion_matrix.diag() / torch.clamp(
  55             confusion_matrix.sum(1), min=1)
  56         res = res.mean().item() * 100
  57     return res
  58 
  59 
  60 def f1_score(pred, target):
  61     """Calculate macro-averaged F1 score according to the prediction and target
  62 
  63     Args:
  64         pred (torch.Tensor | np.array): The model prediction.
  65         target (torch.Tensor | np.array): The target of each prediction.
  66 
  67     Returns:
  68         float: The function will return a single float as F1 score.
  69     """
  70     confusion_matrix = calculate_confusion_matrix(pred, target)
  71     with torch.no_grad():
  72         precision = confusion_matrix.diag() / torch.clamp(
  73             confusion_matrix.sum(1), min=1)
  74         recall = confusion_matrix.diag() / torch.clamp(
  75             confusion_matrix.sum(0), min=1)
  76         res = 2 * precision * recall / torch.clamp(
  77             precision + recall, min=1e-20)
  78         res = torch.where(torch.isnan(res), torch.full_like(res, 0), res)
  79         res = res.mean().item() * 100
  80     return res
  81
  [end of mmcls/models/losses/eval_metrics.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/mmcls/models/losses/eval_metrics.py b/mmcls/models/losses/eval_metrics.py
  --- a/mmcls/models/losses/eval_metrics.py
  +++ b/mmcls/models/losses/eval_metrics.py
  @@ -17,7 +17,8 @@
       assert len(pred_label) == len(target_label)
       confusion_matrix = torch.zeros(num_classes, num_classes)
       with torch.no_grad():
  -        confusion_matrix[target_label.long(), pred_label.long()] += 1
  +        for t, p in zip(target_label, pred_label):
  +            confusion_matrix[t.long(), p.long()] += 1
       return confusion_matrix
  
verification_info: {"golden_diff": "diff --git a/mmcls/models/losses/eval_metrics.py b/mmcls/models/losses/eval_metrics.py\n--- a/mmcls/models/losses/eval_metrics.py\n+++ b/mmcls/models/losses/eval_metrics.py\n@@ -17,7 +17,8 @@\n     assert len(pred_label) == len(target_label)\n     confusion_matrix = torch.zeros(num_classes, num_classes)\n     with torch.no_grad():\n-        confusion_matrix[target_label.long(), pred_label.long()] += 1\n+        for t, p in zip(target_label, pred_label):\n+            confusion_matrix[t.long(), p.long()] += 1\n     return confusion_matrix\n", "issue": "mmcls/models/losses/eval_metrics.py confusion_matrix\nconfusion_matrix[target_label.long(), pred_label.long()] += 1\r\nI think this code is wrong, \u3010target_label.long(), pred_label.long()\u3011 will list all the coordinates that need + 1, but only once + 1 will work\r\nit should be:\r\n`for t, p in zip(target_label, pred_label):\r\n           confusion_matrix[t.long(), p.long()] += 1 `\r\n\n", "before_files": [{"content": "import numpy as np\nimport torch\n\n\ndef calculate_confusion_matrix(pred, target):\n    if isinstance(pred, np.ndarray) and isinstance(target, np.ndarray):\n        pred = torch.from_numpy(pred)\n        target = torch.from_numpy(target)\n    elif not (isinstance(pred, torch.Tensor)\n              and isinstance(target, torch.Tensor)):\n        raise TypeError('pred and target should both be'\n                        'torch.Tensor or np.ndarray')\n    _, pred_label = pred.topk(1, dim=1)\n    num_classes = pred.size(1)\n    pred_label = pred_label.view(-1)\n    target_label = target.view(-1)\n    assert len(pred_label) == len(target_label)\n    confusion_matrix = torch.zeros(num_classes, num_classes)\n    with torch.no_grad():\n        confusion_matrix[target_label.long(), pred_label.long()] += 1\n    return confusion_matrix\n\n\ndef precision(pred, target):\n    \"\"\"Calculate macro-averaged precision according to the prediction and target\n\n    Args:\n        pred (torch.Tensor | np.array): The model prediction.\n        target (torch.Tensor | np.array): The target of each prediction.\n\n    Returns:\n        float: The function will return a single float as precision.\n    \"\"\"\n    confusion_matrix = calculate_confusion_matrix(pred, target)\n    with torch.no_grad():\n        res = confusion_matrix.diag() / torch.clamp(\n            confusion_matrix.sum(0), min=1)\n        res = res.mean().item() * 100\n    return res\n\n\ndef recall(pred, target):\n    \"\"\"Calculate macro-averaged recall according to the prediction and target\n\n    Args:\n        pred (torch.Tensor | np.array): The model prediction.\n        target (torch.Tensor | np.array): The target of each prediction.\n\n    Returns:\n        float: The function will return a single float as recall.\n    \"\"\"\n    confusion_matrix = calculate_confusion_matrix(pred, target)\n    with torch.no_grad():\n        res = confusion_matrix.diag() / torch.clamp(\n            confusion_matrix.sum(1), min=1)\n        res = res.mean().item() * 100\n    return res\n\n\ndef f1_score(pred, target):\n    \"\"\"Calculate macro-averaged F1 score according to the prediction and target\n\n    Args:\n        pred (torch.Tensor | np.array): The model prediction.\n        target (torch.Tensor | np.array): The target of each prediction.\n\n    Returns:\n        float: The function will return a single float as F1 score.\n    \"\"\"\n    confusion_matrix = calculate_confusion_matrix(pred, target)\n    with torch.no_grad():\n        precision = confusion_matrix.diag() / torch.clamp(\n            confusion_matrix.sum(1), min=1)\n        recall = confusion_matrix.diag() / torch.clamp(\n            confusion_matrix.sum(0), min=1)\n        res = 2 * precision * recall / torch.clamp(\n            precision + recall, min=1e-20)\n        res = torch.where(torch.isnan(res), torch.full_like(res, 0), res)\n        res = res.mean().item() * 100\n    return res\n", "path": "mmcls/models/losses/eval_metrics.py"}]}