# Getting KeyError: tensor(\[0\]) while plotting wandb's confusion matrix

**URL:** <https://community.wandb.ai/t/getting-keyerror-tensor-0-while-plotting-wandbs-confusion-matrix/2974>\
**Category:** W&B Help\
**Tags:** wandb\
**Created:** [August 22, 2022, 4:46pm UTC](https://community.wandb.ai/t/getting-keyerror-tensor-0-while-plotting-wandbs-confusion-matrix/2974 "2022-08-22T16:46:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rishav](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@rishav](https://community.wandb.ai/u/rishav)\
**Post date:** [August 22, 2022, 4:46pm UTC](https://community.wandb.ai/t/getting-keyerror-tensor-0-while-plotting-wandbs-confusion-matrix/2974/1 "2022-08-22T16:46:51Z")

</div>

Hi,  
I am trying to plot confusion matrix using wandb’s API.  
But I am getting

```auto
  File "/home/ubuntu/anaconda3/envs/pytorch_p38/lib/python3.8/site-packages/wandb/plot/confusion_matrix.py", line 72, in confusion_matrix
    counts[class_mapping[y_true[i]], class_mapping[preds[i]]] += 1
KeyError: tensor([0])

```

My validation loops like below -

```auto
for batch_idx, (data, target) in enumerate(loader['valid']):
     output = model(data)
     preds = torch.max(output, dim=1, keepdim=True)[1]
     wandb.log({"conf_mat": wandb.plot.confusion_matrix(y_true=target, preds=preds, # noqa
                       class_names=class_names)})

```

---

<div class="post-metadata">

**Author:** ![mohammadbakir](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mohammadbakir](https://community.wandb.ai/u/mohammadbakir)\
**Post date:** [August 24, 2022, 11:57pm UTC](https://community.wandb.ai/t/getting-keyerror-tensor-0-while-plotting-wandbs-confusion-matrix/2974/2 "2022-08-24T23:57:37Z")

</div>

Hi @rishav , this happens when your prediction array values don’t index the correct class name. Example:

The below would produce a `KeyError: 5` as there isn’t a 5th index in class\_names

```auto
preds = [1,2,3,4,5]
class_names = ["one","two","three","four","five"]

```

The above should instead be

```auto
preds = [0,1,2,3,4]
class_names = ["one","two","three","four","five"]

```

---

<div class="post-metadata">

**Author:** ![mohammadbakir](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mohammadbakir](https://community.wandb.ai/u/mohammadbakir)\
**Post date:** [August 30, 2022, 6:08pm UTC](https://community.wandb.ai/t/getting-keyerror-tensor-0-while-plotting-wandbs-confusion-matrix/2974/3 "2022-08-30T18:08:13Z")

</div>

Hi @rishav, since we have not heard back from you we are going to close this request. If you would like to re-open the conversation, please let us know!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/wandb/original/1X/366b649231631dbab896843020da0056074ac79d.png) [@system](https://community.wandb.ai/u/system)\
**Post date:** [October 29, 2022, 6:08pm UTC](https://community.wandb.ai/t/getting-keyerror-tensor-0-while-plotting-wandbs-confusion-matrix/2974/4 "2022-10-29T18:08:31Z")

</div>

Closed on October 29, 2022, 6:08pm UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
