python - 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')
我的数据样本:
原文: مساعد نائب رئيس المنزل: لم نر حتى رسالة كومي حتى غردها جيسون تشافيتز
标记化:['مساعد', 'نائب', 'رئيس', 'ال', '##منزل', ':', 'لم', 'نر', 'حتى', 'رسال', '##ة' , 'كومي', 'حتى', 'غرد', '##ها', 'جيسون', 'تشافي', '##ت', '##ز']
请问有什么建议吗?!
解决方案
您的数据似乎包含 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)
推荐阅读
- javascript - Three.js - 将多个图像映射到一个球体并控制每个图像
- hibernate - Jpa Criteria Api isFalse 和 isTrue 有什么区别?
- android-studio - 错误:注释处理器现在必须显式声明
- java - 使用字节数组创建单色位图
- java - 带有杰克逊 JsonProperty 的 Lombok 构建器模式
- java - 无法将服务自动装配到 AsyncUncaughtExceptionHandler 的实现中
- timer - 1ms定时器触发的ADC转换不工作,STM32F4
- vba - 如何使用 Access VBA 从多个表中填充单词/填充单词
- php - PHP OOP:如何构建需要另一个 Mapper 功能的 Mapper
- python - 为什么使用 get_context_data(self,**kwargs) 和 super()