Flair: nl-ner model gives AttributeError: 'CharLMEmbeddings' object has no attribute 'chars_per_chunk'

Created on 20 Jan 2020  路  12Comments  路  Source: flairNLP/flair

Describe the bug

When I try to tag a sentence with the nl-ner model, I get the following error message: AttributeError: 'CharLMEmbeddings' object has no attribute 'chars_per_chunk'

AttributeError                            Traceback (most recent call last)
<ipython-input-8-38968e7669bc> in <module>
      5 
      6 # predict NER tags
----> 7 tagger.predict(sentence)
      8 
      9 # print sentence with predicted tags

~/code/flair/flair/models/sequence_tagger_model.py in predict(self, sentences, mini_batch_size, embedding_storage_mode, all_tag_prob, verbose, use_tokenizer)
    331                     continue
    332 
--> 333                 feature: torch.Tensor = self.forward(batch)
    334                 tags, all_tags = self._obtain_labels(
    335                     feature=feature,

~/code/flair/flair/models/sequence_tagger_model.py in forward(self, sentences)
    471     def forward(self, sentences: List[Sentence]):
    472 
--> 473         self.embeddings.embed(sentences)
    474 
    475         lengths: List[int] = [len(sentence.tokens) for sentence in sentences]

~/code/flair/flair/embeddings.py in embed(self, sentences, static_embeddings)
    175 
    176         for embedding in self.embeddings:
--> 177             embedding.embed(sentences)
    178 
    179     @property

~/code/flair/flair/embeddings.py in embed(self, sentences)
     94 
     95         if not everything_embedded or not self.static_embeddings:
---> 96             self._add_embeddings_internal(sentences)
     97 
     98         return sentences

~/code/flair/flair/embeddings.py in _add_embeddings_internal(self, sentences)
   2638         # get hidden states from language model
   2639         all_hidden_states_in_lm = self.lm.get_representation(
-> 2640             text_sentences, start_marker, end_marker, self.chars_per_chunk
   2641         )
   2642 

~/anaconda3/envs/flair/lib/python3.7/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
    574                 return modules[name]
    575         raise AttributeError("'{}' object has no attribute '{}'".format(
--> 576             type(self).__name__, name))
    577 
    578     def __setattr__(self, name, value):

AttributeError: 'CharLMEmbeddings' object has no attribute 'chars_per_chunk'

To Reproduce

from flair.models import SequenceTagger
from flair.data import Sentence

# load model
tagger = SequenceTagger.load('nl-ner')

# make Dutch sentence
sentence = Sentence('George Washington ging naar Washington .')

# predict NER tags
tagger.predict(sentence)

Expected behavior

No error when tagging a sentence.

Environment (please complete the following information):

  • OS [e.g. iOS, Linux]: Mac 10.15.2
  • Version [e.g. flair-0.3.2]: 0.4.4

Additional context

I installed flair from #1360, because otherwise I couldn't load the model using torch 1.4.0

Searching the issues, I found #974, which might be related.

bug

Most helpful comment

New models were added to master branch. The new default model is a BERT-based RNN model with the highest accuracy:

from flair.data import Sentence
from flair.models import SequenceTagger

# load the default BERT-based model
tagger = SequenceTagger.load('nl-ner')

# tag sentence
sentence = Sentence('Ik hou van Amsterdam')
tagger.predict(sentence)

You can also load a Flair-based RNN model (might be faster on some setups):

# load the RNN-based model
tagger = SequenceTagger.load('nl-ner-rnn')

Will be part of Flair 0.5.1 but you can already use them if you install from master branch or just download the models to your machines.

All 12 comments

Hi @jvdzwaan , thanks for your report.
I fixed this bug, this bug is because of using the deprecated CharLMEmbeddings.
I think it will be removed in the future, So I will not push here.
You can install from my repository

Thank you, that does solve the problem!

I'm just wondering why you don't push the change. What will be removed in the future? I hope it isn't the Dutch ner model...

These old models trained by class CharLMEmbeddings will be remove in the futrue and retrain it by class FlairEmbeddings.
So I do not think the CharLMEmbeddings bug needed to be pushed.

OK, I understand. Thank you for your reply!

Hello, I'm experiencing the same bug. Even after doing

!pip install git+https://github.com/eurekaqq/flair.git in Google Colab, the following results in the same bug:

from flair.models import SequenceTagger
from flair.data import Sentence

# load model
tagger = SequenceTagger.load('nl-ner')

# make Dutch sentence
sentence = Sentence('George Washington ging naar Washington .')

# predict NER tagstagger.predict(sentence)

Am I importing the library from the Github repository in the wrong way?

Hi @NielsRogge , you can try to use
!pip install git+https://github.com/eurekaqq/flair.git@fixed_deprecated_model_bug to install fixed flair.

Ok thank you, this works.

Is there any idea if this problem is going to be solved anytime soon in the master version?

Hi @stefanievonk - I'll upload some new models for Dutch shortly.

New models were added to master branch. The new default model is a BERT-based RNN model with the highest accuracy:

from flair.data import Sentence
from flair.models import SequenceTagger

# load the default BERT-based model
tagger = SequenceTagger.load('nl-ner')

# tag sentence
sentence = Sentence('Ik hou van Amsterdam')
tagger.predict(sentence)

You can also load a Flair-based RNN model (might be faster on some setups):

# load the RNN-based model
tagger = SequenceTagger.load('nl-ner-rnn')

Will be part of Flair 0.5.1 but you can already use them if you install from master branch or just download the models to your machines.

When will Flair 0.5.1 be released to PyPI?

Hard to say exactly - I think maybe in 1-2 weeks. In the meantime, you can install the master version through pip like this:

pip install --upgrade git+https://github.com/flairNLP/flair.git
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jannenev picture jannenev  路  3Comments

ciaochiaociao picture ciaochiaociao  路  3Comments

jewl123 picture jewl123  路  3Comments

isanvicente picture isanvicente  路  3Comments

alanakbik picture alanakbik  路  3Comments