Обновление уже существующей пространственной модели NER

Я хочу обновить уже существующую пространственную модель en_core_web_sm и обучить ее дополнительным данным.

Мои данные находятся в том же формате, который указан в документации spacy https://spacy.io/usage/training

Я выполнил те же шаги, что и в документации, для обновления модели NER с моими данными.

def model_train(output_dir=None, n_iter=100):
    """Load the model, set up the pipeline and train the entity recognizer."""
    model=('en_core_web_sm')
    nlp = spacy.load(model, entity = False, parser = False)  # load existing spaCy model
    print("Loaded model '%s'" % model)
    print (nlp.pipe_names)

#     # create the built-in pipeline components and add them to the pipeline

    ner = nlp.get_pipe("ner")

#     # add labels
    for texts, annotations in TRAIN_DATA:        
        for ent in annotations.get("entities"):
#             print (ent)
            ner.add_label(ent[2])
#             print (ent[2])

    other_pipes = [pipe for pipe in nlp.pipe_names if pipe != "ner"]
    with nlp.disable_pipes(*other_pipes):  # only train NER

        if model is None:
            nlp.begin_training()
        for itn in range(n_iter):
            random.shuffle(TRAIN_DATA)
            losses = {}
#             # batch up the examples using spaCy's minibatch
            batches = minibatch(TRAIN_DATA, size=compounding(4.0, 32.0, 1.001))
            for batch in batches:
                texts, annotations = zip(*batch)
                nlp.update(
                    [texts],  # batch of texts
                    [annotations],  # batch of annotations
                    drop=0.5,  # dropout - make it harder to memorise data
                    losses=losses,
                )
            print("Losses", losses)


        nlp.to_disk(output_dir)
        print("Saved model to", output_dir)

Ошибка, которую я получаю,

Loaded model 'en_core_web_sm'
['tagger', 'parser', 'ner']
------------------------------------------------------------------------- 

- TypeError Traceback (последний вызов последним) в ----> 1 model_train ()

<ipython-input-337-91366511ed4d> in model_train(output_dir, n_iter)
     56                     [annotations],  # batch of annotations
     57                     drop=0.5,  # dropout - make it harder to 
memorise data
---> 58                     losses=losses,
     59                 )
     60             print("Losses", losses)

C:\ProgramData\Anaconda3\lib\site-packages\spacy\language.py in 
update(self, docs, golds, drop, sgd, losses, component_cfg)
    432                 doc = self.make_doc(doc)
    433             if not isinstance(gold, GoldParse):
--> 434                 gold = GoldParse(doc, **gold)
    435             doc_objs.append(doc)
    436             gold_objs.append(gold)

TypeError: type object argument after ** must be a mapping, not tuple

person Ruchi Awasthi    schedule 27.05.2019    source источник
comment
Вы поняли это?   -  person Jeong Kim    schedule 08.02.2020


Ответы (1)


zip () уже отправляет список текстов и аннотаций

nlp.update(
                    texts,  # batch of texts
                    annotations,  # batch of annotations
                    drop=0.5,  # dropout - make it harder to memorise data
                    losses=losses,
                )

Нажмите здесь

person Sai Ram    schedule 20.02.2020