# Multilabel text classification Trainer API

**URL:** <https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508>\
**Category:** Beginners\
**Created:** [November 9, 2021, 1:15pm UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508 "2021-11-09T13:15:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dalchob](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@dalchob](https://discuss.huggingface.co/u/dalchob)\
**Post date:** [November 9, 2021, 1:15pm UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508/1 "2021-11-09T13:15:01Z")

</div>

Hi all,  
Can someone help me to do a multilabel classification with the Trainer API ?

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [November 9, 2021, 2:41pm UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508/2 "2021-11-09T14:41:22Z")

</div>

Sure, all you need to do is make sure the `problem_type` of the model’s configuration is set to `multi_label_classification`, e.g.:

```auto
from transformers import BertForSequenceClassification

model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=10, problem_type="multi_label_classification")

```

This will make sure the appropriate loss function is used (namely, binary cross entropy). Note that the current version of Transformers does not support this `problem_type` for any model, but the next version of Transformers will (as per [PR #14180](https://github.com/huggingface/transformers/pull/14180)).

I suggest taking a look at the [example notebook](https://github.com/huggingface/notebooks/blob/master/examples/text_classification.ipynb) to do multi-label classification using the Trainer.

Update: I made a notebook myself to illustrate how to fine-tune any encoder-only Transformer model for multi-label text classification: [Transformers-Tutorials/Fine\_tuning\_BERT\_(and\_friends)\_for\_multi\_label\_text\_classification.ipynb at master · NielsRogge/Transformers-Tutorials · GitHub](https://github.com/NielsRogge/Transformers-Tutorials/blob/master/BERT/Fine_tuning_BERT_(and_friends)_for_multi_label_text_classification.ipynb)

---

<div class="post-metadata">

**Author:** ![kskarakostas](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/kskarakostas/32/3399_2.png) [@kskarakostas](https://discuss.huggingface.co/u/kskarakostas)\
**Post date:** [December 7, 2021, 7:23pm UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508/3 "2021-12-07T19:23:54Z")

</div>

Hi, may I ask here where we can find which models are supported for multilabel classification?  
Thank you in advance

---

<div class="post-metadata">

**Author:** ![Ayanki](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Ayanki](https://discuss.huggingface.co/u/Ayanki)\
**Post date:** [February 11, 2022, 11:11am UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508/4 "2022-02-11T11:11:29Z")

</div>

Hello @nielsr !  
Thanks a lot for your example ! I’ve tried it on my data and accuracy stay at 0 and roc auc at 0.5. I’m clearly having an issue but I can’t find why.  
I have 420 labels, which may be the reason why i’m having this issue ?  
I’m beginner and I clearly don’t know where to start to fix this, any help would be greatly appreciated 🙂

Thanks !

---

<div class="post-metadata">

**Author:** ![Bibhabasu](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bibhabasu/32/39043_2.png) [@Bibhabasu](https://discuss.huggingface.co/u/Bibhabasu)\
**Post date:** [May 18, 2022, 4:16am UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508/5 "2022-05-18T04:16:46Z")

</div>

I am too a beginner but 420 labels a lot, like if there’s a lot of training data then it’s fine but . . . if everything’s right with the code then - less data may be the problem.

---

<div class="post-metadata">

**Author:** ![fischerj](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@fischerj](https://discuss.huggingface.co/u/fischerj)\
**Post date:** [August 1, 2023, 4:26pm UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508/7 "2023-08-01T16:26:58Z")

</div>

Hi Niels!

I’m not sure if this has had too much time pass for this question, but when running your notebook with my data, I keep running into this error which I can’t effectively troubleshoot:

“Classification metrics can’t handle a mix of multiclass-multioutput and multilabel-indicator targets”

Any ideas? The only difference I can see is that my data is originally coded as binary flags (1,0) rather than boolean (T,F)

---

<div class="post-metadata">

**Author:** ![nielsr](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nielsr/32/39617_2.png) [@nielsr](https://discuss.huggingface.co/u/nielsr)\
**Post date:** [August 1, 2023, 5:28pm UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508/8 "2023-08-01T17:28:21Z")

</div>

Hi,

If you have a code reproducer, that would make it easier for me to help 🙂

---

<div class="post-metadata">

**Author:** ![fischerj](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@fischerj](https://discuss.huggingface.co/u/fischerj)\
**Post date:** [August 1, 2023, 6:32pm UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508/9 "2023-08-01T18:32:16Z")

</div>

As much as I appreciate your response, I think I magically fixed it! Thanks (Bedankt!) again for sharing your code in the first place.

---

<div class="post-metadata">

**Author:** ![fischerj](https://avatars.discourse-cdn.com/v4/letter/f/e274bd/32.png) [@fischerj](https://discuss.huggingface.co/u/fischerj)\
**Post date:** [August 2, 2023, 11:33pm UTC](https://discuss.huggingface.co/t/multilabel-text-classification-trainer-api/11508/10 "2023-08-02T23:33:16Z")

</div>

Actually, to formally close this out. I was running into a classification error because I had a rogue non binary value in my validation data (a 2 instead of a 1) which was causing the error, and it would only occur if that row was used for validation.
