Repository: flairNLP/flair

Issue:
'TextClassifier' object has no attribute 'embeddings'
TARSClassifier.load error 

AttributeError                            Traceback (most recent call last)
<ipython-input-13-710c2b4d40e4> in <module>
----> 1 tars = TARSClassifier.load('/content/drive/MyDrive/Text_classification/final-model.pt')

2 frames
/usr/local/lib/python3.7/dist-packages/flair/nn/model.py in load(cls, model_path)
    147             state = torch.load(f, map_location="cpu")
    148 
--> 149         model = cls._init_model_with_state_dict(state)
    150 
    151         if "model_card" in state:

/usr/local/lib/python3.7/dist-packages/flair/models/tars_model.py in _init_model_with_state_dict(cls, state, **kwargs)
    739             label_dictionary=state.get("label_dictionary"),
    740             label_type=state.get("label_type", "default_label"),
--> 741             embeddings=state.get("tars_model").embeddings,
    742             num_negative_labels_to_sample=state.get("num_negative_labels_to_sample"),
    743             **kwargs,

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in __getattr__(self, name)
   1206                 return modules[name]
   1207         raise AttributeError("'{}' object has no attribute '{}'".format(
-> 1208             type(self).__name__, name))
   1209 
   1210     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:

AttributeError: 'TextClassifier' object has no attribute 'embeddings'


Discussion:
Which Flair version do you use?
Flair version it's 0.11.3
On master branch or installed through pip? 
pip installed 

#
!pip install --upgrade git+https://github.com/zalandoresearch/flair.git
!pip install flair

Resolving patch:
diff --git a/flair/models/tars_model.py b/flair/models/tars_model.py
--- a/flair/models/tars_model.py
+++ b/flair/models/tars_model.py
@@ -732,13 +732,30 @@ def _get_state_dict(self):
 
     @classmethod
     def _init_model_with_state_dict(cls, state, **kwargs):
+
+        # get the serialized embeddings
+        tars_model = state.get("tars_model")
+        if hasattr(tars_model, "embeddings"):
+            embeddings = tars_model.embeddings
+        else:
+            embeddings = tars_model.document_embeddings
+
+        # remap state dict for models serialized with Flair <= 0.11.3
+        import re
+
+        state_dict = state["state_dict"]
+        for key in list(state_dict.keys()):
+            state_dict[re.sub("^tars_model.document_embeddings\\.", "tars_model.embeddings.", key)] = state_dict.pop(
+                key
+            )
+
         # init new TARS classifier
         model: TARSClassifier = super()._init_model_with_state_dict(
             state,
             task_name=state["current_task"],
             label_dictionary=state.get("label_dictionary"),
             label_type=state.get("label_type", "default_label"),
-            embeddings=state.get("tars_model").embeddings,
+            embeddings=embeddings,
             num_negative_labels_to_sample=state.get("num_negative_labels_to_sample"),
             **kwargs,
         )
@@ -859,12 +876,13 @@ def predict(
                         loss_and_count = self.tars_model.predict(
                             tars_sentence,
                             label_name=label_name,
-                            return_loss=True,
+                            return_loss=return_loss,
                             return_probabilities_for_all_classes=True if label_threshold < 0.5 else False,
                         )
 
-                        overall_loss += loss_and_count[0].item()
-                        overall_count += loss_and_count[1]
+                        if return_loss:
+                            overall_loss += loss_and_count[0].item()
+                            overall_count += loss_and_count[1]
 
                         # add all labels that according to TARS match the text and are above threshold
                         for predicted_tars_label in tars_sentence.get_labels(label_name):


Test patch: