1 year ago

#62322

test-img

Rishabh Vimal

NameError: name 'ner' is not defined

I am new to spacy. I am trying to update NER using spacy using below code

nlp = spacy.blank('en')

def train_model(train_data):

if 'ner' not in nlp.pipe_names:
    nlp.add_pipe('ner', last=True)
    
for _, annotations in train_data:
     for ent in annotations.get('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(10):
        print("Statring iteration " + str(itn))
        random.shuffle(train_data)
        losses = {}
        for batch in spacy.util.minibatch(training_data, size=2):
            for text, annotations in batch:
                doc = nlp.make_doc(text)
                example = Example.from_dict(doc, annotations)
                nlp.update(
                    [example],  # batch of annotations
                    drop=0.2,  # dropout - make it harder to memorise data
                    sgd=optimizer,  # callable to update weights
                    losses=losses)
            print(losses)

even after adding 'ner' to pipeline I am getting error

NameError Traceback (most recent call last) in ----> 1 train_model(training_data)

in train_model(train_data) 7 for _, annotations in train_data: 8 for ent in annotations.get('entities'): ----> 9 ner.add_label(ent[2]) 10 11

NameError: name 'ner' is not defined

nlp

named-entity-recognition

spacy-3

0 Answers

Your Answer

Accepted video resources