首页 > 解决方案 > 如何更新现有的 spacy 模型?

问题描述

我正在为我的硕士论文开发一个命名实体识别功能。我想利用“en_core_web_sm”语言包并训练识别产品的能力。

在训练模型之前,未经训练的模型(“en_core_web_sm”)能够在“验证句”中识别不同的实体,如“PERSON”、“ORG”、“GPE”、“DATE”……。产品偶尔会被识别,但通常标签不正确。在训练模型后,仅识别出“产品”类型的实体,但没有其他实体,尽管应该识别出人员、组织、...。

我感觉我的模型忘记了其他实体,并且在训练之后“知道”了产品实体。

这是我的训练代码:

#nlp = spacy.load("en_core_web_lg")
nlp = spacy.load("en_core_web_sm")
ner = nlp.get_pipe("ner")

optimizer = nlp.create_optimizer()
#other_pipes = [pipe for pipe in nlp.pipe_names if pipe != "ner"]

pipe_exceptions = ["ner", "trf_wordpiecer", "trf_tok2vec"]
other_pipes = [pipe for pipe in nlp.pipe_names if pipe not in pipe_exceptions]
losses = {}
with nlp.disable_pipes(*other_pipes): # only train NER
    for itn in range(100):
        print(itn)
        random.shuffle(TRAIN_DATA)
        losses = {}
        for text, annotations in TRAIN_DATA:
            doc = nlp.make_doc(text)
            example = Example.from_dict(doc, annotations)
            nlp.update([example], drop=0.35, sgd=optimizer, losses=losses)
            print(losses)

如何更新默认的“en_core_web_sm”模型以保持识别 PERSON、DATE、ORG 的能力……但适应经过训练的 PRODUCT 部分?

标签: pythonspacy

解决方案


我确实遇到了一个灾难性的遗忘问题。我也通过提供其他实体标记的训练数据来解决它。


推荐阅读