首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >伯特变压器KeyError: 3

伯特变压器KeyError: 3
EN

Stack Overflow用户
提问于 2021-05-14 08:58:46
回答 1查看 1K关注 0票数 3

我对伯特语言模型很陌生。我目前正在使用Huggingface库,并且在编码输入时遇到了一个错误。该模型的目标是对假新闻进行分类。

首先,我下载了数据集,并将其转化为包含3列的熊猫数据。索引,推特,标签。使用bert大容量的预训练自动令牌器对输入进行编码。

代码语言:javascript
复制
TOKENIZER = AutoTokenizer.from_pretrained("bert-large-uncased")

所使用的功能如下:

代码语言:javascript
复制
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和注意掩码:

代码语言:javascript
复制
train_input_ids,train_attention_masks = bert_encode(train,600)
test_input_ids,test_attention_masks = bert_encode(test,600)

但是,调用该函数会给出以下错误: KeyError: 3提供的beolow是准确的错误消息。

代码语言:javascript
复制
---------------------------------------------------------------------------
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

任何关于如何调试的见解都是受欢迎的。

EN

回答 1

Stack Overflow用户

发布于 2022-01-12 21:07:13

使用:train.indextest.index打印索引

有时,索引是不连续的,因为您可能有来自不同来源的组合表。您可以通过键入

train.reset_index(drop=True, inplace=True)

test.reset_index(drop=True, inplace=True)

如果需要保留traintest的原始索引,请在拆分为traintest之前执行此步骤。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67531678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档