# How to build a multi-label & multi-class dataset correctly?

**URL:** <https://discuss.huggingface.co/t/how-to-build-a-multi-label-multi-class-dataset-correctly/76042>\
**Category:** Beginners\
**Created:** [March 5, 2024, 3:46pm UTC](https://discuss.huggingface.co/t/how-to-build-a-multi-label-multi-class-dataset-correctly/76042 "2024-03-05T15:46:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon6729996](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@anon6729996](https://discuss.huggingface.co/u/anon6729996)\
**Post date:** [March 5, 2024, 3:46pm UTC](https://discuss.huggingface.co/t/how-to-build-a-multi-label-multi-class-dataset-correctly/76042/1 "2024-03-05T15:46:06Z")

</div>

I am unsure how to proceed creating a [Dataset](https://huggingface.co/docs/datasets/en/package_reference/main_classes#datasets.Dataset) with multiple labels and classes where the classes are **not the same** for the different labels.

A multi-label example is shared [here](https://discuss.huggingface.co/t/could-someone-please-explain-how-to-make-a-multi-label-dataset-from-csv/18534/2), but the classes are always either `0` or `1`. My task is slightly different. To illustrate what I mean, take this dataset:

| text ( **`X`** ) | region ( **`y₁`** ) | weather ( **`y₂`** ) | sentiment ( **`y₃`** ) |
| --- | --- | --- | --- |
| ‘The Taj Mahal was beautiful, even in monsoon season’ | ‘asia’ | ‘rain’ | ‘positive’ |
| ‘Surprisingly warm in London with blue skies…’ | ‘europe’ | ‘sun’ | ‘positive’ |
| ‘Snowstorms and icey roads in Illinios, will never go there again!!!’ | ‘america’ | ‘snow’ | ‘negative’ |
| ‘I sincerely enjoyed my Safari despite the hot temperatures’ | ‘africa’ | ‘sun’ | ‘positive’ |

`text`: The input to predict the three outputs.  
`region`: First output to predict, four possible classes `'asia'`, `'europe'`, `'america'`, `'africa'`  
`weather`: Second output to predict, three possible classes `'rain'`, `'sun'`, `'snow'`  
`sentiment`: Third output to predict, two possible classes `'positive'`, `'negative'`

The questions stemming from such a multi-label/multi-class task:

- What are the right types of [Features](https://huggingface.co/docs/datasets/v2.18.0/en/package_reference/main_classes#datasets.Features)? For `text`, it shall be [`Value(dtype='string', id=None)`](https://huggingface.co/docs/datasets/v2.18.0/en/package_reference/main_classes#datasets.Value) feature type, which will be tokenized.
- The outputs are supposed to saved in a `labels` feature, illustrated in [tutorials](https://huggingface.co/docs/transformers/en/training) with the [`ClassLabel`](https://huggingface.co/docs/datasets/v2.18.0/en/package_reference/main_classes#datasets.ClassLabel). So for the `region` label it should be something like this: `ClassLabel(num_classes=3, names=['asia', 'europe', 'america'], id=None)`. But how does it work with three outputs and **different** `names`?

I tried looking into this where the `labels` feature would be of type [Sequence](https://huggingface.co/docs/datasets/v2.18.0/en/package_reference/main_classes#datasets.Sequence) (see [this post](https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724)).  
However, as @lhoestq pointed out, they should have their own columns. Otherwise, `names` ends up having **all** classes in one place, e.g., `Sequence(ClassLabel(names=['asia', 'europe', 'america', 'africa', 'rain', 'sun', 'snow', 'positive', 'negative']))` which would be wrong (?).  
So now I am conflicted how to build my dataset to predict all labels and classes at once correctly.

More questions arising from this once we start training the model:

- Given the above dataset, how would the [`id2label`](https://huggingface.co/docs/transformers/main_classes/configuration#transformers.PretrainedConfig.id2label) and [`label2id`](https://huggingface.co/docs/transformers/main_classes/configuration#transformers.PretrainedConfig.label2id) dicts be structured?
- What would be the correct [`num_labels`](https://huggingface.co/docs/transformers/main_classes/configuration#transformers.PretrainedConfig.num_labels) passed on to the model? Would it be `3` due to the three columns to predict? Or would it be `9` (4+3+2) due to the different outcomes?
- The [`problem_type`](https://huggingface.co/docs/transformers/main_classes/configuration#transformers.PretrainedConfig.problem_type) should be `multi_label_classification`, right? (asking because in previous trainings I ran into issues with tensor sizes not matching and some issues with Float and Long)

Thanks!

---

<div class="post-metadata">

**Author:** ![kevinmmckee](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@kevinmmckee](https://discuss.huggingface.co/u/kevinmmckee)\
**Post date:** [April 14, 2025, 6:46pm UTC](https://discuss.huggingface.co/t/how-to-build-a-multi-label-multi-class-dataset-correctly/76042/2 "2025-04-14T18:46:35Z")

</div>

DId you ever have any luck with this? I’m currently facing a similar problem.

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [April 15, 2025, 5:52am UTC](https://discuss.huggingface.co/t/how-to-build-a-multi-label-multi-class-dataset-correctly/76042/3 "2025-04-15T05:52:21Z")

</div>

This method?

> [@Creating a Sequence of ClassLabel for multi-label and multi-class problems](https://discuss.huggingface.co/t/creating-a-sequence-of-classlabel-for-multi-label-and-multi-class-problems/75724):
>
> Given a df with the following columns: text labels This is a sentence [0, 5, 3] I am unhappy [1, 9, 10] OR text labels1 label2 label3 This is a sentence 0 5 3 I am unhappy 1 9 10 How would one create a Dataset with the following structure of dataset.features: features = Features({ 'text': Value(dtype='string', id=None), 'labels': Sequence({ 'label1': ClassLabel(num\_classes=some\_number, names=some\_names, 'label2': ClassLabel(num\_classes=some\_nu…

---

<div class="post-metadata">

**Author:** ![anon6729996](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@anon6729996](https://discuss.huggingface.co/u/anon6729996)\
**Post date:** [April 15, 2025, 9:16am UTC](https://discuss.huggingface.co/t/how-to-build-a-multi-label-multi-class-dataset-correctly/76042/4 "2025-04-15T09:16:33Z")

</div>

@kevinmmckee No success, sorry!  
I ended up predicting each class individually. I know there _should_ be a more elegant way, but I never figured it out.

---

<div class="post-metadata">

**Author:** ![kevinmmckee](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@kevinmmckee](https://discuss.huggingface.co/u/kevinmmckee)\
**Post date:** [April 18, 2025, 3:51pm UTC](https://discuss.huggingface.co/t/how-to-build-a-multi-label-multi-class-dataset-correctly/76042/5 "2025-04-18T15:51:22Z")

</div>

@anon6729996 Dang. I thought this might be case and was hoping I could achieve this with a single pass across the dataset. Thanks for the reply!
