# How should I compute the accuracy for a multilable dataset?

**URL:** <https://discuss.pytorch.org/t/how-should-i-compute-the-accuracy-for-a-multilable-dataset/43511>\
**Category:** vision\
**Created:** [April 24, 2019, 11:59pm UTC](https://discuss.pytorch.org/t/how-should-i-compute-the-accuracy-for-a-multilable-dataset/43511 "2019-04-24T23:59:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![isalirezag](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/fbc32d/32.png) [@isalirezag](https://discuss.pytorch.org/u/isalirezag)\
**Post date:** [April 24, 2019, 11:59pm UTC](https://discuss.pytorch.org/t/how-should-i-compute-the-accuracy-for-a-multilable-dataset/43511/1 "2019-04-24T23:59:03Z")

</div>

I am wondering how should I get the accuracy for a multi-lable classification task?  
Can someone please provide a toy example? 🙂

---

<div class="post-metadata">

**Author:** ![acobobby](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@acobobby](https://discuss.pytorch.org/u/acobobby)\
**Post date:** [April 25, 2019, 1:56pm UTC](https://discuss.pytorch.org/t/how-should-i-compute-the-accuracy-for-a-multilable-dataset/43511/2 "2019-04-25T13:56:44Z")

</div>

For multiclass classification you should have an output tensor of size `(batch, num_classes)` while the target label tensor should be (a LongTensor) of size `(batch)`, where `batch` is the number of samples.

To compute accuracy you should first compute a softmax in order to have probabilities of each class for each sample, i.e.:

```auto
probs = torch.softmax(out, dim=1)

```

Then you should select the most probable class for each sample, i.e.:

```auto
winners = probs.argmax(dim=1)

```

Now you can compare target with winners:

```auto
corrects = (winners == target)

```

And take the average over all samples:

```auto
accuracy = corrects.sum().float() / float( target.size(0) )

```

If you want to do this all in one you can do it like:

```auto
accuracy = (torch.softmax(out, dim=1).argmax(dim=1) == target).sum().float() / float( target.size(0) )

```

---

<div class="post-metadata">

**Author:** ![isalirezag](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/fbc32d/32.png) [@isalirezag](https://discuss.pytorch.org/u/isalirezag)\
**Post date:** [April 25, 2019, 3:23pm UTC](https://discuss.pytorch.org/t/how-should-i-compute-the-accuracy-for-a-multilable-dataset/43511/3 "2019-04-25T15:23:57Z")

</div>

Lets for the simplicity we say we have one batch in each time. and you have 20 classes.

so the output is gonna be in shape of `torch.Size([1, 20])`, am I correct?

> [@acobobby](#):
>
> To compute accuracy you should first compute a softmax in order to have probabilities of each class for each sample, i.e.:
> 
> ```auto
> probs = torch.softmax(out, dim=1)
> 
> ```
> 
> Then you should select the most probable class for each sample, i.e.:
> 
> ```auto
> winners = probs.argmax(dim=1)
> 
> ```

But in multi lable classification you might have multi class in one time, when you do `winners = probs.argmax(dim=1)` you are considering just one class that I dont think is correct.

---

<div class="post-metadata">

**Author:** ![acobobby](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@acobobby](https://discuss.pytorch.org/u/acobobby)\
**Post date:** [April 25, 2019, 4:08pm UTC](https://discuss.pytorch.org/t/how-should-i-compute-the-accuracy-for-a-multilable-dataset/43511/4 "2019-04-25T16:08:22Z")

</div>

Sorry, I answered a completely different question. My answer is for multiclass classification, while you asked for multilabel classification. My mistake!

A brute-force way to approach your original problem, multilabel classification, is to transform it into multiclass classification by creating all possible combination of the labels and consider each combination a single class.  
In this way you can use the method I explained above, but you have to consider the powerset of the set of all labels, which can be huge.

---

<div class="post-metadata">

**Author:** ![vs74](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vs74/32/35265_2.png) [@vs74](https://discuss.pytorch.org/u/vs74)\
**Post date:** [June 7, 2021, 11:35am UTC](https://discuss.pytorch.org/t/how-should-i-compute-the-accuracy-for-a-multilable-dataset/43511/5 "2021-06-07T11:35:01Z")

</div>

Although your answer if for Multiclass class classification different from what was asked in the question, it really helped a lot as I was searching how to compute the accuracy for multiclass classification.

---

<div class="post-metadata">

**Author:** ![Rohan\_Sharma](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rohan_sharma/32/22502_2.png) [@Rohan\_Sharma](https://discuss.pytorch.org/u/Rohan_Sharma)\
**Post date:** [March 29, 2022, 4:36pm UTC](https://discuss.pytorch.org/t/how-should-i-compute-the-accuracy-for-a-multilable-dataset/43511/6 "2022-03-29T16:36:07Z")

</div>

Why do the softmax before the argmax instead of doing argmax directly on the logits? The answer shouldn’t change…

---

<div class="post-metadata">

**Author:** ![Bibarys\_Mussagaliyev](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bibarys_mussagaliyev/32/34950_2.png) [@Bibarys\_Mussagaliyev](https://discuss.pytorch.org/u/Bibarys_Mussagaliyev)\
**Post date:** [January 13, 2023, 2:08pm UTC](https://discuss.pytorch.org/t/how-should-i-compute-the-accuracy-for-a-multilable-dataset/43511/7 "2023-01-13T14:08:00Z")

</div>

yes, you are right. The results should be the same.
