spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

KeyError when training the tagger

Open ashwinbiyani opened this issue 8 years ago • 3 comments

Your Environment

  • Operating System:
  • Python Version Used:
  • spaCy Version Used:
  • Environment Information:

Requirement:

Trying to train an existing model with general purpose data available as part of model and specific domain data. need both the data set in the new model created.

Code:

!/usr/bin/env python coding: utf8

from future import unicode_literals, print_function

import plac import random from pathlib import Path import spacy

TAG_MAP = { 'N': {'pos': 'NOUN'}, 'V': {'pos': 'VERB'}, 'J': {'pos': 'ADJ'} }

TRAIN_DATA = [ ("I like green eggs", {'tags': ['N', 'V', 'J', 'N']}), ("Eat blue ham", {'tags': ['V', 'J', 'N']}) ]

@plac.annotations( lang=("ISO Code of language to use", "option", "l", str), output_dir=("Optional output directory", "option", "o", Path), n_iter=("Number of training iterations", "option", "n", int)) def main(lang='en', output_dir=None, n_iter=25):

nlp = spacy.load('en_core_web_sm')

nlp.disable_pipes()
optimizer = nlp.begin_training()
for i in range(n_iter):
    random.shuffle(TRAIN_DATA)
    losses = {}
    for text, annotations in TRAIN_DATA:
        nlp.update([text], [annotations], sgd=optimizer, losses=losses)
    print(losses)


test_text = "I like blue eggs"
doc = nlp(test_text)
print('Tags', [(t.text, t.tag_, t.pos_) for t in doc])


if output_dir is not None:
    output_dir = Path(output_dir)
    if not output_dir.exists():
        output_dir.mkdir()
    nlp.to_disk(output_dir)
    print("Saved model to", output_dir)

 
    print("Loading from", output_dir)
    nlp2 = spacy.load(output_dir)
    doc = nlp2(test_text)
    print('Tags', [(t.text, t.tag_, t.pos_) for t in doc])

if name == 'main': plac.call(main)

Error:

base) E:\projects\spacy>python train2.py -l en -o E:\projects\spacy
Warning: Unnamed vectors -- this won't allow multiple vectors models to be loaded. (Shape: (0, 0)) Traceback (most recent call last): File "train2.py", line 88, in plac.call(main) File "E:\Softwares\anaconda3\lib\site-packages\plac_core.py", line 328, in call cmd, result = parser.consume(arglist) File "E:\Softwares\anaconda3\lib\site-packages\plac_core.py", line 207, in consume return cmd, self.func(*(args + varargs + extraopts), **kwargs) File "train2.py", line 64, in main nlp.update([text], [annotations], sgd=optimizer, losses=losses) File "E:\Softwares\anaconda3\lib\site-packages\spacy\language.py", line 427, in update proc.update(docs, golds, drop=drop, sgd=get_grads, losses=losses) File "pipeline.pyx", line 480, in spacy.pipeline.Tagger.update File "pipeline.pyx", line 497, in spacy.pipeline.Tagger.get_loss KeyError: 'N'

ashwinbiyani avatar Aug 27 '18 14:08 ashwinbiyani

It looks like your code doesn't actually call tagger.add_label to add the new labels like 'N' to the tagger. See here for the full example: https://github.com/explosion/spaCy/blob/master/examples/training/train_tagger.py

ines avatar Aug 29 '18 09:08 ines

ok. i will give it a try with code from github. Looks like code given in the documentation is not working fine. It works perfectly fine for both old/new and just new while training NER model.

On Wed, Aug 29, 2018 at 3:06 PM, Ines Montani [email protected] wrote:

It looks like your code doesn't actually call tagger.add_label to add the new labels like 'N' to the tagger. See here for the full example: https://github.com/explosion/spaCy/blob/master/examples/ training/train_tagger.py

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/explosion/spaCy/issues/2711#issuecomment-416890306, or mute the thread https://github.com/notifications/unsubscribe-auth/AfByLUAuJMe0MHG00rEUVBpIXE7F_oZWks5uVmCigaJpZM4WN7Iz .

ashwinbiyani avatar Aug 29 '18 09:08 ashwinbiyani

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Sep 28 '18 10:09 lock[bot]