nlp - 使用 encode_plus 方法时的标记索引序列长度错误
问题描述
encode_plus
尝试使用Transformers 库中提供的方法为 BERT 编码问答对时出现一个奇怪的错误。
我正在使用这个 Kaggle 比赛的数据。给定问题标题、问题正文和答案,模型必须预测 30 个值(回归问题)。我的目标是获取以下编码作为 BERT 的输入:
[CLS] question_title question_body [SEP] 答案 [SEP]
但是,当我尝试使用
tokenizer = transformers.BertTokenizer.from_pretrained("bert-base-uncased")
并仅对来自 train.csv 的第二个输入进行编码,如下所示:
inputs = tokenizer.encode_plus(
df_train["question_title"].values[1] + " " + df_train["question_body"].values[1], # first sequence to be encoded
df_train["answer"].values[1], # second sequence to be encoded
add_special_tokens=True, # [CLS] and 2x [SEP]
max_len = 512,
pad_to_max_length=True
)
我收到以下错误:
Token indices sequence length is longer than the specified maximum sequence length for this model (46 > 512). Running this sequence through the model will result in indexing errors
它说令牌索引的长度比指定的最大序列长度长,但这不是真的(如您所见,46 不 > 512)。
这发生在df_train
. 我在这里做错了吗?
解决方案
模型“bert-base-uncased”未经过预训练以处理 [CLS] + Question + [SEP] + Context + [SEP] 的长文本。Huggingface 模型中专门用于小队问答数据集的任何其他模型都可以处理长序列。
例如,如果我使用 ALBERT 模型,我会选择“ktrapeznikov/albert-xlarge-v2-squad-v2”模型。
推荐阅读
- flutter - 如何通过下拉菜单隐藏字段
- verilog - 如何按 type_id 过滤 uvm_info 消息?
- javascript - 我想将角度分量作为对象的属性值传递
- javascript - 地图功能没有为每个元素设置值
- php - Symfony:在所有数组中获得相同的功能
- prolog - PROLOG - 获取实体验证的所有规则的列表
- reactjs - Material-UI 连续动画
- robotframework - 是否有设置获取机器人框架以使用 JMSLibrary 的示例?
- javascript - 使用 javascript 或 jquery 在页面离开时禁用 Firefox 对话框
- javascript - 未捕获的类型错误:fs.writeFile 不是函数