【发布时间】:2021-11-09 19:58:26
【问题描述】:
我是 SpaCy 和 NLP 的新手。我正在使用 SpaCy v 3.1 和 Python 3.9.7 64 位。
我的目标:使用预训练的 SpaCy 模型 (en_core_web_sm) 并向现有的 NER 标签添加一组自定义标签(GPE、PERSON、MONEY等),以便模型可以识别默认实体和自定义实体。
我查看了 SpaCy 文档,我需要的是 EntityRecogniser,特别是新管道。
但是,我并不清楚应该在工作流程的哪个阶段添加这个新管道,因为在 SpaCy 3 中,培训是在 CLI 中进行的,而且从文档中我甚至不清楚预培训的位置模型被调用。
我们非常感谢您提供的任何教程或建议。
这是我认为应该做的,但我不确定如何做:
import spacy
from spacy import displacy
from spacy_langdetect import LanguageDetector
from spacy.language import Language
from spacy.pipeline import EntityRecognizer
# Load model
nlp = spacy.load("en_core_web_sm")
# Register custom component and turn a simple function into a pipeline component
@Language.factory('new-ner')
def create_bespoke_ner(nlp, name):
# Train the new pipeline with custom labels here??
return LanguageDetector()
# Add custom pipe
custom = nlp.add_pipe("new-ner")
这是我的配置文件到目前为止的样子。我怀疑我的新管道需要放在“tok2vec”和“ner”旁边。
[paths]
train = null
dev = null
vectors = null
init_tok2vec = null
[system]
gpu_allocator = null
seed = 0
[nlp]
lang = "en"
pipeline = ["tok2vec","ner"]
batch_size = 1000
disabled = []
before_creation = null
after_creation = null
after_pipeline_creation = null
tokenizer = {"@tokenizers":"spacy.Tokenizer.v1"}
[components]
[components.ner]
factory = "ner"
incorrect_spans_key = null
moves = null
update_with_oracle_cut_size = 100
【问题讨论】:
-
单独为新的 NER 标签训练模型,然后将其与预训练模型一起包含在管道中。有关如何组合此类模型的信息,请参阅双 NER 示例项目,有关如何训练单个模型,请参阅它所指的药物名称 NER 示例。 github.com/explosion/projects/tree/v3/tutorials/ner_double
标签: python nlp spacy named-entity-recognition