簡體   English   中英

Bert 標記錯誤 ValueError:輸入 nan 無效。 應該是字符串、字符串列表/元組或整數列表/元組

[英]Bert Tokenizing error ValueError: Input nan is not valid. Should be a string, a list/tuple of strings or a list/tuple of integers

我正在使用 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')

我的數據樣本:

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

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

請問有什么建議嗎?!

您的數據似乎包含 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM