pythonspacynamed-entity-extraction

Spacy training model


I want to create my own training model of spacy. with my following code, I am getting error.

TRAIN_DATA = [
    ("Uber blew through $1 million a week", [(0, 4, 'ORG')]),
    ("Android Pay expands to Canada", [(0, 11, 'PRODUCT'), (23, 30, 'GPE')]),
    ("Spotify steps up Asia expansion", [(0, 8, "ORG"), (17, 21, "LOC")]),
    ("Google Maps launches location sharing", [(0, 11, "PRODUCT")]),
    ("Google rebrands its business apps", [(0, 6, "ORG")]),
    ("look what i found on google!", [(21, 27, "PRODUCT")])]

nlp = spacy.blank("en")
optimizer = nlp.begin_training()
for i in range(20):
    random.shuffle(TRAIN_DATA)
    for text, annotations in TRAIN_DATA:
        nlp.update([text], [annotations], sgd=optimizer)
nlp.to_disk("/model")

I am getting following error which is i don't get when i used simple training loop on this site https://spacy.io/usage/training it works

Output:

ValueError                                Traceback (most recent call last)
<ipython-input-53-92de7863a1cf> in <module>
     12     random.shuffle(TRAIN_DATA)
     13     for text, annotations in TRAIN_DATA:
---> 14         nlp.update([text], [annotations], sgd=optimizer)
     15 nlp.to_disk("/model")

c:\python3.6\lib\site-packages\spacy\language.py in update(self, docs, golds, drop, sgd, losses, component_cfg)
    505             sgd = self._optimizer
    506         # Allow dict of args to GoldParse, instead of GoldParse objects.
--> 507         docs, golds = self._format_docs_and_golds(docs, golds)
    508         grads = {}
    509 

c:\python3.6\lib\site-packages\spacy\language.py in _format_docs_and_golds(self, docs, golds)
    476                 if unexpected:
    477                     err = Errors.E151.format(unexp=unexpected, exp=expected_keys)
--> 478                     raise ValueError(err)
    479                 gold = GoldParse(doc, **gold)
    480             doc_objs.append(doc)

ValueError: [E151] Trying to call nlp.update without required annotation types. Expected top-level keys: ('words', 'tags', 'heads', 'deps', 'entities', 'cats', 'links'). Got: [(0, 11, 'PRODUCT')].

could anyone help why I am getting this error?


Solution

  • I think this should fix it:

    from spacy.gold import GoldParse  #<--- add this
    
    ...
    for i in range(20):
        random.shuffle(TRAIN_DATA)
        for text, annotations in TRAIN_DATA:
            text = nlp.make_doc(text)    #<--- add this
            gold = GoldParse(text, entities=annotations)  #<--- add this
            nlp.update([text], [gold], sgd=optimizer)