gpt4 book ai didi

python - 如何在 Spacy 中创建具有多个模型的 NER 管道

转载 作者:行者123 更新时间:2023-12-03 18:50:10 27 4
gpt4 key购买 nike

我正在尝试为 spacy NER 训练新实体。我尝试将我的新实体添加到现有的 spacy 'en' 模型中。然而,这影响了 'en' 的预测模型。和我的新实体。
因此,我创建了一个空白模型并训练了实体识别。这很好用。然而,它只能预测我训练过的那些,而不是常规的空间实体识别。

假设我将“马”训练为 ANIMAL 实体。

对于给定的文本

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*

请让我知道如何有效地为 spacy 中的 NER 创建管​​道。
正在使用 空间 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 )旨在保留任何现有实体,因此新组件仅添加 Jan lives带有德国 NER 标签 PER并按照英语 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']

关于python - 如何在 Spacy 中创建具有多个模型的 NER 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54855780/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com