首页 > 解决方案 > Bert 标记化错误 ValueError:输入 nan 无效。应该是字符串、字符串列表/元组或整数列表/元组

问题描述

当我尝试使用代码标记一个数据样本时,我正在使用 Bert 进行文本分类任务:

encoded_sent = tokenizer.encode(
                        sentences[7],                       
                        add_special_tokens = True)

它进展顺利,但是当我尝试使用代码标记整个数据时:

# For every sentence...
for sent in sentences:
    
    encoded_sent = tokenizer.encode(
                        sent,                       
                        add_special_tokens = True)

它给了我错误:

"ValueError: Input nan is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers."

我尝试了被某人成功标记的英文数据,但我得到了同样的错误。这就是我加载数据的方式:

import pandas as pd

df=pd.read_csv("/content/DATA.csv",header=0,dtype=str)
DATA_COLUMN = 'sentence'
LABEL_COLUMN = 'label'
df.columns = [DATA_COLUMN, LABEL_COLUMN]

df["sentence"].head

这就是我加载标记器的方式:

# Load the BERT tokenizer.
print('Loading BERT tokenizer...')
tokenizer = AutoTokenizer.from_pretrained('aubmindlab/bert-base-arabert')

我的数据样本:

原文: مساعد نائب رئيس المنزل: لم نر حتى رسالة كومي حتى غردها جيسون تشافيتز

标记化:['مساعد', 'نائب', 'رئيس', 'ال', '##منزل', ':', 'لم', 'نر', 'حتى', 'رسال', '##ة' , 'كومي', 'حتى', 'غرد', '##ها', 'جيسون', 'تشافي', '##ت', '##ز']

请问有什么建议吗?!

标签: pythonnlpclassificationtokenizebert-language-model

解决方案


您的数据似乎包含 NAN 值,要解决此问题,您必须消除 NAN 值或将所有数据转换为字符串(本地解决方案)。

尝试使用:

encoded_sent = tokenizer.encode(
        str(sent),                       
        add_special_tokens = True)

如果您确定数据集不计算 NAN 值,您可以使用该解决方案,或者检测您的数据集是否包含您可能使用的 NAN 值:

for sent in sentences: 
    print(sent) 
    encoded_sent = tokenizer.encode( sent, add_special_tokens = True)

推荐阅读