Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loading existing model throws exception KeyError: 1 #1026

Closed
irfan-zoefit opened this issue Apr 27, 2017 · 7 comments
Closed

loading existing model throws exception KeyError: 1 #1026

irfan-zoefit opened this issue Apr 27, 2017 · 7 comments
Labels
bug Bugs and behaviour differing from documentation

Comments

@irfan-zoefit
Copy link

irfan-zoefit commented Apr 27, 2017

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+
@thomasgirault
Copy link

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

@fafg
Copy link

fafg commented Apr 29, 2017

Hi Guys, I am trying to use spacy with rasa_nlu and I am getting this error:

python -m rasa_nlu.server -c config.json --server_model_dirs=./model_20170429-200133
INFO:root:Configuration: {
"num_threads": 1,
"log_level": 20,
"language": "en",
"mitie_file": "data/total_word_feature_extractor.dat",
"emulate": null,
"config": "config.json",
"response_log": "/home/ff/logs",
"server_model_dirs": "./model_20170429-200133",
"fine_tune_spacy_ner": false,
"token": null,
"path": "./models",
"log_file": null,
"data": "./data/examples/demo-rasa.json",
"port": 5000,
"backend": "spacy_sklearn"
}
INFO:root:Logging requests to '/home/ff/logs/rasa_nlu_log-20170429-200311-4566.log'.
INFO:root:using spacy + sklearn backend
Traceback (most recent call last):
File "/root/anaconda2/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"main", fname, loader, pkg_name)
File "/root/anaconda2/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/root/anaconda2/lib/python2.7/site-packages/rasa_nlu/server.py", line 99, in
app = WSGIServer(('0.0.0.0', rasa_nlu_config['port']), create_app(rasa_nlu_config))
File "/root/anaconda2/lib/python2.7/site-packages/rasa_nlu/server.py", line 90, in create_app
rasa_nlu_app.data_router = DataRouter(config)
File "/root/anaconda2/lib/python2.7/site-packages/rasa_nlu/data_router.py", line 29, in init
self.model_store = self.__create_model_store()
File "/root/anaconda2/lib/python2.7/site-packages/rasa_nlu/data_router.py", line 112, in __create_model_store
interpreter = DataRouter.create_interpreter(metadata, nlp, featurizer)
File "/root/anaconda2/lib/python2.7/site-packages/rasa_nlu/data_router.py", line 170, in create_interpreter
return SpacySklearnInterpreter.load(metadata, nlp, featurizer)
File "/root/anaconda2/lib/python2.7/site-packages/rasa_nlu/interpreters/spacy_sklearn_interpreter.py", line 25, in load
meta.metadata.get("should_fine_tune_spacy_ner"))
File "/root/anaconda2/lib/python2.7/site-packages/rasa_nlu/extractors/spacy_entity_extractor.py", line 15, in init
self.ner = EntityRecognizer.load(pathlib.Path(extractor_file), nlp.vocab)
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

just follow the steps to install and raise up a demo (using a docker container) the things just blow up at this point.

my best regards.

@fafg
Copy link

fafg commented Apr 29, 2017

by the way I am using this version of spacy:
spacy 1.8.1 np111py27_0 conda-forge

@fafg
Copy link

fafg commented Apr 29, 2017

i've been created a new docker container with more dependencies
apt-get install build-essential python-dev
but same error happens

@ines ines added the bug Bugs and behaviour differing from documentation label May 7, 2017
@ines
Copy link
Member

ines commented May 7, 2017

Closing this and making #1046 the master issue. Work in progress for spaCy v2.0!

@ines ines closed this as completed May 7, 2017
@lukewendling
Copy link

@ines is there a workaround until 2.0 is released?

@lock
Copy link

lock bot commented May 8, 2018

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 lock bot locked as resolved and limited conversation to collaborators May 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bugs and behaviour differing from documentation
Projects
None yet
Development

No branches or pull requests

5 participants