【问题标题】:PyTorch Huggingface BERT-NLP for Named Entity Recognition用于命名实体识别的 PyTorch Huggingface BERT-NLP
【发布时间】:2019-07-19 07:13:05
【问题描述】:

我已经为 MADE 1.0 数据集使用 Google BERT by HuggingFace 的 PyTorch 实现已经有一段时间了。直到上次(2 月 11 日),我一直在使用该库,并通过微调模型为我的命名实体识别任务获得了 0.81F-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: 令牌索引序列长度大于指定 此 BERT 模型的最大序列长度(632 > 512)。运行这个 通过 BERT 的序列会导致索引错误

完整代码可在此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 已降至 0.40 和我的 precision0.27,而 Recall 保持不变 (0.85)。我无法共享数据集,因为我已经签署了保密条款,但我可以确保 BERT 要求的所有预处理都已完成,并且所有扩展标记(如 (Johanson --> Johan ##son))都已标记为 X 并替换在BERT Paper中所说的预测之后。

有没有其他人遇到过类似的问题,或者可以详细说明可能是什么问题,或者 PyTorch (Huggingface) 人们最近做了哪些改变?

【问题讨论】:

    标签: python nlp data-science named-entity-recognition huggingface-transformers


    【解决方案1】:

    我找到了解决此问题的方法。 使用 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

    谢谢。

    【讨论】:

      【解决方案2】:

      我认为您应该使用batch_encode_plus 和掩码输出以及编码。

      请看https://huggingface.co/transformers/main_classes/tokenizer.html中的batch_encode_plus

      【讨论】:

        猜你喜欢
        • 2018-03-08
        • 2020-07-02
        • 2019-11-12
        • 1970-01-01
        • 2020-02-01
        • 2017-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多