我正在为spacy NER培训新实体。我尝试将我的新实体添加到现有的spacy'en'模型中。但是,这影响了'en'
和我的新实体的预测模型。
一、 为此,建立了一个空白模型并训练了实体识别。这很有效。然而,它只能预测我训练过的那些,而不能预测常规的空间实体识别。在
比如说我把“马”训练成动物实体。在
对于给定的文本
txt ='Did you know that George bought those horses for 10000 dollars?'
我希望能识别以下实体
^{pr2}$以我目前的设置,它只识别马。在
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*
请让我知道如何在spacy中有效地为NER创建管道。 我正在使用spacy 2.0.18
主要问题是如何加载和组合管道组件,使它们使用相同的
Vocab
(nlp.vocab
),因为管道假设所有组件共享相同的词汇,否则您可能会得到与StringStore
相关的错误。在您不应该尝试组合使用不同单词向量训练的管道组件,但只要向量相同,问题是如何从具有相同词汇的不同模型加载组件。在
使用
spacy.load()
无法做到这一点,因此我认为最简单的选择是用所需的voab初始化一个新的管道组件,并通过临时序列化将现有组件重新加载到新组件中。在为了使用易于访问的模型进行简短的演示,我将演示如何将德语NER模型从
de_core_news_sm
添加到英文模型en_core_web_sm
中,尽管这不是您通常想要做的事情:Spacy的NER组件(},并按照英语NER的预测保留所有其他实体。在
EntityRuler
和EntityRecognizer
)是为了保留任何现有的实体而设计的,因此新组件只添加了带有德语NER标记PER
的{您可以使用
^{pr2}$add_pipe()
的选项来确定组件在管道中的插入位置。要在默认英语NER之前添加德语NER,请执行以下操作:所有
add_pipe()
选项都在文档中:https://spacy.io/api/language#add_pipe您可以将扩展管道保存为单个模型,以便下次可以用
spacy.load()
在一行中加载它:相关问题 更多 >
编程相关推荐