首页 > 解决方案 > 如何解决错误:nlp.add_pipe` 现在采用已注册组件工厂的字符串名称,而不是可调用组件

问题描述

我正在使用空白的 spacy 模型来训练我自己的 ner 数据。我正在训练我的模型以获取来自 train_data 的实体。

nlp = spacy.blank('en')

def train_model(train_data) :
    if 'ner' not in nlp.pipe_names:
        ner = nlp.create_pipe('ner')
        nlp.add_pipe(ner)        
    
    for _ ,annotation in train_data :
        for ent in annotation['entities']:
            ner.add_label(ent[2])
           
    other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']
    with nlp.disable_pipes(*other_pipes):  # only train NER
        optimizer = nlp.begin_training()
        for itn in range(15):
            print("Starting Iteration"   + str(itn))
            random.shuffle(train_data)
            losses = {}
            index = 0
        
            for text, annotations in train_data :
                try:
                    nlp.update([text], [annotations], sgd=optimizer, drop=0.20,
                       losses=losses)
                except Exception as e:
                    pass
            print('Losses', losses)    

我收到以下错误。 nlp.add_pipe现在采用已注册组件工厂的字符串名称,而不是可调用组件。预期的字符串,但在 0x0000022969A29C88> 处得到 <spacy.pipeline.ner.EntityRecognizer 对象>(名称:'None')。

标签: pythonparsingspacynamed-entity-recognition

解决方案


关于错误 - 替换

ner = nlp.create_pipe('ner')
nlp.add_pipe(ner)

ner = nlp.add_pipe('ner')

推荐阅读