question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

loading existing model throws exception KeyError: 1

See original GitHub issue

I’ve trained a model using BILUO tags after saving when I try to load the mode it throws an error. Here is the code for saving the model

def save_model(ner, model):
    model_dir = pathlib.Path(model)
    if not model_dir.exists():
        model_dir.mkdir()
    assert model_dir.is_dir()

    with (model_dir / 'config.json').open('w') as file_:
        json.dump(ner.cfg, file_)
    ner.model.dump(str(model_dir / 'model'))
    if not (model_dir / 'vocab').exists():
        (model_dir / 'vocab').mkdir()
    ner.vocab.dump(str(model_dir / 'vocab' / 'lexemes.bin'))
    with (model_dir / 'vocab' / 'strings.json').open('w', encoding='utf8') as file_:
        ner.vocab.strings.dump(file_)

Here is the code for loading the model

def load_model(model):
    model_dir = pathlib.Path(model)
    if not model_dir.exists():
        return False

    nlp = spacy.load('en', parser=False, entity=False, add_vectors=False)
    vocab_dir = pathlib.Path(model_dir / 'vocab')

    with (vocab_dir / 'strings.json').open('r', encoding='utf8') as file_:
        nlp.vocab.strings.load(file_)

    nlp.vocab.load_lexemes(vocab_dir / 'lexemes.bin')

    ner = EntityRecognizer.load(model_dir, nlp.vocab, require=True)
    return (nlp, ner)

This line throws error ner = EntityRecognizer.load(model_dir, nlp.vocab, require=True) Here is the console output.

line 12, in load_model
    ner = EntityRecognizer.load(model_dir, nlp.vocab, require=True)
  File "spacy/syntax/parser.pyx", line 154, in spacy.syntax.parser.Parser.load (spacy/syntax/parser.cpp:6999)
  File "spacy/syntax/parser.pyx", line 177, in spacy.syntax.parser.Parser.__init__ (spacy/syntax/parser.cpp:7370)
  File "spacy/syntax/ner.pyx", line 71, in spacy.syntax.ner.BiluoPushDown.get_actions (spacy/syntax/ner.cpp:3906)
KeyError: 1

Your Environment

Info about spaCy

  • spaCy version: 1.8.2
  • Installed models: en, en_default
  • Platform: Linux-4.4.0-75-generic-x86_64-with-Ubuntu-16.04-xenial
  • Python version: 3.5.1+

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
lukewendlingcommented, Jul 6, 2017

@ines is there a workaround until 2.0 is released?

1reaction
thomasgiraultcommented, Apr 28, 2017

I am trying to train my own named entity recognizer but I fails exactly in the same way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python KeyError Exceptions and How to Handle Them
In this tutorial, you'll learn how to handle Python KeyError exceptions. They are often caused by a bad key lookup in a dictionary,...
Read more >
How to fix Python KeyError Exceptions in simple steps?
First, we access an existing key in the try-except block. If the Keyerror is not raised, there are no errors. Then the else...
Read more >
I'm getting Key error in python - Stack Overflow
A KeyError generally means the key doesn't exist. So, are you sure the path key exists? From the official python docs: exception KeyError....
Read more >
Python KeyError Exception Handling Examples - DigitalOcean
Python KeyError is raised when we try to access a key from dict, which doesn't exist. It's one of the built-in exception classes...
Read more >
How to Fix KeyError Exceptions in Python - Rollbar
The Python KeyError is raised when a mapping key is not found in the set of existing keys of the mapping. In Python,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found