我对伯特语言模型很陌生。我目前正在使用Huggingface库,并且在编码输入时遇到了一个错误。该模型的目标是对假新闻进行分类。
首先,我下载了数据集,并将其转化为包含3列的熊猫数据。索引,推特,标签。使用bert大容量的预训练自动令牌器对输入进行编码。
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提供的beolow是准确的错误消息。
---------------------------------------------------------------------------
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, key, method, 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任何关于如何调试的见解都是受欢迎的。
发布于 2022-01-12 21:07:13
使用:train.index和test.index打印索引
有时,索引是不连续的,因为您可能有来自不同来源的组合表。您可以通过键入
train.reset_index(drop=True, inplace=True)
test.reset_index(drop=True, inplace=True)
如果需要保留train和test的原始索引,请在拆分为train和test之前执行此步骤。
https://stackoverflow.com/questions/67531678
复制相似问题