如何在Spacy中创建多模型的NER管道 [英] How to create NER pipeline with multiple models in Spacy

查看:16
本文介绍了如何在Spacy中创建多模型的NER管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试培训Spacy Ner的新实体。我尝试将我的新实体添加到现有的Spacy‘en’模型中。但是,这影响了'en'和我的新实体的预测模型。

因此,我创建了一个空白模型,并训练了实体识别。这样做效果很好。然而,它只能预测我训练过的那些,而不能预测常规的空间实体识别。

假设我把‘马’训练成动物实体。

对于给定文本

txt ='Did you know that George bought those horses for 10000 dollars?'

我希望识别以下实体

George - PERSON
horses - ANIMAL
10000 dollars - MONEY.

在我目前的设置中,它只能识别马。

nlp = spacy.load('en')
hsnlp = spacy.load('models/spacy/animal/')
nlp.add_pipe(hsnlp.pipeline[-1][-1], 'hsner')

nlp.pipe_names

这给了

----------------------
['tagger', 'parser', 'ner', 'hsner']
----------------------

但是,当我尝试执行

doc = nlp(txt)  *<-- Gives me kernel error and stops working*

请让我知道如何有效地创建NER在Spacy的管道。 我正在使用Spacy 2.0.18

推荐答案

主要问题是如何加载和组合管道组件,使它们使用相同的Vocab(nlp.vocab),因为管道假定所有组件共享相同的词汇,否则可能会出现与StringStore相关的错误。

您不应尝试组合使用不同单词向量训练的流水线组件,但只要向量相同,问题就在于如何从具有相同单词的不同模型加载组件。

使用spacy.load()无法做到这一点,因此我认为最简单的选择是使用所需的词汇标签初始化新的管道组件,并通过临时序列化将现有组件重新加载到新组件中。

为了使用易于访问的模型进行简短的工作演示,我将展示如何将de_core_news_sm中的德语NER模型添加到英语模型en_core_web_sm中,尽管这不是您通常想要做的事情:

import spacy # tested with v2.2.3
from spacy.pipeline import EntityRecognizer

text = "Jane lives in Boston. Jan lives in Bremen."

# load the English and German models
nlp_en = spacy.load('en_core_web_sm')  # NER tags PERSON, GPE, ...
nlp_de = spacy.load('de_core_news_sm') # NER tags PER, LOC, ...

# the Vocab objects are not the same
assert nlp_en.vocab != nlp_de.vocab

# but the vectors are identical (because neither model has vectors)
assert nlp_en.vocab.vectors.to_bytes() == nlp_de.vocab.vectors.to_bytes()

# original English output
doc1 = nlp_en(text)
print([(ent.text, ent.label_) for ent in doc1.ents])
# [('Jane', 'PERSON'), ('Boston', 'GPE'), ('Bremen', 'GPE')]

# original German output (the German model makes weird predictions for English text)
doc2 = nlp_de(text)
print([(ent.text, ent.label_) for ent in doc2.ents])
# [('Jane lives', 'PER'), ('Boston', 'LOC'), ('Jan lives', 'PER'), ('Bremen', 'LOC')]

# initialize a new NER component with the vocab from the English pipeline
ner_de = EntityRecognizer(nlp_en.vocab)

# reload the NER component from the German model by serializing
# without the vocab and deserializing using the new NER component
ner_de.from_bytes(nlp_de.get_pipe("ner").to_bytes(exclude=["vocab"]))

# add the German NER component to the end of the English pipeline
nlp_en.add_pipe(ner_de, name="ner_de")

# check that they have the same vocab
assert nlp_en.vocab == ner_de.vocab

# combined output (English NER runs first, German second)
doc3 = nlp_en(text)
print([(ent.text, ent.label_) for ent in doc3.ents])
# [('Jane', 'PERSON'), ('Boston', 'GPE'), ('Jan lives', 'PER'), ('Bremen', 'GPE')]

Spacy的NER组件(EntityRulerEntityRecognizer)旨在保留任何现有实体,因此新组件仅添加带有德语NER标签PERJan lives,并保留英语NER预测的所有其他实体。

您可以使用add_pipe()的选项来确定组件在管道中的插入位置。要在默认英语NER之前添加德语NER:

nlp_en.add_pipe(ner_de, name="ner_de", before="ner")
# [('Jane lives', 'PER'), ('Boston', 'LOC'), ('Jan lives', 'PER'), ('Bremen', 'LOC')]

所有add_pipe()选项都在文档中:https://spacy.io/api/language#add_pipe

您可以将扩展管道作为单个模型保存到磁盘,这样您就可以在下一次使用spacy.load()一行加载它:

nlp_en.to_disk("/path/to/model")
nlp_reloaded = spacy.load("/path/to/model")
print(nlp_reloaded.pipe_names) # ['tagger', 'parser', 'ner', 'ner_de']

这篇关于如何在Spacy中创建多模型的NER管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆