Repository: flairNLP/flair

Issue:
Add Twitter Embeddings
We used to have pre-trained Twitter embeddings in earlier releases. We somehow removed them, but they are important for good scores on the WNUT task. So put them back in.


Discussion:

Resolving patch:
diff --git a/flair/embeddings.py b/flair/embeddings.py
--- a/flair/embeddings.py
+++ b/flair/embeddings.py
@@ -173,6 +173,11 @@ def __init__(self, embeddings: str):
             cached_path(f'{base_path}en-fasttext-news-300d-1M.vectors.npy', cache_dir=cache_dir)
             embeddings = cached_path(f'{base_path}en-fasttext-news-300d-1M', cache_dir=cache_dir)
 
+        # twitter embeddings
+        elif embeddings.lower() == 'twitter' or embeddings.lower() == 'en-twitter':
+            cached_path(f'{old_base_path}twitter.gensim.vectors.npy', cache_dir=cache_dir)
+            embeddings = cached_path(f'{old_base_path}twitter.gensim', cache_dir=cache_dir)
+
         # two-letter language code wiki embeddings
         elif len(embeddings.lower()) == 2:
             cached_path(f'{embeddings_path_v4}{embeddings}-wiki-fasttext-300d-1M.vectors.npy', cache_dir=cache_dir)
diff --git a/flair/hyperparameter/param_selection.py b/flair/hyperparameter/param_selection.py
--- a/flair/hyperparameter/param_selection.py
+++ b/flair/hyperparameter/param_selection.py
@@ -108,6 +108,7 @@ def _objective(self, params: dict):
         final_score = (sum(scores) / float(len(scores)))
         final_var = (sum(vars) / float(len(vars)))
 
+        test_score = result['test_score']
         log_line(log)
         log.info(f'Done evaluating parameter combination:')
         for k, v in params.items():
@@ -116,6 +117,7 @@ def _objective(self, params: dict):
             log.info(f'\t{k}: {v}')
         log.info(f'{self.optimization_value.value}: {final_score}')
         log.info(f'variance: {final_var}')
+        log.info(f'test_score: {test_score}\n')
         log_line(log)
 
         with open(self.param_selection_file, 'a') as f:
@@ -126,6 +128,7 @@ def _objective(self, params: dict):
                 f.write(f'\t{k}: {str(v)}\n')
             f.write(f'{self.optimization_value.value}: {final_score}\n')
             f.write(f'variance: {final_var}\n')
+            f.write(f'test_score: {test_score}\n')
             f.write('-' * 100 + '\n')
 
         self.run += 1
diff --git a/flair/trainers/trainer.py b/flair/trainers/trainer.py
--- a/flair/trainers/trainer.py
+++ b/flair/trainers/trainer.py
@@ -1,4 +1,3 @@
-from enum import Enum
 from pathlib import Path
 from typing import List, Union
 
@@ -114,7 +113,10 @@ def train(self,
             for epoch in range(0 + self.epoch, max_epochs + self.epoch):
                 log_line(log)
 
-                bad_epochs = scheduler.num_bad_epochs
+                try:
+                    bad_epochs = scheduler.num_bad_epochs
+                except:
+                    bad_epochs = 0
                 for group in optimizer.param_groups:
                     learning_rate = group['lr']
 
@@ -242,9 +244,8 @@ def train(self,
                 self.model.save(base_path / 'final-model.pt')
                 log.info('Done.')
 
-        final_score = 0.0
-        if not param_selection_mode:
-            final_score = self.final_test(base_path, embeddings_in_memory, evaluation_metric, eval_mini_batch_size)
+        # test best model on test data
+        final_score = self.final_test(base_path, embeddings_in_memory, evaluation_metric, eval_mini_batch_size)
 
         return {'test_score': final_score,
                 'dev_score_history': dev_score_history,


Test patch: