Repository: flairNLP/flair

Issue:
Serializing the multi_label_threshold along with the classification models
Currently for classification models, more specifically for [TextClassifier](https://github.com/flairNLP/flair/blob/master/flair/models/text_classification_model.py#L83) and [TextPairClassifier](https://github.com/flairNLP/flair/blob/master/flair/models/pairwise_classification_model.py#L128), the `multi_label_threshold` is not part of the `state_dict`, which means that if the threshold is changed and the model is saved, the changed threshold is not serialized, and therefore will not be preserved in the loaded model.  

This could be done by making `multi_label_threshold` part of `state_dict` for these two classes, so the threshold is is serialized with the model.


Discussion:

Resolving patch:
diff --git a/flair/models/pairwise_classification_model.py b/flair/models/pairwise_classification_model.py
--- a/flair/models/pairwise_classification_model.py
+++ b/flair/models/pairwise_classification_model.py
@@ -132,6 +132,7 @@ def _get_state_dict(self):
             "label_dictionary": self.label_dictionary,
             "label_type": self.label_type,
             "multi_label": self.multi_label,
+            "multi_label_threshold": self.multi_label_threshold,
             "loss_weights": self.loss_weights,
             "embed_separately": self.embed_separately,
         }
@@ -145,6 +146,7 @@ def _init_model_with_state_dict(state):
             label_dictionary=state["label_dictionary"],
             label_type=state["label_type"],
             multi_label=state["multi_label"],
+            multi_label_threshold=0.5 if "multi_label_threshold" not in state.keys() else state["multi_label_threshold"],
             loss_weights=state["loss_weights"],
             embed_separately=state["embed_separately"],
         )
diff --git a/flair/models/text_classification_model.py b/flair/models/text_classification_model.py
--- a/flair/models/text_classification_model.py
+++ b/flair/models/text_classification_model.py
@@ -87,6 +87,7 @@ def _get_state_dict(self):
             "label_dictionary": self.label_dictionary,
             "label_type": self.label_type,
             "multi_label": self.multi_label,
+            "multi_label_threshold": self.multi_label_threshold,
             "weight_dict": self.weight_dict,
         }
         return model_state
@@ -101,6 +102,7 @@ def _init_model_with_state_dict(state):
             label_dictionary=state["label_dictionary"],
             label_type=label_type,
             multi_label=state["multi_label"],
+            multi_label_threshold=0.5 if "multi_label_threshold" not in state.keys() else state["multi_label_threshold"],
             loss_weights=weights,
         )
         model.load_state_dict(state["state_dict"])
@@ -140,4 +142,4 @@ def _fetch_model(model_name) -> str:
 
     @property
     def label_type(self):
-        return self._label_type
\ No newline at end of file
+        return self._label_type
diff --git a/flair/nn/model.py b/flair/nn/model.py
--- a/flair/nn/model.py
+++ b/flair/nn/model.py
@@ -502,6 +502,7 @@ def predict(
                                 if label_score > label_threshold or multi_class_prob:
                                     label.set_value(value=label_value, score=label_score)
                                     sentence.add_complex_label(label_name, copy.deepcopy(label))
+
                     else:
                         softmax = torch.nn.functional.softmax(scores, dim=-1)
                         conf, idx = torch.max(softmax, dim=-1)
@@ -553,7 +554,6 @@ def _get_multi_label(self, label_scores) -> List[Label]:
             label_score = conf.item()
             if label_score > label_threshold:
                 labels.append(Label(label_value, label_score))
-
         return labels
 
     def _get_single_label(self, label_scores) -> List[Label]:


Test patch: