提问者:小点点

Spacy训练模型


我想创建我自己的SPACY训练模式。 在下面的代码中,我遇到了错误。

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")

当我在这个网站上使用简单的训练循环时,我遇到了以下错误:https://spacy.io/usage/training,它工作了

输出:

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')].

有人能帮我解释一下为什么我会出现这个错误吗?


共1个答案

匿名用户

我想这应该能解决问题:

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)