如何解决BERT 转换器密钥错误:3
我对 BERT 语言模型很陌生。我目前正在使用 Huggingface 转换器库,但在对输入进行编码时遇到错误。该模型的目标是对假新闻进行分类。
首先,我下载了数据集,然后将其转换为包含 3 列的 Pandas 数据框。索引、推文、标签。来自 bert large uncased 的预训练自动分词器用于对输入进行编码。
TOKENIZER = AutoTokenizer.from_pretrained("bert-large-uncased")
使用了以下函数:
def bert_encode(data,maximum_len) :
input_ids = []
attention_masks = []
for i in range(len(data.tweet)):
encoded = TOKENIZER.encode_plus(data.tweet[i],add_special_tokens=True,max_length=maximum_len,pad_to_max_length=True,return_attention_mask=True,truncation=True)
input_ids.append(encoded['input_ids'])
attention_masks.append(encoded['attention_mask'])
return np.array(input_ids),np.array(attention_masks)
该函数应用于数据以获取训练输入 id 和注意力掩码:
train_input_ids,train_attention_masks = bert_encode(train,600)
test_input_ids,test_attention_masks = bert_encode(test,600)
但是,调用该函数会出现以下错误:KeyError: 3 下面提供的是确切的错误消息。
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self,key,method,tolerance)
2897 try:
-> 2898 return self._engine.get_loc(casted_key)
2899 except KeyError as err:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 3
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
4 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self,tolerance)
2898 return self._engine.get_loc(casted_key)
2899 except KeyError as err:
-> 2900 raise KeyError(key) from err
2901
2902 if tolerance is not None:
KeyError: 3
欢迎任何有关如何调试的见解。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。