There is error when loading a text classifier,
Code:
from flair.models import TextClassifier
classifier = TextClassifier.load('en-sentiment')
Error:
AttributeError Traceback (most recent call last)
<ipython-input-4-f517a501f1cf> in <module>
----> 1 classifier = TextClassifier.load('en-sentiment')
~/Desktop/flair/flair/nn.py in load(cls, model)
86 state = torch.load(f, map_location=flair.device)
87
---> 88 model = cls._init_model_with_state_dict(state)
89
90 model.eval()
~/Desktop/flair/flair/models/text_classification_model.py in _init_model_with_state_dict(state)
129 multi_label=state["multi_label"],
130 beta=beta,
--> 131 loss_weights=weights,
132 )
133
~/Desktop/flair/flair/models/text_classification_model.py in __init__(self, document_embeddings, label_dictionary, multi_label, multi_label_threshold, beta, loss_weights)
90
91 # auto-spawn on GPU if available
---> 92 self.to(flair.device)
93
94 def _init_weights(self):
~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in to(self, *args, **kwargs)
423 return t.to(device, dtype if t.is_floating_point() else None, non_blocking)
424
--> 425 return self._apply(convert)
426
427 def register_backward_hook(self, hook):
~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in _apply(self, fn)
199 def _apply(self, fn):
200 for module in self.children():
--> 201 module._apply(fn)
202
203 def compute_should_use_set_data(tensor, tensor_applied):
~/.local/lib/python3.6/site-packages/torch/nn/modules/module.py in _apply(self, fn)
199 def _apply(self, fn):
200 for module in self.children():
--> 201 module._apply(fn)
202
203 def compute_should_use_set_data(tensor, tensor_applied):
~/.local/lib/python3.6/site-packages/torch/nn/modules/rnn.py in _apply(self, fn)
135 # Note: be v. careful before removing this, as 3rd party device types
136 # likely rely on this behavior to properly .to() modules like LSTM.
--> 137 self._flat_weights = [getattr(self, weight) for weight in self._flat_weights_names]
138
139 # Flattens params (on CUDA)
~/.local/lib/python3.6/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: 'GRU' object has no attribute '_flat_weights_names'
Environment
It is probably fixed in master: https://github.com/flairNLP/flair/pull/1360
@carlodavid012 thanks for reporting this - the error persists even in master. I will push a fix in a moment. We will also do a release probably today so it will then also be fixed if you install the new version through pip!
@alanakbik Could you please confirm if it is fixed? We still face this issue in the 0.4.5 release.
@chandu188 in our tests, yes. To reproduce:
open a new Python3 notebook on Google Colab
update libraries
pip install --upgrade flair torch
from flair.models import TextClassifier
from flair.data import Sentence
classifier = TextClassifier.load('en-sentiment')
classifier.predict(Sentence('This movie is great'))
Most helpful comment
@carlodavid012 thanks for reporting this - the error persists even in master. I will push a fix in a moment. We will also do a release probably today so it will then also be fixed if you install the new version through pip!