# Inputs.word\_ids() length not matching word label length

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finputs-word-ids-length-not-matching-word-label-length%2F58976%3E%5C
**Category:** 🤗Tokenizers\
**Created:** [October 18, 2023, 11:18am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finputs-word-ids-length-not-matching-word-label-length%2F58976 "2023-10-18T11:18:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![enicholsonbmj](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@enicholsonbmj](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fenicholsonbmj)%5C
**Post date:** [October 18, 2023, 11:18am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finputs-word-ids-length-not-matching-word-label-length%2F58976%2F1 "2023-10-18T11:18:33Z")

</div>

Hi there, I’m having trouble with the tokenizer word\_ids referring to words not in the index. To elaborate, here’s code and error message:

from transformers import AutoTokenizer  
model\_checkpoint = “bionlp/bluebert\_pubmed\_uncased\_L-24\_H-1024\_A-16”  
tokenizer = AutoTokenizer.from\_pretrained(model\_checkpoint)

tokenized\_column\_list =   
word\_id\_list =   
input\_id\_list =   
token\_type\_id\_list =   
attention\_mask\_list =

for index, row in ta\_train\_df.iterrows():  
text = row[‘Raw text’].lower()  
inputs = tokenizer(text)  
tokenized\_column\_list.append(inputs.tokens())  
word\_id\_list.append(inputs.word\_ids())  
input\_id\_list.append(inputs[‘input\_ids’])  
token\_type\_id\_list.append(inputs[‘token\_type\_ids’])  
attention\_mask\_list.append(inputs[‘attention\_mask’])

ta\_train\_df[‘Tokenized\_text’] = tokenized\_column\_list  
ta\_train\_df[‘Word\_ID’] = word\_id\_list  
ta\_train\_df[‘input\_ids’] = input\_id\_list  
ta\_train\_df[‘token\_type\_ids’] = token\_type\_id\_list  
ta\_train\_df[‘attention\_mask’] = attention\_mask\_list

label\_to\_numeric = {‘O’:0, ‘-’: -100, ‘B-rx’:1… etc

def convert\_labels\_to\_numeric(labels):  
numeric\_labels =   
for label in labels:  
if label in label\_to\_numeric:  
numeric\_labels.append(label\_to\_numeric[label])  
else:  
numeric\_labels.append(-100)  
return numeric\_labels

ta\_train\_df[‘numeric\_iob\_tags’] = ta\_train\_df[‘iob\_tags’].apply(convert\_labels\_to\_numeric)

def align\_labels\_with\_tokens(labels, word\_ids):  
new\_labels =   
current\_word = None  
for word\_id in word\_ids:  
if word\_id != current\_word:  
# Start of a new word!  
current\_word = word\_id  
label = -100 if word\_id is None else labels[word\_id]  
new\_labels.append(label)  
elif word\_id is None:  
# Special token  
new\_labels.append(-100)  
else:  
# Same word as previous token  
label = labels[word\_id]  
# If the label is B-XXX we change it to I-XXX  
if label % 2 == 1:  
label += 1  
new\_labels.append(label)  
return new\_labels

labels = ta\_train\_df[‘numeric\_iob\_tags’][0]  
word\_ids = ta\_train\_df[‘Word\_ID’][0]

align\_labels\_with\_tokens(labels, word\_ids)

* * *

IndexError Traceback (most recent call last)  
 in \<cell line: 6\>()  
4 print(labels)  
5 print(word\_ids)  
----\> 6 align\_labels\_with\_tokens(labels, word\_ids)

in align\_labels\_with\_tokens(labels, word\_ids)  
7 current\_word = word\_id  
8 print(current\_word)  
----\> 9 label = -100 if word\_id is None else labels[word\_id]  
10 new\_labels.append(label)  
11 elif word\_id is None:

IndexError: list index out of range

* * *

The input.word\_ids are referring to word 356, whereas there are only 352 words in the labels. I’m not sure where the tokenizer is producing 4 extra words. The text in df\_train\_df[‘Raw text’][0] is this:

'acute. confirmed CES. 1. DECOMPRESSION SURGERY

```auto
Decompression surgery should be performed as soon as possible. Although surgery within 48 hours of symptom onset has been used by some clinicians as a guide, this has been challenged, and remains controversial. It is likely that the level of neurologic dysfunction at the time of surgery (rather than time since symptom onset) is the most significant determinant of prognosis. One retrospective cohort study of 20,924 patients with CES reported that patients undergoing surgical decompression on hospital day 0 or 1 had better improved inpatient outcomes, including lower complication and mortality rates, than patients having surgery on day 2 or later. Evidence on the benefits of earlier surgery (e.g., within 24 hours) is equivocal. This may be due to differences in neurologic dysfunction among participants; some studies suggest that surgery within 24 hours of symptom onset may reduce postoperative bladder dysfunction in patients with incomplete CES, but not in patients with CES with urinary retention, compared with surgery between 24 and 48 hours.

Therefore, as the 48-hour time window is controversial, urgent surgery should not be delayed, especially since the precise time of symptom onset can be difficult to define. British Association of Spine Surgeons guidelines recommend that surgery should take place as soon as possible, while taking into account the duration and clinical course of symptoms and signs, as well as the potential for increased morbidity when operating at night.

The goal of surgery is to alleviate compression of the cauda equina, which may be achieved through a number of surgical techniques (e.g., wide-decompressive laminectomy, lumbar microdiskectomy). The appropriate surgical technique should be chosen based on pathology and the experience of the surgeon.

Intraoperative monitoring of somatosensory and motor-evoked potentials allows for evaluation of radiculopathy and neuropathy, but is not a necessary part of urgent procedures.'
```

---

<div class="post-metadata">

**Author:** ![enicholsonbmj](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@enicholsonbmj](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fenicholsonbmj)%5C
**Post date:** [October 18, 2023, 12:18pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finputs-word-ids-length-not-matching-word-label-length%2F58976%2F2 "2023-10-18T12:18:38Z")

</div>

I think I’ve figures this out. It’s due to special characters in the raw text, such as ‘~~~’, ‘{{’, ‘}}’, ‘|’ , ‘20,357’, etc. It turn out my text labels and the tokenizers handle these cases differently - where the tokenizer separates them and counts them individually, but my labels don’t.

---

<div class="post-metadata">

**Author:** ![mokshmalik5757](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mokshmalik5757](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fmokshmalik5757)%5C
**Post date:** [March 19, 2024, 12:03pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finputs-word-ids-length-not-matching-word-label-length%2F58976%2F3 "2024-03-19T12:03:16Z")

</div>

Hi

Have you been able to solve this problem? Did you write a new function, or created a new tokenizer?

---

<div class="post-metadata">

**Author:** ![enicholsonbmj](https://avatars.discourse-cdn.com/v4/letter/e/48db29/32.png) [@enicholsonbmj](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fenicholsonbmj)%5C
**Post date:** [March 22, 2024, 8:28am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Finputs-word-ids-length-not-matching-word-label-length%2F58976%2F4 "2024-03-22T08:28:08Z")

</div>

Hi, I didn’t create a new function or tokenizer, I just cleaned my raw text a bit better to get rid of the special characters
