# Dataset label format for multi-label text classification

**URL:** <https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998>\
**Category:** 🤗Datasets\
**Created:** [February 21, 2022, 10:37pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998 "2022-02-21T22:37:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![zbeloki](https://avatars.discourse-cdn.com/v4/letter/z/e36b37/32.png) [@zbeloki](https://discuss.huggingface.co/u/zbeloki)\
**Post date:** [February 21, 2022, 10:37pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/1 "2022-02-21T22:37:36Z")

</div>

Hi,

I’ve been able to train a multi-label Bert classifier using a custom Dataset object and the Trainer API from Transformers. The Dataset contains two columns: text and label. After tokenizing, I have all the needed columns for training.

For multi-label classification I also set `model.config.problem_type = "multi_label_classification"`, and define each label as a multi-hot vector (a list of 0/1 values, each corresponding to a different class).

So for instance, given the task consists in assigning one or more topics (sports, politics, culture) to text articles, I end up with labels like [0, 1, 0] (politics) or [1, 1, 0] (sports & politics). This works as expected and I managed to train the model.

Now I’d like to define the label column as a Sequence of ClassLabel items, and keep labels like [0, 2] (sports and culture), [0] (sports), [0, 1, 2] (sports, politics, culture)… If I structure the dataset like that, I get the following error when trying to train:

> ValueError: Target size (torch.Size([16, 2])) must be the same as input size (torch.Size([16, 3]))

Is it necessary to define the label column as a multi-hot encoded vector in order to train a multi-label classifier or can I use ClassLabel objects similar to single-label multi-class task?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![dhruvmetha](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dhruvmetha](https://discuss.huggingface.co/u/dhruvmetha)\
**Post date:** [February 21, 2022, 10:41pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/2 "2022-02-21T22:41:38Z")

</div>

@zbeloki  
Defining the labels as one-hot worked for me, the other thing I did was cast it as a `Sequence` of `ClassLabel`!

Make sure your one-hot encoded labels is `float` datatype.

If you are using pandas dataframes, here’s a short snippet!

```auto
label_enum = {k:j for j, k in enumerate(df['label_categories'].unique())}
num_labels = len(label_enum)
df['labels'] = df['label_categories'].apply(lambda x: [1.0 if label_enum[x]==i else 0.0 for i in range(num_labels)])
```

---

<div class="post-metadata">

**Author:** ![zbeloki](https://avatars.discourse-cdn.com/v4/letter/z/e36b37/32.png) [@zbeloki](https://discuss.huggingface.co/u/zbeloki)\
**Post date:** [February 21, 2022, 11:07pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/3 "2022-02-21T23:07:36Z")

</div>

Thanks @dhruvmetha.

Defining labels as “one”-hot vectors ([1, 1, 0, 0, 1]) also works for me, but I’d like to avoid it and define them as a Sequence(feature=ClassLabel(…)). I can’t manage to train the model that way.

When I train a basic single-label multi-class model with labels as `ClassLabel` it works, but if I have multiple possible labels for each entry, I’m not sure if I can use ClassLabel or I’m supposed to use raw “one”-hot vectors as labels.

For instance, take [GoEmotions-simplified](https://huggingface.co/datasets/go_emotions/viewer/simplified/train) dataset as a reference. The labels are a list of ClassLabel elements, but then I can’t train the model as is, I have to convert those labels to multi-hot encoded vectors. Am I doing something wrong? Or it is the expected behaviour and I am required to convert the dataset to multi-hot vectors?

---

<div class="post-metadata">

**Author:** ![dhruvmetha](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dhruvmetha](https://discuss.huggingface.co/u/dhruvmetha)\
**Post date:** [February 21, 2022, 11:17pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/4 "2022-02-21T23:17:18Z")

</div>

> [@Fine-Tune for MultiClass or MultiLabel-MultiClass](https://discuss.huggingface.co/t/fine-tune-for-multiclass-or-multilabel-multiclass/4035/23):
>
> sylvain beat me to it slight_smile i was curious how the problem\_type really worked so cooked up a quick 'n dirty notebook which shows how the type conversion can be made: [transformers\_multilabel-text-classification-with-problem-type.ipynb - Google Drive](https://drive.google.com/file/d/1aue7x525rKy6yYLqqt-5Ll96qjQvpqS7/view?usp=sharing)

Maybe this thread can help you.  
A notebook from that thread that helped me figure this out

> **[Google Colab](https://colab.research.google.com/drive/1aue7x525rKy6yYLqqt-5Ll96qjQvpqS7)**

---

<div class="post-metadata">

**Author:** ![zbeloki](https://avatars.discourse-cdn.com/v4/letter/z/e36b37/32.png) [@zbeloki](https://discuss.huggingface.co/u/zbeloki)\
**Post date:** [February 21, 2022, 11:27pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/5 "2022-02-21T23:27:48Z")

</div>

That notebook was helpful! It does basically the same as I do, encoding the labels as multi-hot vectors. So I guess that is the way to go.

Thank you!

---

<div class="post-metadata">

**Author:** ![muhtasham](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/muhtasham/32/20892_2.png) [@muhtasham](https://discuss.huggingface.co/u/muhtasham)\
**Post date:** [March 4, 2022, 3:12pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/6 "2022-03-04T15:12:36Z")

</div>

Hello, do you know any example notebooks to train using custom dataset for multi-label classification?

---

<div class="post-metadata">

**Author:** ![pierreguillou](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/pierreguillou/32/19626_2.png) [@pierreguillou](https://discuss.huggingface.co/u/pierreguillou)\
**Post date:** [September 19, 2022, 8:53pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/7 "2022-09-19T20:53:07Z")

</div>

> [@zbeloki](#):
>
> encoding the labels as multi-hot vectors

Hi @zbeloki,

I read your posts in this thread with attention as I have the same issue:

**How to use a Dataset with input text as `string` and labels as `Sequence(feature=ClassLabel(…)`) like [GoEmotions-simplified](https://huggingface.co/datasets/go_emotions/viewer/simplified/train) to train a multilabel classification model, and that WITHOUT converting my labels vectors ([4, 23, 1999] for example) to “one”-hot vectors?**

Why? I have 2000 labels and 1.000.000 rows in my Dataset…

It’s a shame to do all these conversions to one-hot vectors.

Did you find another solution?

cc @lewtun @nielsr

---

<div class="post-metadata">

**Author:** ![trasformernewbe](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@trasformernewbe](https://discuss.huggingface.co/u/trasformernewbe)\
**Post date:** [February 9, 2023, 10:57am UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/8 "2023-02-09T10:57:57Z")

</div>

HI @pierreguillou did you find a way to avoid converting the label vectors to one-hot encoded vectors?

---

<div class="post-metadata">

**Author:** ![pierreguillou](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/pierreguillou/32/19626_2.png) [@pierreguillou](https://discuss.huggingface.co/u/pierreguillou)\
**Post date:** [February 9, 2023, 1:01pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/9 "2023-02-09T13:01:29Z")

</div>

No. I did not find another solution ☹

---

<div class="post-metadata">

**Author:** ![trasformernewbe](https://avatars.discourse-cdn.com/v4/letter/t/c5a1d2/32.png) [@trasformernewbe](https://discuss.huggingface.co/u/trasformernewbe)\
**Post date:** [February 9, 2023, 6:35pm UTC](https://discuss.huggingface.co/t/dataset-label-format-for-multi-label-text-classification/14998/10 "2023-02-09T18:35:01Z")

</div>

What about the solution provided here: [Multi-class Using Dataset - #2 by lhoestq](https://discuss.huggingface.co/t/multi-class-using-dataset/8970/2)
