# Multi-class Using Dataset

**URL:** <https://discuss.huggingface.co/t/multi-class-using-dataset/8970>\
**Category:** 🤗Datasets\
**Created:** [August 5, 2021, 2:20am UTC](https://discuss.huggingface.co/t/multi-class-using-dataset/8970 "2021-08-05T02:20:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnrodriguez190380](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@johnrodriguez190380](https://discuss.huggingface.co/u/johnrodriguez190380)\
**Post date:** [August 5, 2021, 2:20am UTC](https://discuss.huggingface.co/t/multi-class-using-dataset/8970/1 "2021-08-05T02:20:59Z")

</div>

I have a dataset that is multi-label in nature. The explanatory feature is an image. And the labels are the target.

An example of the classes:  
classes = [‘Smears’, ‘Loaded Language’, ‘Name calling/Labeling’, ‘Glittering generalities (Virtue)’,  
‘Appeal to (Strong) Emotions’, ‘Appeal to fear/prejudice’, ‘Transfer’, ‘Doubt’,  
‘Exaggeration/Minimisation’, ‘Whataboutism’, ‘Slogans’, ‘Flag-waving’,  
“Misrepresentation of Someone’s Position (Straw Man)”, ‘Causal Oversimplification’,  
‘Thought-terminating cliché’, ‘Black-and-white Fallacy/Dictatorship’, ‘Appeal to authority’,  
‘Reductio ad hitlerum’, ‘Repetition’, ‘Obfuscation, Intentional vagueness, Confusion’,  
‘Presenting Irrelevant Data (Red Herring)’, ‘Bandwagon’]

I wanted to setup using this pattern: [https://theaisummer.com/hugging-face-vit/](https://theaisummer.com/hugging-face-vit/)

The example uses datasets.Dataset from hugginface. I wanted to replicate this same example using my data found here: [SEMEVAL-2021-task6-corpus/data at main · di-dimitrov/SEMEVAL-2021-task6-corpus · GitHub](https://github.com/di-dimitrov/SEMEVAL-2021-task6-corpus/tree/main/data).

I loaded my data from the json file:  
with open(‘./data/training\_set\_task3/training\_set\_task3.json’, encoding=‘utf-8’) as f: ### Training data  
training = json.load(f)

df\_training = pd.DataFrame(training)

I use the multilabelbinerizer to convert my multi-labels to one hot encodings, load my images and an example of the dataset is below:

| id | labels | text | image | img | label | |
| --- | --- | --- | --- | --- | --- | --- |
| 0 | 128 | [Black-and-white Fallacy/Dictatorship, Name ca… | THERE ARE ONLY TWO GENDERS\n\nFEMALE \n\nMALE\n | 128\_image.png | [[[255, 255, 255], [255, 255, 255], [255, 255,… | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, … |
| 1 | 189 | [Reductio ad hitlerum, Smears, Transfer] | This is not an accident! | 189\_image.png | [[[11, 15, 64], [11, 15, 64], [11, 15, 64], [1… | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, … |

After subsetting my dataset here is the modeling dataset.

 ![image](https://us1.discourse-cdn.com/hellohellohello/original/2X/e/e8e05c34e3e0364da78aa4feac6feec474e55328.png)

df\_train = df\_train.rename(columns={‘label’:‘labels’})

features = Features({  
‘labels’: ClassLabel(  
names= classes),  
‘img’: Array3D(dtype=“int64”, shape=(3, 32, 32)),  
‘pixel\_values’: Array3D(dtype=“float32”, shape=(3, 224, 224)),  
})

preprocessed\_train\_ds = dataset\_training.map(preprocess\_images, batched=True, batch\_size = 8, features=features)

This last statement generates an error because I failed to account the example is using a single label. I was unable to find an equivalent usage for multi-labels. Is this feature available? Is it coming? Any recommendations for how to address using this pattern? Any ideas would be greatly appreciated. Worst case, I can use this pattern and go back to the other methods of processing but when I tried the example, I loved how everything was self contained and easy to organize.

My goal is to use this visual transformer model from huggingface to process my images. I have used hugging face for the text components so wanted to keep both techniques similar for an understandability perspective.

## Error:

ArrowInvalid Traceback (most recent call last)  
 in   
----\> 1 preprocessed\_train\_ds = dataset\_training.map(preprocess\_images, batched=True, batch\_size = 8, features=features)

~\anaconda3\envs\pytorch\lib\site-packages\datasets\arrow\_dataset.py in map(self, function, with\_indices, input\_columns, batched, batch\_size, drop\_last\_batch, remove\_columns, keep\_in\_memory, load\_from\_cache\_file, cache\_file\_name, writer\_batch\_size, features, disable\_nullable, fn\_kwargs, num\_proc, suffix\_template, new\_fingerprint, desc)  
1663  
1664 if num\_proc is None or num\_proc == 1:  
 → 1665 return self.\_map\_single(  
1666 function=function,  
1667 with\_indices=with\_indices,

~\anaconda3\envs\pytorch\lib\site-packages\datasets\arrow\_dataset.py in wrapper(\*args, \*\*kwargs)  
183 }  
184 # apply actual function  
 → 185 out: Union[“Dataset”, “DatasetDict”] = func(self, \*args, \*\*kwargs)  
186 datasets: List[“Dataset”] = list(out.values()) if isinstance(out, dict) else [out]  
187 # re-apply format to the output

~\anaconda3\envs\pytorch\lib\site-packages\datasets\fingerprint.py in wrapper(\*args, \*\*kwargs)  
395 # Call actual function  
396  
 → 397 out = func(self, \*args, \*\*kwargs)  
398  
399 # Update fingerprint of in-place transforms + update in-place history of transforms

~\anaconda3\envs\pytorch\lib\site-packages\datasets\arrow\_dataset.py in \_map\_single(self, function, with\_indices, input\_columns, batched, batch\_size, drop\_last\_batch, remove\_columns, keep\_in\_memory, load\_from\_cache\_file, cache\_file\_name, writer\_batch\_size, features, disable\_nullable, fn\_kwargs, new\_fingerprint, rank, offset, disable\_tqdm, desc)  
2032 else:  
2033 batch = cast\_to\_python\_objects(batch)  
 → 2034 writer.write\_batch(batch)  
2035 if update\_data and writer is not None:  
2036 writer.finalize() # close\_stream=bool(buf\_writer is None)) # We only close if we are writing in a file

~\anaconda3\envs\pytorch\lib\site-packages\datasets\arrow\_writer.py in write\_batch(self, batch\_examples, writer\_batch\_size)  
389 typed\_sequence = OptimizedTypedSequence(batch\_examples[col], type=col\_type, try\_type=col\_try\_type, col=col)  
390 typed\_sequence\_examples[col] = typed\_sequence  
 → 391 pa\_table = pa.Table.from\_pydict(typed\_sequence\_examples)  
392 self.write\_table(pa\_table, writer\_batch\_size)  
393

~\anaconda3\envs\pytorch\lib\site-packages\pyarrow\table.pxi in pyarrow.lib.Table.from\_pydict()

~\anaconda3\envs\pytorch\lib\site-packages\pyarrow\array.pxi in pyarrow.lib.asarray()

~\anaconda3\envs\pytorch\lib\site-packages\pyarrow\array.pxi in pyarrow.lib.array()

~\anaconda3\envs\pytorch\lib\site-packages\pyarrow\array.pxi in pyarrow.lib.\_handle\_arrow\_array\_protocol()

~\anaconda3\envs\pytorch\lib\site-packages\datasets\arrow\_writer.py in **arrow\_array** (self, type)  
96 out = pa.ExtensionArray.from\_storage(type, pa.array(self.data, type.storage\_dtype))  
97 else:  
—\> 98 out = pa.array(self.data, type=type)  
99 if trying\_type and out[0].as\_py() != self.data[0]:  
100 raise TypeError(

~\anaconda3\envs\pytorch\lib\site-packages\pyarrow\array.pxi in pyarrow.lib.array()

~\anaconda3\envs\pytorch\lib\site-packages\pyarrow\array.pxi in pyarrow.lib.\_sequence\_to\_array()

~\anaconda3\envs\pytorch\lib\site-packages\pyarrow\error.pxi in pyarrow.lib.pyarrow\_internal\_check\_status()

~\anaconda3\envs\pytorch\lib\site-packages\pyarrow\error.pxi in pyarrow.lib.check\_status()

ArrowInvalid: Could not convert [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0] with type list: tried to convert to int

Thanks,

John R.

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/52888_2.png) [@lhoestq](https://discuss.huggingface.co/u/lhoestq)\
**Post date:** [August 23, 2021, 4:20pm UTC](https://discuss.huggingface.co/t/multi-class-using-dataset/8970/2 "2021-08-23T16:20:16Z")

</div>

Hi !

The ClassLabel feature type is for single-label multi-class classification.  
For multi-label classification you can use

```python
from datasets.features import ClassLabel, Sequence
labels_type = Sequence(ClassLabel(names=classes))

```

and modify your the function you pass to `map` to convert your list of 21 booleans to the list of True indices. For example:

[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0] → [4, 12, 18]

---

<div class="post-metadata">

**Author:** ![johnrodriguez190380](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@johnrodriguez190380](https://discuss.huggingface.co/u/johnrodriguez190380)\
**Post date:** [August 23, 2021, 6:10pm UTC](https://discuss.huggingface.co/t/multi-class-using-dataset/8970/3 "2021-08-23T18:10:52Z")

</div>

Thank you for providing that explanation. I will try that and report back. 🙂

That is a fascinating approach, I had not considered converting it to a list of true indices. I will need to look up this technique.

---

<div class="post-metadata">

**Author:** ![pbowyer](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/pbowyer/32/4310_2.png) [@pbowyer](https://discuss.huggingface.co/u/pbowyer)\
**Post date:** [March 23, 2022, 12:32pm UTC](https://discuss.huggingface.co/t/multi-class-using-dataset/8970/4 "2022-03-23T12:32:27Z")

</div>

Did you get this to work? I am trying to get multi-label classification working and tried @lhoestq 's technique, but got further using my list of 0’s and 1’s than I did with True indices.

Do you have any sample notebooks I can look at? Thanks.

---

<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:59am UTC](https://discuss.huggingface.co/t/multi-class-using-dataset/8970/5 "2023-02-09T10:59:35Z")

</div>

Hi, any update on how to correctly train the model using sparse representation instead of the one-hot encoded vectors?

---

<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 10, 2023, 5:05am UTC](https://discuss.huggingface.co/t/multi-class-using-dataset/8970/6 "2023-02-10T05:05:20Z")

</div>

How would you fit this dataset to the trainer?
