gpt4 book ai didi

python - spaCy v3 基于现有模型训练 NER 或将自定义训练的 NER 添加到现有模型

转载 作者:行者123 更新时间:2023-12-04 17:21:17 30 4
gpt4 key购买 nike

在 spaCy < 3.0 中,我能够在经过训练的 en_core_web_sm 模型中训练 NER 组件:

python -m spacy train en model training validation --base-model en_core_web_sm --pipeline "ner" -R -n 10
具体来说,我需要标记器和 en_core_web_sm 模型的解析器。
spaCy 的新版本不再使用这些命令,它们需要在配置文件中进行设置。根据 spaCy 的网站,可以将这些组件与相应的源一起添加,然后插入到配置文件训练部分中的frozen_component(我将在此问题的末尾提供我的完整配置):
[components]

[components.tagger]
source = "en_core_web_sm"
replace_listeners = ["model.tok2vec"]

[components.parser]
source = "en_core_web_sm"
replace_listeners = ["model.tok2vec"]
.
.
.
[training]
frozen_components = ["tagger","parser"]
当我调试时,出现以下错误:
ValueError: [E922] Component 'tagger' has been initialized with an output dimension of 49 - cannot add any more labels.
当我将标记器放入配置文件的 nlp 部分中的禁用组件时,或者如果我删除了与标记器相关的所有内容,调试和训练就可以工作了。但是,当将经过训练的模型应用于加载到文档的文本时,只有经过训练的 NER 有效,其他组件均无效。例如。解析器预测一切都是 ROOT。
我还尝试自己训练 NER 模型,然后将其添加到加载的 en_core_web_sm 模型中:
MODEL_PATH = 'data/model/model-best'
nlp = spacy.load(MODEL_PATH)

english_nlp = spacy.load("en_core_web_sm")

ner_labels = nlp.get_pipe("ner")
english_nlp.add_pipe('ner_labels')
这会导致以下错误:
ValueError: [E002] Can't find factory for 'ner_labels' for language English (en). This usually happens when spaCy calls `nlp.create_pipe` with a custom component name that's not registered on the current language class. If you're using a Transformer, make sure to install 'spacy-transformers'. If you're using a custom component, make sure you've added the decorator `@Language.component` (for function components) or `@Language.factory` (for class components).

Available factories: attribute_ruler, tok2vec, merge_noun_chunks, merge_entities, merge_subtokens, token_splitter, parser, beam_parser, entity_linker, ner, beam_ner, entity_ruler, lemmatizer, tagger, morphologizer, senter, sentencizer, textcat, textcat_multilabel, en.lemmatizer
有没有人建议我如何使用 en_core_web_sm 模型训练我的 NER 或如何集成我训练过的组件?
这是完整的配置文件:
[paths]
train = "training"
dev = "validation"
vectors = null
init_tok2vec = null

[system]
gpu_allocator = null
seed = 0

[nlp]
lang = "en"
pipeline = ["tok2vec","tagger","parser","ner"]
batch_size = 1000
disabled = []
before_creation = null
after_creation = null
after_pipeline_creation = null
tokenizer = {"@tokenizers":"spacy.Tokenizer.v1"}

[components]

[components.tagger]
source = "en_core_web_sm"
replace_listeners = ["model.tok2vec"]

[components.parser]
source = "en_core_web_sm"
replace_listeners = ["model.tok2vec"]

[components.ner]
factory = "ner"
moves = null
update_with_oracle_cut_size = 100

[components.ner.model]
@architectures = "spacy.TransitionBasedParser.v2"
state_type = "ner"
extra_state_tokens = false
hidden_width = 64
maxout_pieces = 2
use_upper = true
nO = null

[components.ner.model.tok2vec]
@architectures = "spacy.Tok2VecListener.v1"
width = ${components.tok2vec.model.encode.width}
upstream = "*"

[components.tok2vec]
factory = "tok2vec"

[components.tok2vec.model]
@architectures = "spacy.Tok2Vec.v2"

[components.tok2vec.model.embed]
@architectures = "spacy.MultiHashEmbed.v1"
width = ${components.tok2vec.model.encode.width}
attrs = ["NORM","PREFIX","SUFFIX","SHAPE"]
rows = [5000,2500,2500,2500]
include_static_vectors = false

[components.tok2vec.model.encode]
@architectures = "spacy.MaxoutWindowEncoder.v2"
width = 256
depth = 8
window_size = 1
maxout_pieces = 3

[corpora]

[corpora.dev]
@readers = "spacy.Corpus.v1"
path = ${paths.dev}
max_length = 0
gold_preproc = false
limit = 0
augmenter = null

[corpora.train]
@readers = "spacy.Corpus.v1"
path = ${paths.train}
max_length = 2000
gold_preproc = false
limit = 0
augmenter = null

[training]
dev_corpus = "corpora.dev"
train_corpus = "corpora.train"
seed = ${system.seed}
gpu_allocator = ${system.gpu_allocator}
dropout = 0.1
accumulate_gradient = 1
patience = 1600
max_epochs = 0
max_steps = 20000
eval_frequency = 200
frozen_components = ["tagger","parser"]
before_to_disk = null

[training.batcher]
@batchers = "spacy.batch_by_words.v1"
discard_oversize = false
tolerance = 0.2
get_length = null

[training.batcher.size]
@schedules = "compounding.v1"
start = 100
stop = 1000
compound = 1.001
t = 0.0

[training.logger]
@loggers = "spacy.ConsoleLogger.v1"
progress_bar = false

[training.optimizer]
@optimizers = "Adam.v1"
beta1 = 0.9
beta2 = 0.999
L2_is_weight_decay = true
L2 = 0.01
grad_clip = 1.0
use_averages = false
eps = 0.00000001
learn_rate = 0.001

[training.score_weights]
ents_per_type = null
ents_f = 1.0
ents_p = 0.0
ents_r = 0.0

[pretraining]

[initialize]
vectors = "en_core_web_lg"
init_tok2vec = ${paths.init_tok2vec}
vocab_data = null
lookups = null
before_init = null
after_init = null

[initialize.components]

[initialize.tokenizer]

最佳答案

我在 spaCy 的论坛上提供了更长的答案 here ,但简而言之,如果您想获取和卡住解析器/标记器,请在配置中使用它:

[components.tagger]
source = "en_core_web_sm"
replace_listeners = ["model.tok2vec"]

[components.parser]
source = "en_core_web_sm"
replace_listeners = ["model.tok2vec"]

[components.tok2vec]
source = "en_core_web_sm"
即确保标记器和解析器可以连接到正确的 tok2vec例如,他们最初接受过培训。
然后,您可以在源(和预训练) tok2vec 之上创建一个独立的 NER 组件。 ,或创建一个新的内部 tok2vec NER 的组件,或创建第二个 tok2vec具有不同名称的组件,您将其称为 upstream NER 的论据 Tok2VecListener .

关于python - spaCy v3 基于现有模型训练 NER 或将自定义训练的 NER 添加到现有模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66090423/

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