Skip to content

Commit 083eb11

Browse files
authored
Merge pull request flairNLP#1627 from flairNLP/flairNLPGH-1626-documentation
flairNLPGH-1626: documentation
2 parents 72f4ad6 + 9541652 commit 083eb11

24 files changed

+847
-1484
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Sentence: "I love Berlin ." - 4 Tokens
106106

107107
The following NER tags are found:
108108

109-
LOC-span [3]: "Berlin"
109+
Span [3]: "Berlin" [− Labels: LOC (0.9992)]
110110
```
111111

112112
## Tutorials
@@ -118,10 +118,9 @@ We provide a set of quick tutorials to get you started with the library:
118118
* [Tutorial 3: Embedding Words](/resources/docs/TUTORIAL_3_WORD_EMBEDDING.md)
119119
* [Tutorial 4: List of All Word Embeddings](/resources/docs/TUTORIAL_4_ELMO_BERT_FLAIR_EMBEDDING.md)
120120
* [Tutorial 5: Embedding Documents](/resources/docs/TUTORIAL_5_DOCUMENT_EMBEDDINGS.md)
121-
* [Tutorial 6: Loading your own Corpus](/resources/docs/TUTORIAL_6_CORPUS.md)
122-
* [Tutorial 7: Training your own Models](/resources/docs/TUTORIAL_7_TRAINING_A_MODEL.md)
123-
* [Tutorial 8: Optimizing your Models](/resources/docs/TUTORIAL_8_MODEL_OPTIMIZATION.md)
124-
* [Tutorial 9: Training your own Flair Embeddings](/resources/docs/TUTORIAL_9_TRAINING_LM_EMBEDDINGS.md)
121+
* [Tutorial 6: Loading a Dataset](/resources/docs/TUTORIAL_6_CORPUS.md)
122+
* [Tutorial 7: Training a Model](/resources/docs/TUTORIAL_7_TRAINING_A_MODEL.md)
123+
* [Tutorial 8: Training your own Flair Embeddings](/resources/docs/TUTORIAL_9_TRAINING_LM_EMBEDDINGS.md)
125124

126125
The tutorials explain how the base NLP classes work, how you can load pre-trained models to tag your
127126
text, how you can embed your text with different word or document embeddings, and how you can train your own

flair/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import logging.config
2626

27-
__version__ = "0.4.5"
27+
__version__ = "0.5"
2828

2929
logging.config.dictConfig(
3030
{

flair/data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ def to_dict(self):
175175
return {"value": self.value, "confidence": self.score}
176176

177177
def __str__(self):
178-
return f"{self._value} ({self._score:.4f})"
178+
return f"{self._value} ({round(self._score, 4)})"
179179

180180
def __repr__(self):
181-
return f"{self._value} ({self._score:.4f})"
181+
return f"{self._value} ({round(self._score, 4)})"
182182

183183

184184
class DataPoint:

flair/datasets/sequence_labeling.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ def __init__(
672672
data_folder, columns, tag_to_bioes=tag_to_bioes, in_memory=in_memory
673673
)
674674

675+
675676
class NER_FINNISH(ColumnCorpus):
676677
def __init__(
677678
self,

flair/embeddings/document.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def __setstate__(self, d):
170170
model_name = self.name.split('transformer-document-')[-1]
171171
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
172172

173+
173174
class DocumentPoolEmbeddings(DocumentEmbeddings):
174175
def __init__(
175176
self,

flair/embeddings/token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def get_cached_vec(self, word: str) -> torch.Tensor:
246246
word_embedding = np.zeros(self.embedding_length, dtype="float")
247247

248248
word_embedding = torch.tensor(
249-
word_embedding, device=flair.device, dtype=torch.float
249+
word_embedding.tolist(), device=flair.device, dtype=torch.float
250250
)
251251
return word_embedding
252252

@@ -1113,7 +1113,7 @@ def get_cached_vec(self, word: str) -> torch.Tensor:
11131113
word_embedding = np.zeros(self.embedding_length, dtype="float")
11141114

11151115
word_embedding = torch.tensor(
1116-
word_embedding, device=flair.device, dtype=torch.float
1116+
word_embedding.tolist(), device=flair.device, dtype=torch.float
11171117
)
11181118
return word_embedding
11191119

flair/models/sequence_tagger_model.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(
7575
dropout: float = 0.0,
7676
word_dropout: float = 0.05,
7777
locked_dropout: float = 0.5,
78+
reproject_to: int = None,
7879
train_initial_hidden_state: bool = False,
7980
rnn_type: str = "LSTM",
8081
pickle_module: str = "pickle",
@@ -92,6 +93,7 @@ def __init__(
9293
:param rnn_layers: number of RNN layers
9394
:param dropout: dropout probability
9495
:param word_dropout: word dropout probability
96+
:param reproject_to: set this to control the dimensionality of the reprojection layer
9597
:param locked_dropout: locked dropout probability
9698
:param train_initial_hidden_state: if True, trains initial hidden state of RNN
9799
:param beta: Parameter for F-beta score for evaluation and training annealing
@@ -154,12 +156,16 @@ def __init__(
154156
if locked_dropout > 0.0:
155157
self.locked_dropout = flair.nn.LockedDropout(locked_dropout)
156158

157-
rnn_input_dim: int = self.embeddings.embedding_length
159+
embedding_dim: int = self.embeddings.embedding_length
158160

159-
self.relearn_embeddings: bool = True
161+
# if no dimensionality for reprojection layer is set, reproject to equal dimension
162+
self.reproject_to = reproject_to
163+
if self.reproject_to is None: self.reproject_to = embedding_dim
164+
rnn_input_dim: int = self.reproject_to
160165

166+
self.relearn_embeddings: bool = True
161167
if self.relearn_embeddings:
162-
self.embedding2nn = torch.nn.Linear(rnn_input_dim, rnn_input_dim)
168+
self.embedding2nn = torch.nn.Linear(embedding_dim, rnn_input_dim)
163169

164170
self.train_initial_hidden_state = train_initial_hidden_state
165171
self.bidirectional = True
@@ -237,6 +243,7 @@ def _get_state_dict(self):
237243
"rnn_type": self.rnn_type,
238244
"beta": self.beta,
239245
"weight_dict": self.weight_dict,
246+
"reproject_to": self.reproject_to,
240247
}
241248
return model_state
242249

@@ -260,6 +267,7 @@ def _init_model_with_state_dict(state):
260267
)
261268
beta = 1.0 if "beta" not in state.keys() else state["beta"]
262269
weights = None if "weight_dict" not in state.keys() else state["weight_dict"]
270+
reproject_to = None if "reproject_to" not in state.keys() else state["reproject_to"]
263271

264272
model = SequenceTagger(
265273
hidden_size=state["hidden_size"],
@@ -276,6 +284,7 @@ def _init_model_with_state_dict(state):
276284
rnn_type=rnn_type,
277285
beta=beta,
278286
loss_weights=weights,
287+
reproject_to=reproject_to,
279288
)
280289
model.load_state_dict(state["state_dict"])
281290
return model
@@ -1006,7 +1015,7 @@ def _fetch_model(model_name) -> str:
10061015
[hu_path, "release-de-pos-0", "de-pos-ud-hdt-v0.5.pt"]
10071016
)
10081017

1009-
model_map["de-pos-fine-grained"] = "/".join(
1018+
model_map["de-pos-tweets"] = "/".join(
10101019
[
10111020
aws_resource_path_v04,
10121021
"POS-fine-grained-german-tweets",
@@ -1028,8 +1037,8 @@ def _fetch_model(model_name) -> str:
10281037
model_map["nl-ner"] = "/".join(
10291038
[aws_resource_path_v04, "NER-conll2002-dutch", "nl-ner-conll02-v0.1.pt"]
10301039
)
1031-
model_map["ml-pos"] = "https://raw.githubusercontent.com/qburst/models-repository/master/FlairMalayalamModels/malayalam-upos-model.pt"
1032-
model_map["ml-xpos"] = "https://raw.githubusercontent.com/qburst/models-repository/master/FlairMalayalamModels/malayalam-xpos-model.pt"
1040+
model_map["ml-pos"] = "https://raw.githubusercontent.com/qburst/models-repository/master/FlairMalayalamModels/malayalam-xpos-model.pt"
1041+
model_map["ml-upos"] = "https://raw.githubusercontent.com/qburst/models-repository/master/FlairMalayalamModels/malayalam-upos-model.pt"
10331042

10341043
cache_dir = Path("models")
10351044
if model_name in model_map:

flair/trainers/trainer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def train(
6969
scheduler = AnnealOnPlateau,
7070
anneal_factor: float = 0.5,
7171
patience: int = 3,
72+
initial_extra_patience = 0,
7273
min_learning_rate: float = 0.0001,
7374
train_with_dev: bool = False,
7475
monitor_train: bool = False,
@@ -150,6 +151,8 @@ def train(
150151
if learning_rate < min_learning_rate:
151152
min_learning_rate = learning_rate / 10
152153

154+
initial_learning_rate = learning_rate
155+
153156
# cast string to Path
154157
if type(base_path) is str:
155158
base_path = Path(base_path)
@@ -228,6 +231,7 @@ def train(
228231
optimizer,
229232
factor=anneal_factor,
230233
patience=patience,
234+
initial_extra_patience=initial_extra_patience,
231235
mode=anneal_mode,
232236
verbose=True,
233237
)
@@ -492,6 +496,7 @@ def train(
492496
new_learning_rate = group["lr"]
493497
if new_learning_rate != previous_learning_rate:
494498
bad_epochs = patience + 1
499+
if previous_learning_rate == initial_learning_rate: bad_epochs += initial_extra_patience
495500

496501
# log bad epochs
497502
log.info(f"BAD EPOCHS (no improvement): {bad_epochs}")

flair/training_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class AnnealOnPlateau(object):
343343
>>> scheduler.step(val_loss)
344344
"""
345345

346-
def __init__(self, optimizer, mode='min', aux_mode='min', factor=0.1, patience=10,
346+
def __init__(self, optimizer, mode='min', aux_mode='min', factor=0.1, patience=10, initial_extra_patience=0,
347347
verbose=False, cooldown=0, min_lr=0, eps=1e-8):
348348

349349
if factor >= 1.0:
@@ -364,7 +364,8 @@ def __init__(self, optimizer, mode='min', aux_mode='min', factor=0.1, patience=1
364364
else:
365365
self.min_lrs = [min_lr] * len(optimizer.param_groups)
366366

367-
self.patience = patience
367+
self.default_patience = patience
368+
self.effective_patience = patience + initial_extra_patience
368369
self.verbose = verbose
369370
self.cooldown = cooldown
370371
self.cooldown_counter = 0
@@ -423,10 +424,11 @@ def step(self, metric, auxiliary_metric = None):
423424
self.cooldown_counter -= 1
424425
self.num_bad_epochs = 0 # ignore any bad epochs in cooldown
425426

426-
if self.num_bad_epochs > self.patience:
427+
if self.num_bad_epochs > self.effective_patience:
427428
self._reduce_lr(epoch)
428429
self.cooldown_counter = self.cooldown
429430
self.num_bad_epochs = 0
431+
self.effective_patience = self.default_patience
430432

431433
self._last_lr = [group['lr'] for group in self.optimizer.param_groups]
432434

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ scikit-learn>=0.21.3
1010
sqlitedict>=1.6.0
1111
deprecated>=1.2.4
1212
hyperopt>=0.1.1
13-
transformers>=2.6.0
13+
transformers>=2.10.0
1414
bpemb>=0.2.9
1515
regex
1616
tabulate

0 commit comments

Comments
 (0)