# NER Label tokenization with overflowing tokens

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fner-label-tokenization-with-overflowing-tokens%2F21561%3E%5C
**Category:** 🤗Tokenizers\
**Created:** [August 12, 2022, 2:16pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fner-label-tokenization-with-overflowing-tokens%2F21561 "2022-08-12T14:16:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sajan](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@Sajan](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FSajan)%5C
**Post date:** [August 12, 2022, 2:16pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fner-label-tokenization-with-overflowing-tokens%2F21561%2F1 "2022-08-12T14:16:23Z")

</div>

I am trying to train bert for token classification and I want to use full text and if necessary, split in two samples. For that I am using return\_overflowing\_tokens with a specific stride. I want to tokenize the labels as well for this. I have seen tokenize\_and\_align\_labels function in the tutorial ([Token classification](https://huggingface.co/docs/transformers/tasks/token_classification)) but this doesn’t take care of such overflow. Is there something already available for this? Or should I generate the sample without truncation and then split to a specific length later on?

---

<div class="post-metadata">

**Author:** ![Raisa06](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Raisa06](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FRaisa06)%5C
**Post date:** [November 5, 2023, 5:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fner-label-tokenization-with-overflowing-tokens%2F21561%2F2 "2023-11-05T17:21:39Z")

</div>

@Sajan Hey I’m also working on the same issue. I tried the same approach and getting the same error where input\_ids has different length due to sliding window chunks but the labels still remain the same length as that of dataset. Please let me know if the issue has been resolved and how did you tackled it?

> ArrowInvalid: Column 2 named input\_ids expected length 2 but got length 4

---

<div class="post-metadata">

**Author:** ![Sajan](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@Sajan](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FSajan)%5C
**Post date:** [November 6, 2023, 1:42pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fner-label-tokenization-with-overflowing-tokens%2F21561%2F3 "2023-11-06T13:42:58Z")

</div>

Hi @Raisa06 The error (`ArrowInvalid: Column 2 named input_ids expected length 2 but got length 4`) you are getting seems different.

For this issue, the bert tokenizer returns word\_ids function which gives the original index of pretokenized words and works with return\_overflowing\_tokens. (eg: `tokenized_encoding.word_ids(2)` gives original index of input which can be mapped to labels. I then loop over the tokenized word ids and add corresponding label to the final labels list

---

<div class="post-metadata">

**Author:** ![Raisa06](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Raisa06](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FRaisa06)%5C
**Post date:** [November 6, 2023, 3:15pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fner-label-tokenization-with-overflowing-tokens%2F21561%2F4 "2023-11-06T15:15:35Z")

</div>

Hi @Sajan Thanks for your reply. I get you. Now I’ve implemented the sliding windows and mapped labels to each window. For example,

1. len(example[“words”]) is 2 - i.e, two list with tokens and size more than maximum sequence length.

2. Introduced sliding windows during tokenization,

3. 

> tokenized\_inputs = tokenizer(examples[“words”], is\_split\_into\_words=True, truncation=True, padding=“max\_length”, max\_length=500, stride=200, return\_overflowing\_tokens=True, return\_offsets\_mapping=True)

4. so tokenized\_input[“input\_ids”] - will have lets say 4 list with respect to sliding window configuration that I’ve set.

5. and I’ve mapped input\_ids to word\_ids and then to label manually (hugging face doesn’t have any functions to align labels for sliding windows it is generating)

6. will result in tokenized\_input[“labels”] with 4 list

tokenized\_dataset = datasets.map(tokenize\_and\_align\_labels, batched=True)

More info on datasets,

> DatasetDict({  
> train: Dataset({  
> features: [‘words’, ‘labels’],  
> num\_rows: 2  
> })  
> })

On calling the function again I’m getting the below error,

> ArrowInvalid: Column 1 named labels expected length 2 but got length 4

My doubt here is, Is it an internal function level issue?

---

<div class="post-metadata">

**Author:** ![Sajan](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@Sajan](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FSajan)%5C
**Post date:** [November 6, 2023, 4:36pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fner-label-tokenization-with-overflowing-tokens%2F21561%2F5 "2023-11-06T16:36:57Z")

</div>

How do you map the input ids to word ids? Can you please share that code? Or better yet, the code snippet that does this processing
