我正在尝试训练新的Spence 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 2.0.18
答案 0 :(得分:1)
主要问题是如何加载和组合管道组件,使它们使用相同的Vocab
(nlp.vocab
),因为管道假定所有组件都共享相同的vocab,否则您将获得与StringStore
有关的错误。
您不应该尝试结合使用不同的单词向量训练的管道组件,但是只要向量相同,这就是如何从具有相同vocab的不同模型中加载组件的问题。
无法使用spacy.load()
来做到这一点,所以我认为最简单的选择是使用所需的vocab初始化一个新的管道组件,并通过临时序列化将现有的组件重新加载到新的组件中。
要使用易于访问的模型进行简短的演示,我将向您展示如何将de_core_news_sm
的German 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组件(EntityRuler
和EntityRecognizer
)旨在保留任何现有实体,因此新组件仅添加带有德国NER标签Jan lives
的{{1}}并保留所有其他实体都由英语NER预测。
您可以使用PER
的选项来确定组件在管道中的插入位置。要在默认英语NER之前添加德语NER:
add_pipe()
所有nlp_en.add_pipe(ner_de, name="ner_de", before="ner")
# [('Jane lives', 'PER'), ('Boston', 'LOC'), ('Jan lives', 'PER'), ('Bremen', 'LOC')]
选项都在文档中:https://spacy.io/api/language#add_pipe
您可以将扩展管道作为单个模型保存到磁盘,以便下次使用add_pipe()
将其装入一行:
spacy.load()