python - 如何更新现有的 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 部分?
解决方案
我确实遇到了一个灾难性的遗忘问题。我也通过提供其他实体标记的训练数据来解决它。
推荐阅读
- windows - 在 gradle 的 Windows 调试问题上颤振
- android - 使用 REST API 调用更新应用程序
- sql - 如何使用 SQL 在没有聚合且不知道列值的情况下对表进行 PIVOT?
- python - 使用 Matplotlib 组织轴
- git - 从 git 子树中远程 git 分支结帐
- python - Python Django 应用程序记录器记录在不同的应用程序目录中
- json - 在 Swift 中使用多个组值解码 JSON
- jquery - Clone() 离开 [object HTMLSpanElement]
- ios - 注册为 Apple 开发者 - 作为公司
- sass - SASS @each 语句中的多个变量