PaddlePaddle / PaddleNLP

👑 Easy-to-use and powerful NLP and LLM library with 🤗 Awesome model zoo, supporting wide-range of NLP tasks from research to industrial applications, including 🗂Text Classification, 🔍 Neural Search, ❓ Question Answering, ℹ️ Information Extraction, 📄 Document Intelligence, 💌 Sentiment Analysis etc.
https://paddlenlp.readthedocs.io
Apache License 2.0
11.71k stars 2.86k forks source link

[Question]: tokenizer转换字符串得到的input_ids列表长度不对 #8640

Closed jjy260782149 closed 3 days ago

jjy260782149 commented 1 week ago

tokenizer转换字符串得到的input_ids列表长度不对

计算交叉函数时出现错误:ValueError: (InvalidArgument) Input(Logits) and Input(Label) should in same shape in dimensions except axis. [Hint: Expected logits_dims[i] == labels_dims[i], but received logits_dims[i]:46 != labels_dims[i]:45.] (at ..\paddle\phi\infermeta\binary.cc:940)。 后来调试的时候感觉可能是使用tokenizer转换时出现问题,使用ernie-1.0的tokenizer转换时,获取到的input_ids列表长度比输入文字列表长度多3,正常应该是多2吧? 这是其中一条,我认为未转换正确的数据: 输入数据:金 华 市 婺 城 区 宾 虹 西 路 1564 号 美 保 工 具 电 商 中 心 input ids:1 192 403 99 4501 257 121 1468 2321 213 216 4340 9488 500 188 161 35 248 128 278 12 111 2 labels:46 12 13 13 14 15 15 6 7 7 7 8 9 16 17 17 17 17 17 17 17 46

创建的tokenizer

tokenizer = ErnieTokenizer.from_pretrained('ernie-1.0')

转换函数

def convert_example(example, tokenizer, label_vocab): tokens, labels = example tokenized_input = tokenizer( tokens, return_length=True, is_split_into_words=True)

Token '[CLS]' and '[SEP]' will get label 'O'

labels = ['O'] + labels + ['O']
tokenized_input['labels'] = [str(label_vocab[x]) for x in labels]
return tokenized_input['input_ids'], tokenized_input[
    'token_type_ids'], tokenized_input['seq_len'], tokenized_input['labels']
jjy260782149 commented 1 week ago

今天把input_ids和labels列表长度不一样的数据删掉了,就可以正确运行了,确认是tokenizer转化的问题,应该如何解决?

jjy260782149 commented 3 days ago

已发现是输入的数据处理有问题,我输入的数据通常会把连续的数字作为一个字符进行转换,这样转换后有时会分配一个id,有时会分配多个id;输入的数据应该为单个字符,不能为多个字符。