我一直在使用 Google 的 BERT 的 PyTorch 实现通过 HuggingFace对于 MADE 1.0 数据集已经有一段时间了。直到上次(2 月 11 日),我一直在使用该库并通过微调模型获得了 0.81 的F-Score,用于我的命名实体识别任务。但是这周当我运行与之前编译和运行的完全相同的代码时,它在执行这条语句时抛出了一个错误:
input_ids = pad_sequences([tokenizer.convert_tokens_to_ids(txt) for txt in tokenized_texts], maxlen=MAX_LEN, dtype="long", truncating="post", padding="post")
ValueError: Token indices sequence length is longer than the specified maximum sequence length for this BERT model (632 > 512). Running this sequence through BERT will result in indexing errors
完整代码可在 colab notebook 中找到.
为了解决这个错误,我通过获取任何序列的前 512 个标记将上面的语句修改为下面的语句,并进行必要的更改以根据需要将 [SEP] 的索引添加到截断/填充序列的末尾由 BERT 提供。
input_ids = pad_sequences([tokenizer.convert_tokens_to_ids(txt[:512]) for txt in tokenized_texts], maxlen=MAX_LEN, dtype="long", truncating="post", padding="post")
结果不应该改变,因为我只考虑序列中的前 512 个标记,然后截断为 75 作为我的 (MAX_LEN=75),但我的 F-Score 已降至 < strong>0.40,我的精度为0.27,而召回率保持不变(0.85)。我无法共享数据集,因为我已经签署了保密条款,但我可以确保 BERT 要求的所有预处理都已完成,并且所有扩展 token (如 (Johanson --> Johan ##son))都已用 X 标记并替换后来在 BERT Paper 中所说的预测之后.
有没有其他人遇到过类似的问题,或者可以详细说明可能是什么问题,或者 PyTorch (Huggingface) 的人们最近在他们这边做了什么改变?
请您参考如下方法:
我找到了解决这个问题的方法。 使用 pytorch-pretrained-bert==0.4.0 运行相同的代码可以解决问题并且性能恢复正常。 在影响模型性能的新更新中,BERT Tokenizer 或 BERTForTokenClassification 中的模型性能有些困惑。 希望 HuggingFace 尽快解决这个问题。 :)
pytorch-pretrained-bert==0.4.0,测试F1-Score:0.82
pytorch-pretrained-bert==0.6.1,测试F1-Score:0.41
谢谢。
