We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
TextRegressor, AttributeError: 'list' object has no attribute 'labels'
from flair.datasets import WASSA_JOY
corpus = WASSA_JOY()

embeddings = DocumentPoolEmbeddings([WordEmbeddings('glove')], fine_tune_mode='linear')

model = TextRegressor(embeddings, label_name='happiness')

output_folder = 'resources/taggers/regression_test/'

trainer = ModelTrainer(model, corpus)
trainer.train(
    output_folder,
    mini_batch_size=16,
    max_epochs=10,
)

model = TextRegressor.load(output_folder + 'best-model.pt')

sentence = Sentence('I am so happy')
model.predict(sentence)

print(sentence)


########################
got the following error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-b98a96006f68> in <module>()
     17     output_folder,
     18     mini_batch_size=16,
---> 19     max_epochs=10,
     20 )
     21 

4 frames
/usr/local/lib/python3.7/dist-packages/flair/models/text_regression_model.py in <listcomp>(.0)
     36                 [float(label.value) for label in sentence.labels], dtype=torch.float
     37             )
---> 38             for sentence in sentences
     39         ]
     40 

AttributeError: 'list' object has no attribute 'labels'

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `flair/models/text_regression_model.py`
Content:
```
1 from pathlib import Path
2 
3 from torch.utils.data.dataset import Dataset
4 
5 import flair
6 import flair.embeddings
7 import torch
8 import torch.nn as nn
9 from typing import List, Union, Optional
10 
11 from flair.datasets import DataLoader, SentenceDataset
12 from flair.training_utils import MetricRegression, Result, store_embeddings
13 from flair.data import Sentence, Label, DataPoint
14 import logging
15 
16 log = logging.getLogger("flair")
17 
18 
19 class TextRegressor(flair.models.TextClassifier):
20     def __init__(self, document_embeddings: flair.embeddings.DocumentEmbeddings, label_name: str = 'label'):
21 
22         super(TextRegressor, self).__init__(
23             document_embeddings=document_embeddings,
24             label_dictionary=flair.data.Dictionary(),
25             multi_label=False,
26             label_type=label_name,
27         )
28 
29         log.info("Using REGRESSION - experimental")
30 
31         self.loss_function = nn.MSELoss()
32 
33     def _labels_to_indices(self, sentences: List[Sentence]):
34         indices = [
35             torch.tensor(
36                 [float(label.value) for label in sentence.labels], dtype=torch.float
37             )
38             for sentence in sentences
39         ]
40 
41         vec = torch.cat(indices, 0).to(flair.device)
42 
43         return vec
44 
45     def predict(
46             self,
47             sentences: Union[Sentence, List[Sentence]],
48             label_name: Optional[str] = None,
49             mini_batch_size: int = 32,
50             embedding_storage_mode="none",
51     ) -> List[Sentence]:
52 
53         if label_name == None:
54             label_name = self.label_type if self.label_type is not None else 'label'
55 
56         with torch.no_grad():
57             if type(sentences) is Sentence:
58                 sentences = [sentences]
59 
60             filtered_sentences = self._filter_empty_sentences(sentences)
61 
62             # remove previous embeddings
63             store_embeddings(filtered_sentences, "none")
64 
65             batches = [
66                 filtered_sentences[x: x + mini_batch_size]
67                 for x in range(0, len(filtered_sentences), mini_batch_size)
68             ]
69 
70             for batch in batches:
71                 scores = self.forward(batch)
72 
73                 for (sentence, score) in zip(batch, scores.tolist()):
74                     sentence.set_label(label_name, value=str(score[0]))
75 
76                 # clearing token embeddings to save memory
77                 store_embeddings(batch, storage_mode=embedding_storage_mode)
78 
79             return sentences
80 
81     def _calculate_loss(
82             self, scores: torch.tensor, sentences: List[Sentence]
83     ) -> torch.tensor:
84         """
85         Calculates the loss.
86         :param scores: the prediction scores from the model
87         :param sentences: list of sentences
88         :return: loss value
89         """
90         return self.loss_function(scores.squeeze(1), self._labels_to_indices(sentences))
91 
92     def forward_labels_and_loss(
93             self, sentences: Union[Sentence, List[Sentence]]
94     ) -> (List[List[float]], torch.tensor):
95 
96         scores = self.forward(sentences)
97         loss = self._calculate_loss(scores, sentences)
98         return scores, loss
99 
100     def evaluate(
101             self,
102             sentences: Union[List[DataPoint], Dataset],
103             out_path: Union[str, Path] = None,
104             embedding_storage_mode: str = "none",
105             mini_batch_size: int = 32,
106             num_workers: int = 8,
107             **kwargs
108     ) -> (Result, float):
109 
110         # read Dataset into data loader (if list of sentences passed, make Dataset first)
111         if not isinstance(sentences, Dataset):
112             sentences = SentenceDataset(sentences)
113         data_loader = DataLoader(sentences, batch_size=mini_batch_size, num_workers=num_workers)
114 
115         with torch.no_grad():
116             eval_loss = 0
117 
118             metric = MetricRegression("Evaluation")
119 
120             lines: List[str] = []
121             total_count = 0
122             for batch_nr, batch in enumerate(data_loader):
123 
124                 if isinstance(batch, Sentence):
125                     batch = [batch]
126 
127                 scores, loss = self.forward_labels_and_loss(batch)
128 
129                 true_values = []
130                 for sentence in batch:
131                     total_count += 1
132                     for label in sentence.labels:
133                         true_values.append(float(label.value))
134 
135                 results = []
136                 for score in scores:
137                     if type(score[0]) is Label:
138                         results.append(float(score[0].score))
139                     else:
140                         results.append(float(score[0]))
141 
142                 eval_loss += loss
143 
144                 metric.true.extend(true_values)
145                 metric.pred.extend(results)
146 
147                 for sentence, prediction, true_value in zip(
148                         batch, results, true_values
149                 ):
150                     eval_line = "{}\t{}\t{}\n".format(
151                         sentence.to_original_text(), true_value, prediction
152                     )
153                     lines.append(eval_line)
154 
155                 store_embeddings(batch, embedding_storage_mode)
156 
157             eval_loss /= total_count
158 
159             ##TODO: not saving lines yet
160             if out_path is not None:
161                 with open(out_path, "w", encoding="utf-8") as outfile:
162                     outfile.write("".join(lines))
163 
164             log_line = f"{metric.mean_squared_error()}\t{metric.spearmanr()}\t{metric.pearsonr()}"
165             log_header = "MSE\tSPEARMAN\tPEARSON"
166 
167             detailed_result = (
168                 f"AVG: mse: {metric.mean_squared_error():.4f} - "
169                 f"mae: {metric.mean_absolute_error():.4f} - "
170                 f"pearson: {metric.pearsonr():.4f} - "
171                 f"spearman: {metric.spearmanr():.4f}"
172             )
173 
174             result: Result = Result(main_score=metric.pearsonr(),
175                                     loss=eval_loss,
176                                     log_header=log_header,
177                                     log_line=log_line,
178                                     detailed_results=detailed_result,
179             )
180 
181             return result
182 
183     def _get_state_dict(self):
184         model_state = {
185             "state_dict": self.state_dict(),
186             "document_embeddings": self.document_embeddings,
187             "label_name": self.label_type,
188         }
189         return model_state
190 
191     @staticmethod
192     def _init_model_with_state_dict(state):
193 
194         label_name = state["label_name"] if "label_name" in state.keys() else None
195 
196         model = TextRegressor(document_embeddings=state["document_embeddings"], label_name=label_name)
197 
198         model.load_state_dict(state["state_dict"])
199         return model
200
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/flair/models/text_regression_model.py b/flair/models/text_regression_model.py
--- a/flair/models/text_regression_model.py
+++ b/flair/models/text_regression_model.py
@@ -1,35 +1,63 @@
+import logging
 from pathlib import Path
+from typing import List, Union, Optional
 
+import torch
+import torch.nn as nn
 from torch.utils.data.dataset import Dataset
 
 import flair
 import flair.embeddings
-import torch
-import torch.nn as nn
-from typing import List, Union, Optional
-
+from flair.data import Sentence, Label, DataPoint
 from flair.datasets import DataLoader, SentenceDataset
 from flair.training_utils import MetricRegression, Result, store_embeddings
-from flair.data import Sentence, Label, DataPoint
-import logging
 
 log = logging.getLogger("flair")
 
 
-class TextRegressor(flair.models.TextClassifier):
-    def __init__(self, document_embeddings: flair.embeddings.DocumentEmbeddings, label_name: str = 'label'):
+class TextRegressor(flair.nn.Model):
 
-        super(TextRegressor, self).__init__(
-            document_embeddings=document_embeddings,
-            label_dictionary=flair.data.Dictionary(),
-            multi_label=False,
-            label_type=label_name,
-        )
+    def __init__(self, document_embeddings: flair.embeddings.DocumentEmbeddings, label_name: str = 'label'):
 
+        super().__init__()
         log.info("Using REGRESSION - experimental")
 
+        self.document_embeddings: flair.embeddings.DocumentEmbeddings = document_embeddings
+        self.label_name = label_name
+
+        self.decoder = nn.Linear(self.document_embeddings.embedding_length, 1)
+
+        nn.init.xavier_uniform_(self.decoder.weight)
+
         self.loss_function = nn.MSELoss()
 
+        # auto-spawn on GPU if available
+        self.to(flair.device)
+
+    def label_type(self):
+        return self.label_name
+
+    def forward(self, sentences):
+
+        self.document_embeddings.embed(sentences)
+
+        embedding_names = self.document_embeddings.get_names()
+
+        text_embedding_list = [sentence.get_embedding(embedding_names).unsqueeze(0) for sentence in sentences]
+        text_embedding_tensor = torch.cat(text_embedding_list, 0).to(flair.device)
+
+        label_scores = self.decoder(text_embedding_tensor)
+
+        return label_scores
+
+    def forward_loss(
+            self, data_points: Union[List[Sentence], Sentence]
+    ) -> torch.tensor:
+
+        scores = self.forward(data_points)
+
+        return self._calculate_loss(scores, data_points)
+
     def _labels_to_indices(self, sentences: List[Sentence]):
         indices = [
             torch.tensor(
@@ -176,7 +204,7 @@
                                     log_header=log_header,
                                     log_line=log_line,
                                     detailed_results=detailed_result,
-            )
+                                    )
 
             return result
 
@@ -197,3 +225,14 @@
 
         model.load_state_dict(state["state_dict"])
         return model
+
+    @staticmethod
+    def _filter_empty_sentences(sentences: List[Sentence]) -> List[Sentence]:
+        filtered_sentences = [sentence for sentence in sentences if sentence.tokens]
+        if len(sentences) != len(filtered_sentences):
+            log.warning(
+                "Ignore {} sentence(s) with no tokens.".format(
+                    len(sentences) - len(filtered_sentences)
+                )
+            )
+        return filtered_sentences

{"golden_diff": "diff --git a/flair/models/text_regression_model.py b/flair/models/text_regression_model.py\n--- a/flair/models/text_regression_model.py\n+++ b/flair/models/text_regression_model.py\n@@ -1,35 +1,63 @@\n+import logging\n from pathlib import Path\n+from typing import List, Union, Optional\n \n+import torch\n+import torch.nn as nn\n from torch.utils.data.dataset import Dataset\n \n import flair\n import flair.embeddings\n-import torch\n-import torch.nn as nn\n-from typing import List, Union, Optional\n-\n+from flair.data import Sentence, Label, DataPoint\n from flair.datasets import DataLoader, SentenceDataset\n from flair.training_utils import MetricRegression, Result, store_embeddings\n-from flair.data import Sentence, Label, DataPoint\n-import logging\n \n log = logging.getLogger(\"flair\")\n \n \n-class TextRegressor(flair.models.TextClassifier):\n-    def __init__(self, document_embeddings: flair.embeddings.DocumentEmbeddings, label_name: str = 'label'):\n+class TextRegressor(flair.nn.Model):\n \n-        super(TextRegressor, self).__init__(\n-            document_embeddings=document_embeddings,\n-            label_dictionary=flair.data.Dictionary(),\n-            multi_label=False,\n-            label_type=label_name,\n-        )\n+    def __init__(self, document_embeddings: flair.embeddings.DocumentEmbeddings, label_name: str = 'label'):\n \n+        super().__init__()\n         log.info(\"Using REGRESSION - experimental\")\n \n+        self.document_embeddings: flair.embeddings.DocumentEmbeddings = document_embeddings\n+        self.label_name = label_name\n+\n+        self.decoder = nn.Linear(self.document_embeddings.embedding_length, 1)\n+\n+        nn.init.xavier_uniform_(self.decoder.weight)\n+\n         self.loss_function = nn.MSELoss()\n \n+        # auto-spawn on GPU if available\n+        self.to(flair.device)\n+\n+    def label_type(self):\n+        return self.label_name\n+\n+    def forward(self, sentences):\n+\n+        self.document_embeddings.embed(sentences)\n+\n+        embedding_names = self.document_embeddings.get_names()\n+\n+        text_embedding_list = [sentence.get_embedding(embedding_names).unsqueeze(0) for sentence in sentences]\n+        text_embedding_tensor = torch.cat(text_embedding_list, 0).to(flair.device)\n+\n+        label_scores = self.decoder(text_embedding_tensor)\n+\n+        return label_scores\n+\n+    def forward_loss(\n+            self, data_points: Union[List[Sentence], Sentence]\n+    ) -> torch.tensor:\n+\n+        scores = self.forward(data_points)\n+\n+        return self._calculate_loss(scores, data_points)\n+\n     def _labels_to_indices(self, sentences: List[Sentence]):\n         indices = [\n             torch.tensor(\n@@ -176,7 +204,7 @@\n                                     log_header=log_header,\n                                     log_line=log_line,\n                                     detailed_results=detailed_result,\n-            )\n+                                    )\n \n             return result\n \n@@ -197,3 +225,14 @@\n \n         model.load_state_dict(state[\"state_dict\"])\n         return model\n+\n+    @staticmethod\n+    def _filter_empty_sentences(sentences: List[Sentence]) -> List[Sentence]:\n+        filtered_sentences = [sentence for sentence in sentences if sentence.tokens]\n+        if len(sentences) != len(filtered_sentences):\n+            log.warning(\n+                \"Ignore {} sentence(s) with no tokens.\".format(\n+                    len(sentences) - len(filtered_sentences)\n+                )\n+            )\n+        return filtered_sentences\n", "issue": "TextRegressor, AttributeError: 'list' object has no attribute 'labels'\nfrom flair.datasets import WASSA_JOY\r\ncorpus = WASSA_JOY()\r\n\r\nembeddings = DocumentPoolEmbeddings([WordEmbeddings('glove')], fine_tune_mode='linear')\r\n\r\nmodel = TextRegressor(embeddings, label_name='happiness')\r\n\r\noutput_folder = 'resources/taggers/regression_test/'\r\n\r\ntrainer = ModelTrainer(model, corpus)\r\ntrainer.train(\r\n    output_folder,\r\n    mini_batch_size=16,\r\n    max_epochs=10,\r\n)\r\n\r\nmodel = TextRegressor.load(output_folder + 'best-model.pt')\r\n\r\nsentence = Sentence('I am so happy')\r\nmodel.predict(sentence)\r\n\r\nprint(sentence)\r\n\r\n\r\n########################\r\ngot the following error\r\n\r\n---------------------------------------------------------------------------\r\nAttributeError                            Traceback (most recent call last)\r\n<ipython-input-17-b98a96006f68> in <module>()\r\n     17     output_folder,\r\n     18     mini_batch_size=16,\r\n---> 19     max_epochs=10,\r\n     20 )\r\n     21 \r\n\r\n4 frames\r\n/usr/local/lib/python3.7/dist-packages/flair/models/text_regression_model.py in <listcomp>(.0)\r\n     36                 [float(label.value) for label in sentence.labels], dtype=torch.float\r\n     37             )\r\n---> 38             for sentence in sentences\r\n     39         ]\r\n     40 \r\n\r\nAttributeError: 'list' object has no attribute 'labels'\n", "before_files": [{"content": "from pathlib import Path\n\nfrom torch.utils.data.dataset import Dataset\n\nimport flair\nimport flair.embeddings\nimport torch\nimport torch.nn as nn\nfrom typing import List, Union, Optional\n\nfrom flair.datasets import DataLoader, SentenceDataset\nfrom flair.training_utils import MetricRegression, Result, store_embeddings\nfrom flair.data import Sentence, Label, DataPoint\nimport logging\n\nlog = logging.getLogger(\"flair\")\n\n\nclass TextRegressor(flair.models.TextClassifier):\n    def __init__(self, document_embeddings: flair.embeddings.DocumentEmbeddings, label_name: str = 'label'):\n\n        super(TextRegressor, self).__init__(\n            document_embeddings=document_embeddings,\n            label_dictionary=flair.data.Dictionary(),\n            multi_label=False,\n            label_type=label_name,\n        )\n\n        log.info(\"Using REGRESSION - experimental\")\n\n        self.loss_function = nn.MSELoss()\n\n    def _labels_to_indices(self, sentences: List[Sentence]):\n        indices = [\n            torch.tensor(\n                [float(label.value) for label in sentence.labels], dtype=torch.float\n            )\n            for sentence in sentences\n        ]\n\n        vec = torch.cat(indices, 0).to(flair.device)\n\n        return vec\n\n    def predict(\n            self,\n            sentences: Union[Sentence, List[Sentence]],\n            label_name: Optional[str] = None,\n            mini_batch_size: int = 32,\n            embedding_storage_mode=\"none\",\n    ) -> List[Sentence]:\n\n        if label_name == None:\n            label_name = self.label_type if self.label_type is not None else 'label'\n\n        with torch.no_grad():\n            if type(sentences) is Sentence:\n                sentences = [sentences]\n\n            filtered_sentences = self._filter_empty_sentences(sentences)\n\n            # remove previous embeddings\n            store_embeddings(filtered_sentences, \"none\")\n\n            batches = [\n                filtered_sentences[x: x + mini_batch_size]\n                for x in range(0, len(filtered_sentences), mini_batch_size)\n            ]\n\n            for batch in batches:\n                scores = self.forward(batch)\n\n                for (sentence, score) in zip(batch, scores.tolist()):\n                    sentence.set_label(label_name, value=str(score[0]))\n\n                # clearing token embeddings to save memory\n                store_embeddings(batch, storage_mode=embedding_storage_mode)\n\n            return sentences\n\n    def _calculate_loss(\n            self, scores: torch.tensor, sentences: List[Sentence]\n    ) -> torch.tensor:\n        \"\"\"\n        Calculates the loss.\n        :param scores: the prediction scores from the model\n        :param sentences: list of sentences\n        :return: loss value\n        \"\"\"\n        return self.loss_function(scores.squeeze(1), self._labels_to_indices(sentences))\n\n    def forward_labels_and_loss(\n            self, sentences: Union[Sentence, List[Sentence]]\n    ) -> (List[List[float]], torch.tensor):\n\n        scores = self.forward(sentences)\n        loss = self._calculate_loss(scores, sentences)\n        return scores, loss\n\n    def evaluate(\n            self,\n            sentences: Union[List[DataPoint], Dataset],\n            out_path: Union[str, Path] = None,\n            embedding_storage_mode: str = \"none\",\n            mini_batch_size: int = 32,\n            num_workers: int = 8,\n            **kwargs\n    ) -> (Result, float):\n\n        # read Dataset into data loader (if list of sentences passed, make Dataset first)\n        if not isinstance(sentences, Dataset):\n            sentences = SentenceDataset(sentences)\n        data_loader = DataLoader(sentences, batch_size=mini_batch_size, num_workers=num_workers)\n\n        with torch.no_grad():\n            eval_loss = 0\n\n            metric = MetricRegression(\"Evaluation\")\n\n            lines: List[str] = []\n            total_count = 0\n            for batch_nr, batch in enumerate(data_loader):\n\n                if isinstance(batch, Sentence):\n                    batch = [batch]\n\n                scores, loss = self.forward_labels_and_loss(batch)\n\n                true_values = []\n                for sentence in batch:\n                    total_count += 1\n                    for label in sentence.labels:\n                        true_values.append(float(label.value))\n\n                results = []\n                for score in scores:\n                    if type(score[0]) is Label:\n                        results.append(float(score[0].score))\n                    else:\n                        results.append(float(score[0]))\n\n                eval_loss += loss\n\n                metric.true.extend(true_values)\n                metric.pred.extend(results)\n\n                for sentence, prediction, true_value in zip(\n                        batch, results, true_values\n                ):\n                    eval_line = \"{}\\t{}\\t{}\\n\".format(\n                        sentence.to_original_text(), true_value, prediction\n                    )\n                    lines.append(eval_line)\n\n                store_embeddings(batch, embedding_storage_mode)\n\n            eval_loss /= total_count\n\n            ##TODO: not saving lines yet\n            if out_path is not None:\n                with open(out_path, \"w\", encoding=\"utf-8\") as outfile:\n                    outfile.write(\"\".join(lines))\n\n            log_line = f\"{metric.mean_squared_error()}\\t{metric.spearmanr()}\\t{metric.pearsonr()}\"\n            log_header = \"MSE\\tSPEARMAN\\tPEARSON\"\n\n            detailed_result = (\n                f\"AVG: mse: {metric.mean_squared_error():.4f} - \"\n                f\"mae: {metric.mean_absolute_error():.4f} - \"\n                f\"pearson: {metric.pearsonr():.4f} - \"\n                f\"spearman: {metric.spearmanr():.4f}\"\n            )\n\n            result: Result = Result(main_score=metric.pearsonr(),\n                                    loss=eval_loss,\n                                    log_header=log_header,\n                                    log_line=log_line,\n                                    detailed_results=detailed_result,\n            )\n\n            return result\n\n    def _get_state_dict(self):\n        model_state = {\n            \"state_dict\": self.state_dict(),\n            \"document_embeddings\": self.document_embeddings,\n            \"label_name\": self.label_type,\n        }\n        return model_state\n\n    @staticmethod\n    def _init_model_with_state_dict(state):\n\n        label_name = state[\"label_name\"] if \"label_name\" in state.keys() else None\n\n        model = TextRegressor(document_embeddings=state[\"document_embeddings\"], label_name=label_name)\n\n        model.load_state_dict(state[\"state_dict\"])\n        return model\n", "path": "flair/models/text_regression_model.py"}], "after_files": [{"content": "import logging\nfrom pathlib import Path\nfrom typing import List, Union, Optional\n\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data.dataset import Dataset\n\nimport flair\nimport flair.embeddings\nfrom flair.data import Sentence, Label, DataPoint\nfrom flair.datasets import DataLoader, SentenceDataset\nfrom flair.training_utils import MetricRegression, Result, store_embeddings\n\nlog = logging.getLogger(\"flair\")\n\n\nclass TextRegressor(flair.nn.Model):\n\n    def __init__(self, document_embeddings: flair.embeddings.DocumentEmbeddings, label_name: str = 'label'):\n\n        super().__init__()\n        log.info(\"Using REGRESSION - experimental\")\n\n        self.document_embeddings: flair.embeddings.DocumentEmbeddings = document_embeddings\n        self.label_name = label_name\n\n        self.decoder = nn.Linear(self.document_embeddings.embedding_length, 1)\n\n        nn.init.xavier_uniform_(self.decoder.weight)\n\n        self.loss_function = nn.MSELoss()\n\n        # auto-spawn on GPU if available\n        self.to(flair.device)\n\n    def label_type(self):\n        return self.label_name\n\n    def forward(self, sentences):\n\n        self.document_embeddings.embed(sentences)\n\n        embedding_names = self.document_embeddings.get_names()\n\n        text_embedding_list = [sentence.get_embedding(embedding_names).unsqueeze(0) for sentence in sentences]\n        text_embedding_tensor = torch.cat(text_embedding_list, 0).to(flair.device)\n\n        label_scores = self.decoder(text_embedding_tensor)\n\n        return label_scores\n\n    def forward_loss(\n            self, data_points: Union[List[Sentence], Sentence]\n    ) -> torch.tensor:\n\n        scores = self.forward(data_points)\n\n        return self._calculate_loss(scores, data_points)\n\n    def _labels_to_indices(self, sentences: List[Sentence]):\n        indices = [\n            torch.tensor(\n                [float(label.value) for label in sentence.labels], dtype=torch.float\n            )\n            for sentence in sentences\n        ]\n\n        vec = torch.cat(indices, 0).to(flair.device)\n\n        return vec\n\n    def predict(\n            self,\n            sentences: Union[Sentence, List[Sentence]],\n            label_name: Optional[str] = None,\n            mini_batch_size: int = 32,\n            embedding_storage_mode=\"none\",\n    ) -> List[Sentence]:\n\n        if label_name == None:\n            label_name = self.label_type if self.label_type is not None else 'label'\n\n        with torch.no_grad():\n            if type(sentences) is Sentence:\n                sentences = [sentences]\n\n            filtered_sentences = self._filter_empty_sentences(sentences)\n\n            # remove previous embeddings\n            store_embeddings(filtered_sentences, \"none\")\n\n            batches = [\n                filtered_sentences[x: x + mini_batch_size]\n                for x in range(0, len(filtered_sentences), mini_batch_size)\n            ]\n\n            for batch in batches:\n                scores = self.forward(batch)\n\n                for (sentence, score) in zip(batch, scores.tolist()):\n                    sentence.set_label(label_name, value=str(score[0]))\n\n                # clearing token embeddings to save memory\n                store_embeddings(batch, storage_mode=embedding_storage_mode)\n\n            return sentences\n\n    def _calculate_loss(\n            self, scores: torch.tensor, sentences: List[Sentence]\n    ) -> torch.tensor:\n        \"\"\"\n        Calculates the loss.\n        :param scores: the prediction scores from the model\n        :param sentences: list of sentences\n        :return: loss value\n        \"\"\"\n        return self.loss_function(scores.squeeze(1), self._labels_to_indices(sentences))\n\n    def forward_labels_and_loss(\n            self, sentences: Union[Sentence, List[Sentence]]\n    ) -> (List[List[float]], torch.tensor):\n\n        scores = self.forward(sentences)\n        loss = self._calculate_loss(scores, sentences)\n        return scores, loss\n\n    def evaluate(\n            self,\n            sentences: Union[List[DataPoint], Dataset],\n            out_path: Union[str, Path] = None,\n            embedding_storage_mode: str = \"none\",\n            mini_batch_size: int = 32,\n            num_workers: int = 8,\n            **kwargs\n    ) -> (Result, float):\n\n        # read Dataset into data loader (if list of sentences passed, make Dataset first)\n        if not isinstance(sentences, Dataset):\n            sentences = SentenceDataset(sentences)\n        data_loader = DataLoader(sentences, batch_size=mini_batch_size, num_workers=num_workers)\n\n        with torch.no_grad():\n            eval_loss = 0\n\n            metric = MetricRegression(\"Evaluation\")\n\n            lines: List[str] = []\n            total_count = 0\n            for batch_nr, batch in enumerate(data_loader):\n\n                if isinstance(batch, Sentence):\n                    batch = [batch]\n\n                scores, loss = self.forward_labels_and_loss(batch)\n\n                true_values = []\n                for sentence in batch:\n                    total_count += 1\n                    for label in sentence.labels:\n                        true_values.append(float(label.value))\n\n                results = []\n                for score in scores:\n                    if type(score[0]) is Label:\n                        results.append(float(score[0].score))\n                    else:\n                        results.append(float(score[0]))\n\n                eval_loss += loss\n\n                metric.true.extend(true_values)\n                metric.pred.extend(results)\n\n                for sentence, prediction, true_value in zip(\n                        batch, results, true_values\n                ):\n                    eval_line = \"{}\\t{}\\t{}\\n\".format(\n                        sentence.to_original_text(), true_value, prediction\n                    )\n                    lines.append(eval_line)\n\n                store_embeddings(batch, embedding_storage_mode)\n\n            eval_loss /= total_count\n\n            ##TODO: not saving lines yet\n            if out_path is not None:\n                with open(out_path, \"w\", encoding=\"utf-8\") as outfile:\n                    outfile.write(\"\".join(lines))\n\n            log_line = f\"{metric.mean_squared_error()}\\t{metric.spearmanr()}\\t{metric.pearsonr()}\"\n            log_header = \"MSE\\tSPEARMAN\\tPEARSON\"\n\n            detailed_result = (\n                f\"AVG: mse: {metric.mean_squared_error():.4f} - \"\n                f\"mae: {metric.mean_absolute_error():.4f} - \"\n                f\"pearson: {metric.pearsonr():.4f} - \"\n                f\"spearman: {metric.spearmanr():.4f}\"\n            )\n\n            result: Result = Result(main_score=metric.pearsonr(),\n                                    loss=eval_loss,\n                                    log_header=log_header,\n                                    log_line=log_line,\n                                    detailed_results=detailed_result,\n                                    )\n\n            return result\n\n    def _get_state_dict(self):\n        model_state = {\n            \"state_dict\": self.state_dict(),\n            \"document_embeddings\": self.document_embeddings,\n            \"label_name\": self.label_type,\n        }\n        return model_state\n\n    @staticmethod\n    def _init_model_with_state_dict(state):\n\n        label_name = state[\"label_name\"] if \"label_name\" in state.keys() else None\n\n        model = TextRegressor(document_embeddings=state[\"document_embeddings\"], label_name=label_name)\n\n        model.load_state_dict(state[\"state_dict\"])\n        return model\n\n    @staticmethod\n    def _filter_empty_sentences(sentences: List[Sentence]) -> List[Sentence]:\n        filtered_sentences = [sentence for sentence in sentences if sentence.tokens]\n        if len(sentences) != len(filtered_sentences):\n            log.warning(\n                \"Ignore {} sentence(s) with no tokens.\".format(\n                    len(sentences) - len(filtered_sentences)\n                )\n            )\n        return filtered_sentences\n", "path": "flair/models/text_regression_model.py"}]}