flair.models#
- class flair.models.SpanClassifier(embeddings, label_dictionary, pooling_operation='first_last', label_type='nel', candidates=None, **classifierargs)View on GitHub#
Bases:
DefaultClassifier
[Sentence
,Span
]Entity Linking Model.
The model expects text/sentences with annotated entity mentions and predicts entities to these mentions. To this end a word embedding is used to embed the sentences and the embedding of the entity mention goes through a linear layer to get the actual class label. The model is able to predict ‘<unk>’ for entity mentions that the model can not confidently match to any of the known labels.
- emb_first(span, embedding_names)View on GitHub#
- emb_last(span, embedding_names)View on GitHub#
- emb_firstAndLast(span, embedding_names)View on GitHub#
- emb_mean(span, embedding_names)View on GitHub#
- property label_type#
Each model predicts labels of a certain type.
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- class flair.models.LanguageModel(dictionary, is_forward_lm, hidden_size, nlayers, embedding_size=100, nout=None, document_delimiter='\\n', dropout=0.1, recurrent_type='LSTM', has_decoder=True)View on GitHub#
Bases:
Module
Container module with an encoder, a recurrent module, and a decoder.
- init_weights()View on GitHub#
- forward(input, hidden, ordered_sequence_lengths=None, decode=True)View on GitHub#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- get_representation(strings, start_marker, end_marker, chars_per_chunk=512)View on GitHub#
- get_output(text)View on GitHub#
Wraps hidden states in new Variables, to detach them from their history.
- static initialize(matrix)View on GitHub#
- classmethod load_language_model(model_file, has_decoder=True)View on GitHub#
- classmethod load_checkpoint(model_file)View on GitHub#
- save_checkpoint(file, optimizer, epoch, split, loss)View on GitHub#
- save(file)View on GitHub#
- generate_text(prefix='\\n', number_of_characters=1000, temperature=1.0, break_on_suffix=None)View on GitHub#
- Return type:
Tuple
[str
,float
]
- calculate_perplexity(text)View on GitHub#
- Return type:
float
- class flair.models.Lemmatizer(embeddings=None, label_type='lemma', rnn_input_size=50, rnn_hidden_size=256, rnn_layers=2, encode_characters=True, char_dict='common-chars-lemmatizer', max_sequence_length_dependent_on_input=True, max_sequence_length=20, use_attention=True, beam_size=1, start_symbol_for_encoding=True, end_symbol_for_encoding=True, bidirectional_encoding=True)View on GitHub#
Bases:
Classifier
[Sentence
]- property label_type#
Each model predicts labels of a certain type.
- words_to_char_indices(tokens, end_symbol=True, start_symbol=False, padding_in_front=False, seq_length=None)View on GitHub#
For a given list of strings this function creates index vectors that represent the characters of the strings.
Each string is represented by sequence_length (maximum string length + entries for special symbol) many indices representing characters in self.char_dict. One can manually set the vector length with the parameter seq_length, though the vector length is always at least maximum string length in the list.
- Parameters:
seq_length – the maximum sequence length to use, if None the maximum is taken..
tokens (
List
[str
]) – the texts of the toekens to encodeend_symbol – add self.end_index at the end of each representation
start_symbol – add self.start_index in front of each representation
padding_in_front – whether to fill up with self.dummy_index in front or in back of strings
- forward_pass(sentences)View on GitHub#
- decode(decoder_input_indices, initial_hidden_states, all_encoder_outputs)View on GitHub#
- forward(encoder_input_indices, lengths, token_embedding_hidden)View on GitHub#
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
Tuple
[Tensor
,Optional
[Tensor
]]Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- encode(sentences)View on GitHub#
- encode_token(token)View on GitHub#
- forward_loss(sentences)View on GitHub#
Performs a forward pass and returns a loss tensor for backpropagation.
Implement this to enable training.
- Return type:
Tuple
[Tensor
,int
]
- predict(sentences, mini_batch_size=16, return_probabilities_for_all_classes=False, verbose=False, label_name='predicted', return_loss=False, embedding_storage_mode='none')View on GitHub#
Predict lemmas of words for a given (list of) sentence(s).
- Parameters:
sentences (
Union
[List
[Sentence
],Sentence
]) – sentences to predictlabel_name – label name used for predicted lemmas
mini_batch_size (
int
) – number of tokens that are send through the RNN simultaneously, assuming batching_in_rnn is set to Trueembedding_storage_mode – default is ‘none’ which is always best. Only set to ‘cpu’ or ‘gpu’ if you wish to not only predict, but also keep the generated embeddings in CPU or GPU memory respectively.
return_loss – whether to compute and return loss. Setting it to True only makes sense if labels are provided
verbose (
bool
) – If True, lemmatized sentences will be printed in the console.return_probabilities_for_all_classes (
bool
) – unused parameter.
- evaluate(*args, **kwargs)View on GitHub#
Evaluates the model. Returns a Result object containing evaluation results and a loss value.
Implement this to enable evaluation.
- Parameters:
data_points – The labeled data_points to evaluate.
gold_label_type – The label type indicating the gold labels
out_path – Optional output path to store predictions
embedding_storage_mode – One of ‘none’, ‘cpu’ or ‘gpu’. ‘none’ means all embeddings are deleted and freshly recomputed, ‘cpu’ means all embeddings are stored on CPU, or ‘gpu’ means all embeddings are stored on GPU
mini_batch_size – The batch_size to use for predictions
main_evaluation_metric – Specify which metric to highlight as main_score
exclude_labels – Specify classes that won’t be considered in evaluation
gold_label_dictionary – Specify which classes should be considered, all other classes will be taken as <unk>.
return_loss – Weather to additionally compute the loss on the data-points.
**kwargs – Arguments that will be ignored.
- Return type:
Result
- Returns:
The evaluation results.
- class flair.models.TextPairClassifier(embeddings, label_type, embed_separately=False, **classifierargs)View on GitHub#
Bases:
DefaultClassifier
[DataPair
[Sentence
,Sentence
],DataPair
[Sentence
,Sentence
]]Text Pair Classification Model for tasks such as Recognizing Textual Entailment, build upon TextClassifier.
The model takes document embeddings and puts resulting text representation(s) into a linear layer to get the actual class label. We provide two ways to embed the DataPairs: Either by embedding both DataPoints and concatenating the resulting vectors (“embed_separately=True”) or by concatenating the DataPoints and embedding the resulting vector (“embed_separately=False”).
- property label_type#
Each model predicts labels of a certain type.
- get_used_tokens(corpus, context_length=0, respect_document_boundaries=True)View on GitHub#
- Return type:
Iterable
[List
[str
]]
- class flair.models.TextPairRegressor(embeddings, label_type, embed_separately=False, dropout=0.0, locked_dropout=0.0, word_dropout=0.0, decoder=None)View on GitHub#
Bases:
Model
[DataPair
[Sentence
,Sentence
]],ReduceTransformerVocabMixin
Text Pair Regression Model for tasks such as Semantic Textual Similarity Benchmark.
The model takes document embeddings and puts resulting text representation(s) into a linear layer to get the score. We provide two ways to embed the DataPairs: Either by embedding both DataPoints and concatenating the resulting vectors (“embed_separately=True”) or by concatenating the DataPoints and embedding the resulting vector (“embed_separately=False”).
- property label_type#
Each model predicts labels of a certain type.
- get_used_tokens(corpus, context_length=0, respect_document_boundaries=True)View on GitHub#
- Return type:
Iterable
[List
[str
]]
- forward_loss(pairs)View on GitHub#
Performs a forward pass and returns a loss tensor for backpropagation.
Implement this to enable training.
- Return type:
Tuple
[Tensor
,int
]
- predict(pairs, mini_batch_size=32, verbose=False, label_name=None, embedding_storage_mode='none')View on GitHub#
- evaluate(data_points, gold_label_type, out_path=None, embedding_storage_mode='none', mini_batch_size=32, main_evaluation_metric=('micro avg', 'f1-score'), exclude_labels=[], gold_label_dictionary=None, return_loss=True, **kwargs)View on GitHub#
Evaluates the model. Returns a Result object containing evaluation results and a loss value.
Implement this to enable evaluation.
- Parameters:
data_points (
Union
[List
[DataPair
[Sentence
,Sentence
]],Dataset
]) – The labeled data_points to evaluate.gold_label_type (
str
) – The label type indicating the gold labelsout_path (
Union
[str
,Path
,None
]) – Optional output path to store predictionsembedding_storage_mode (
str
) – One of ‘none’, ‘cpu’ or ‘gpu’. ‘none’ means all embeddings are deleted and freshly recomputed, ‘cpu’ means all embeddings are stored on CPU, or ‘gpu’ means all embeddings are stored on GPUmini_batch_size (
int
) – The batch_size to use for predictionsmain_evaluation_metric (
Tuple
[str
,str
]) – Specify which metric to highlight as main_scoreexclude_labels (
List
[str
]) – Specify classes that won’t be considered in evaluationgold_label_dictionary (
Optional
[Dictionary
]) – Specify which classes should be considered, all other classes will be taken as <unk>.return_loss (
bool
) – Weather to additionally compute the loss on the data-points.**kwargs – Arguments that will be ignored.
- Return type:
Result
- Returns:
The evaluation results.
- class flair.models.RelationClassifier(embeddings, label_dictionary, label_type, entity_label_types, entity_pair_labels=None, entity_threshold=None, cross_augmentation=True, encoding_strategy=<flair.models.relation_classifier_model.TypedEntityMarker object>, zero_tag_value='O', allow_unk_tag=True, **classifierargs)View on GitHub#
Bases:
DefaultClassifier
[EncodedSentence
,EncodedSentence
]Relation Classifier to predict the relation between two entities.
Task#
Relation Classification (RC) is the task of identifying the semantic relation between two entities in a text. In contrast to (end-to-end) Relation Extraction (RE), RC requires pre-labelled entities.
Example:#
For the founded_by relation from ORG (head) to PER (tail) and the sentence “Larry Page and Sergey Brin founded Google .”, we extract the relations - founded_by(head=’Google’, tail=’Larry Page’) and - founded_by(head=’Google’, tail=’Sergey Brin’).
Architecture#
The Relation Classifier Model builds upon a text classifier. The model generates an encoded sentence for each entity pair in the cross product of all entities in the original sentence. In the encoded representation, the entities in the current entity pair are masked/marked with control tokens. (For an example, see the docstrings of different encoding strategies, e.g.
TypedEntityMarker
.) Then, for each encoded sentence, the model takes its document embedding and puts the resulting text representation(s) through a linear layer to get the class relation label.The implemented encoding strategies are taken from this paper by Zhou et al.: https://arxiv.org/abs/2102.01373
Warning
Currently, the model has no multi-label support.
- transform_sentence(sentences)View on GitHub#
Transforms sentences into encoded sentences specific to the RelationClassifier.
For more information on the internal sentence transformation procedure, see the
flair.models.RelationClassifier
architecture and the differentflair.models.relation_classifier_model.EncodingStrategy
variants docstrings.
- transform_dataset(dataset)View on GitHub#
Transforms a dataset into a dataset containing encoded sentences specific to the RelationClassifier.
The returned dataset is stored in memory. For more information on the internal sentence transformation procedure, see the
RelationClassifier
architecture and the differentEncodingStrategy
variants docstrings.- Parameters:
dataset (
Dataset
[Sentence
]) – A dataset of sentences to transform- Return type:
FlairDatapointDataset
[EncodedSentence
]
Returns: A dataset of encoded sentences specific to the RelationClassifier
- transform_corpus(corpus)View on GitHub#
Transforms a corpus into a corpus containing encoded sentences specific to the RelationClassifier.
The splits of the returned corpus are stored in memory. For more information on the internal sentence transformation procedure, see the
RelationClassifier
architecture and the differentEncodingStrategy
variants docstrings.- Parameters:
corpus (
Corpus
[Sentence
]) – A corpus of sentences to transform- Return type:
Corpus
[EncodedSentence
]
Returns: A corpus of encoded sentences specific to the RelationClassifier
- predict(sentences, mini_batch_size=32, return_probabilities_for_all_classes=False, verbose=False, label_name=None, return_loss=False, embedding_storage_mode='none')View on GitHub#
Predicts the class labels for the given sentence(s).
Standard Sentence objects and EncodedSentences specific to the RelationClassifier are allowed as input. The (relation) labels are directly added to the sentences.
- Parameters:
sentences (
Union
[List
[Sentence
],List
[EncodedSentence
],Sentence
,EncodedSentence
]) – A list of (encoded) sentences.mini_batch_size (
int
) – The mini batch size to usereturn_probabilities_for_all_classes (
bool
) – Return probabilities for all classes instead of only best predictedverbose (
bool
) – Set to display a progress barreturn_loss (
bool
) – Set to return losslabel_name (
Optional
[str
]) – Set to change the predicted label type nameembedding_storage_mode (
str
) – The default is ‘none’, which is always best. Only set to ‘cpu’ or ‘gpu’ if you wish to predict and keep the generated embeddings in CPU or GPU memory, respectively.
- Return type:
Optional
[Tuple
[Tensor
,int
]]
Returns: The loss and the total number of classes, if return_loss is set
- property label_type: str#
Each model predicts labels of a certain type.
- property zero_tag_value: str#
- property allow_unk_tag: bool#
- get_used_tokens(corpus, context_length=0, respect_document_boundaries=True)View on GitHub#
- Return type:
Iterable
[List
[str
]]
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- class flair.models.RelationExtractor(embeddings, label_type, entity_label_type, entity_pair_filters=None, pooling_operation='first_last', train_on_gold_pairs_only=False, **classifierargs)View on GitHub#
Bases:
DefaultClassifier
[Sentence
,Relation
]- property label_type#
Each model predicts labels of a certain type.
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- class flair.models.RegexpTagger(mapping)View on GitHub#
Bases:
object
- property registered_labels#
- register_labels(mapping)View on GitHub#
Register a regexp -> label mapping.
- Parameters:
mapping (
Union
[List
[Tuple
[str
,str
]],Tuple
[str
,str
]]) – A list of tuples or a single tuple representing a mapping as regexp -> label
- remove_labels(labels)View on GitHub#
Remove a registered regexp -> label mapping given by label.
- Parameters:
labels (
Union
[List
[str
],str
]) – A list of labels or a single label as strings.
- predict(sentences)View on GitHub#
Predict the given sentences according to the registered mappings.
- Return type:
List
[Sentence
]
- class flair.models.SequenceTagger(embeddings, tag_dictionary, tag_type, use_rnn=True, rnn=None, rnn_type='LSTM', tag_format='BIOES', hidden_size=256, rnn_layers=1, bidirectional=True, use_crf=True, reproject_embeddings=True, dropout=0.0, word_dropout=0.05, locked_dropout=0.5, train_initial_hidden_state=False, loss_weights=None, init_from_state_dict=False, allow_unk_predictions=False)View on GitHub#
Bases:
Classifier
[Sentence
]- property label_type#
Each model predicts labels of a certain type.
- static RNN(rnn_type, rnn_layers, hidden_size, bidirectional, rnn_input_dim)View on GitHub#
Static wrapper function returning an RNN instance from PyTorch.
- Parameters:
rnn_type (
str
) – Type of RNN from torch.nnrnn_layers (
int
) – number of layers to includehidden_size (
int
) – hidden size of RNN cellbidirectional (
bool
) – If True, RNN cell is bidirectionalrnn_input_dim (
int
) – Input dimension to RNN cell
- Return type:
RNN
- forward_loss(sentences)View on GitHub#
Performs a forward pass and returns a loss tensor for backpropagation.
Implement this to enable training.
- Return type:
Tuple
[Tensor
,int
]
- forward(sentence_tensor, lengths)View on GitHub#
Forward propagation through network.
- Parameters:
sentence_tensor (
Tensor
) – A tensor representing the batch of sentences.lengths (
LongTensor
) – A IntTensor representing the lengths of the respective sentences.
- predict(sentences, mini_batch_size=32, return_probabilities_for_all_classes=False, verbose=False, label_name=None, return_loss=False, embedding_storage_mode='none', force_token_predictions=False)View on GitHub#
Predicts labels for current batch with CRF or Softmax.
- Parameters:
sentences (
Union
[List
[Sentence
],Sentence
]) – List of sentences in batchmini_batch_size (
int
) – batch size for test datareturn_probabilities_for_all_classes (
bool
) – Whether to return probabilities for all classesverbose (
bool
) – whether to use progress barlabel_name (
Optional
[str
]) – which label to predictreturn_loss – whether to return loss value
embedding_storage_mode – determines where to store embeddings - can be “gpu”, “cpu” or None.
force_token_predictions (
bool
) – add labels per token instead of span labels, even if self.predict_spans is True
- push_to_hub(repo_id, token=None, private=None, commit_message='Add new SequenceTagger model.')View on GitHub#
Uploads the Sequence Tagger model to a Hugging Face Hub repository.
- Parameters:
repo_id (
str
) – A namespace (user or an organization) and a repo name separated by a /.token (
Optional
[str
]) – An authentication token (See https://huggingface.co/settings/token).private (
Optional
[bool
]) – Whether the repository is private.commit_message (
str
) – Message to commit while pushing.
Returns: The url of the repository.
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- class flair.models.TokenClassifier(embeddings, label_dictionary, label_type, span_encoding='BIOES', **classifierargs)View on GitHub#
Bases:
DefaultClassifier
[Sentence
,Token
]This is a simple class of models that tags individual words in text.
- property label_type#
Each model predicts labels of a certain type.
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- class flair.models.WordTagger(embeddings, label_dictionary, label_type, span_encoding='BIOES', **classifierargs)View on GitHub#
Bases:
TokenClassifier
Deprecated since version 0.12.2: The WordTagger was renamed to
flair.models.TokenClassifier
.
- class flair.models.FewshotClassifierView on GitHub#
Bases:
Classifier
[Sentence
],ABC
- forward_loss(data_points)View on GitHub#
Performs a forward pass and returns a loss tensor for backpropagation.
Implement this to enable training.
- Return type:
Tuple
[Tensor
,int
]
- property tars_embeddings#
- train(mode=True)View on GitHub#
Populate label similarity map based on cosine similarity before running epoch.
If the num_negative_labels_to_sample is set to an integer value then before starting each epoch the model would create a similarity measure between the label names based on cosine distances between their BERT encoded embeddings.
- get_current_label_dictionary()View on GitHub#
- get_current_label_type()View on GitHub#
- is_current_task_multi_label()View on GitHub#
- add_and_switch_to_new_task(task_name, label_dictionary, label_type, multi_label=True, force_switch=False)View on GitHub#
Adds a new task to an existing TARS model.
Sets necessary attributes and finally ‘switches’ to the new task. Parameters are similar to the constructor except for model choice, batch size and negative sampling. This method does not store the resultant model onto disk.
- Parameters:
task_name (
str
) – a string depicting the name of the tasklabel_dictionary (
Union
[List
,Set
,Dictionary
,str
]) – dictionary of the labels you want to predictlabel_type (
str
) – string to identify the label type (‘ner’, ‘sentiment’, etc.)multi_label (
bool
) – whether this task is a multi-label prediction problemforce_switch (
bool
) – if True, will overwrite existing task with same name
- list_existing_tasks()View on GitHub#
Lists existing tasks in the loaded TARS model on the console.
- Return type:
Set
[str
]
- switch_to_task(task_name)View on GitHub#
Switches to a task which was previously added.
- property label_type#
Each model predicts labels of a certain type.
- predict_zero_shot(sentences, candidate_label_set, multi_label=True)View on GitHub#
Make zero shot predictions from the TARS model.
- get_used_tokens(corpus, context_length=0, respect_document_boundaries=True)View on GitHub#
- Return type:
Iterable
[List
[str
]]
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- class flair.models.TARSClassifier(task_name=None, label_dictionary=None, label_type=None, embeddings='bert-base-uncased', num_negative_labels_to_sample=2, prefix=True, **tagger_args)View on GitHub#
Bases:
FewshotClassifier
TARS model for text classification.
In the backend, the model uses a BERT based binary text classifier which given a <label, text> pair predicts the probability of two classes “True”, and “False”. The input data is a usual Sentence object which is inflated by the model internally before pushing it through the transformer stack of BERT.
- static_label_type = 'tars_label'#
- LABEL_MATCH = 'YES'#
- LABEL_NO_MATCH = 'NO'#
- property tars_embeddings#
- predict(sentences, mini_batch_size=32, return_probabilities_for_all_classes=False, verbose=False, label_name=None, return_loss=False, embedding_storage_mode='none', label_threshold=0.5, multi_label=None, force_label=False)View on GitHub#
Predict sentences on the Text Classification task.
- Parameters:
return_probabilities_for_all_classes (
bool
) – if True, all classes will be added with their respective confidences.sentences (
Union
[List
[Sentence
],Sentence
]) – a Sentence or a List of Sentenceforce_label (
bool
) – when multilabel is active, you can force to always get at least one prediction.multi_label (
Optional
[bool
]) – if True multiple labels can be predicted. Defaults to the setting of the configured task.label_threshold (
float
) – when multi_label, specify the threshold when a class is considered as predicted.mini_batch_size – size of the minibatch, usually bigger is more rapid but consume more memory, up to a point when it has no more effect.
all_tag_prob – True to compute the score for each tag on each token, otherwise only the score of the best tag is returned
verbose (
bool
) – set to True to display a progress barreturn_loss – set to True to also compute the loss
label_name (
Optional
[str
]) – set this to change the name of the label type that is predictedembedding_storage_mode – default is ‘none’ which doesn’t store the embeddings in RAM. Only set to ‘cpu’ or ‘gpu’ if you wish to not only predict, but also keep the generated embeddings in CPU or GPU memory respectively. ‘gpu’ to store embeddings in GPU memory.
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- class flair.models.TARSTagger(task_name=None, label_dictionary=None, label_type=None, embeddings='bert-base-uncased', num_negative_labels_to_sample=2, prefix=True, **tagger_args)View on GitHub#
Bases:
FewshotClassifier
TARS model for sequence tagging.
In the backend, the model uses a BERT based 5-class sequence labeler which given a <label, text> pair predicts the probability for each word to belong to one of the BIOES classes. The input data is a usual Sentence object which is inflated by the model internally before pushing it through the transformer stack of BERT.
- static_label_type = 'tars_label'#
- property tars_embeddings#
- predict(sentences, mini_batch_size=32, return_probabilities_for_all_classes=False, verbose=False, label_name=None, return_loss=False, embedding_storage_mode='none', most_probable_first=True)View on GitHub#
Predict sequence tags for Named Entity Recognition task.
- Parameters:
sentences (
Union
[List
[Sentence
],Sentence
]) – a Sentence or a List of Sentencemini_batch_size – size of the minibatch, usually bigger is more rapid but consume more memory, up to a point when it has no more effect.
all_tag_prob – True to compute the score for each tag on each token, otherwise only the score of the best tag is returned
verbose (
bool
) – set to True to display a progress barreturn_loss – set to True to also compute the loss
label_name (
Optional
[str
]) – set this to change the name of the label type that is predictedembedding_storage_mode – default is ‘none’ which doesn’t store the embeddings in RAM. Only set to ‘cpu’ or ‘gpu’ if you wish to not only predict, but also keep the generated embeddings in CPU or GPU memory respectively. ‘gpu’ to store embeddings in GPU memory.
return_probabilities_for_all_classes (
bool
) – if True, all classes will be added with their respective confidences.most_probable_first (
bool
) – if True, nested predictions will be removed, if False all predictions will be returned, including overlaps
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- class flair.models.TextClassifier(embeddings, label_type, **classifierargs)View on GitHub#
Bases:
DefaultClassifier
[Sentence
,Sentence
]Text Classification Model.
The model takes word embeddings, puts them into an RNN to obtain a text representation, and puts the text representation in the end into a linear layer to get the actual class label. The model can handle single and multi class data sets.
- property label_type#
Each model predicts labels of a certain type.
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- class flair.models.TextRegressor(document_embeddings, label_name='label')View on GitHub#
Bases:
Model
[Sentence
],ReduceTransformerVocabMixin
- property label_type#
Each model predicts labels of a certain type.
- forward(*args)View on GitHub#
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
Tensor
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- forward_loss(sentences)View on GitHub#
Performs a forward pass and returns a loss tensor for backpropagation.
Implement this to enable training.
- Return type:
Tuple
[Tensor
,int
]
- predict(sentences, mini_batch_size=32, verbose=False, label_name=None, embedding_storage_mode='none')View on GitHub#
- Return type:
List
[Sentence
]
- forward_labels_and_loss(sentences)View on GitHub#
- Return type:
Tuple
[Tensor
,Tensor
]
- evaluate(data_points, gold_label_type, out_path=None, embedding_storage_mode='none', mini_batch_size=32, main_evaluation_metric=('micro avg', 'f1-score'), exclude_labels=[], gold_label_dictionary=None, return_loss=True, **kwargs)View on GitHub#
Evaluates the model. Returns a Result object containing evaluation results and a loss value.
Implement this to enable evaluation.
- Parameters:
data_points (
Union
[List
[Sentence
],Dataset
]) – The labeled data_points to evaluate.gold_label_type (
str
) – The label type indicating the gold labelsout_path (
Union
[str
,Path
,None
]) – Optional output path to store predictionsembedding_storage_mode (
str
) – One of ‘none’, ‘cpu’ or ‘gpu’. ‘none’ means all embeddings are deleted and freshly recomputed, ‘cpu’ means all embeddings are stored on CPU, or ‘gpu’ means all embeddings are stored on GPUmini_batch_size (
int
) – The batch_size to use for predictionsmain_evaluation_metric (
Tuple
[str
,str
]) – Specify which metric to highlight as main_scoreexclude_labels (
List
[str
]) – Specify classes that won’t be considered in evaluationgold_label_dictionary (
Optional
[Dictionary
]) – Specify which classes should be considered, all other classes will be taken as <unk>.return_loss (
bool
) – Weather to additionally compute the loss on the data-points.**kwargs – Arguments that will be ignored.
- Return type:
Result
- Returns:
The evaluation results.
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model
- get_used_tokens(corpus, context_length=0, respect_document_boundaries=True)View on GitHub#
- Return type:
Iterable
[List
[str
]]
- class flair.models.ClusteringModel(model, embeddings)View on GitHub#
Bases:
object
A wrapper class to apply sklearn clustering models on DocumentEmbeddings.
- fit(corpus, **kwargs)View on GitHub#
Trains the model.
- Parameters:
corpus (
Corpus
) – the flair corpus this wrapper will use for fitting the model.**kwargs – parameters propagated to the models .fit() method.
- predict(corpus)View on GitHub#
Predict labels given a list of sentences and returns the respective class indices.
- Parameters:
corpus (
Corpus
) – the flair corpus this wrapper will use for predicting the labels.
- save(model_file)View on GitHub#
Saves current model.
- Parameters:
model_file (
Union
[str
,Path
]) – path where to save the model.
- static load(model_file)View on GitHub#
Loads a model from a given path.
- Parameters:
model_file (
Union
[str
,Path
]) – path to the file where the model is saved.
- evaluate(corpus, label_type)View on GitHub#
This method calculates some evaluation metrics for the clustering.
Also, the result of the evaluation is logged.
- Parameters:
corpus (
Corpus
) – the flair corpus this wrapper will use for evaluation.label_type (
str
) – the label from the sentence will be used for the evaluation.
- class flair.models.MultitaskModel(models, task_ids=None, loss_factors=None, use_all_tasks=False)View on GitHub#
Bases:
Classifier
Multitask Model class which acts as wrapper for creating custom multitask models.
Takes different tasks as input, parameter sharing is done by objects in flair, i.e. creating a Embedding Layer and passing it to two different Models, will result in a hard parameter-shared embedding layer. The abstract class takes care of calling the correct forward propagation and loss function of the respective model.
- forward(*args)View on GitHub#
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
Tensor
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- forward_loss(sentences)View on GitHub#
Calls the respective forward loss of each model and sums them weighted by their loss factors.
- Parameters:
sentences (
Union
[List
[Sentence
],Sentence
]) – batch of sentences- Return type:
Tuple
[Tensor
,int
]
Returns: loss and sample count
- predict(sentences, **predictargs)View on GitHub#
Predicts the class labels for the given sentences.
The labels are directly added to the sentences.
- Parameters:
sentences – list of sentences
mini_batch_size – mini batch size to use
return_probabilities_for_all_classes – return probabilities for all classes instead of only best predicted
verbose – set to True to display a progress bar
return_loss – set to True to return loss
label_name – set this to change the name of the label type that is predicted # noqa: E501
embedding_storage_mode – default is ‘none’ which is always best. Only set to ‘cpu’ or ‘gpu’ if you wish to not only predict, but also keep the generated embeddings in CPU or GPU memory respectively. ‘gpu’ to store embeddings in GPU memory. # noqa: E501
- static split_batch_to_task_ids(sentences, all_tasks=False)View on GitHub#
Splits a batch of sentences to its respective model.
If single sentence is assigned to several tasks (i.e. same corpus but different tasks), then the model assignment for this batch is randomly chosen.
- Parameters:
- Return type:
Dict
Returns: Key-value pairs as (task_id, list of sentences ids in batch)
- evaluate(data_points, gold_label_type, out_path=None, main_evaluation_metric=('micro avg', 'f1-score'), evaluate_all=True, **evalargs)View on GitHub#
Evaluates the model. Returns a Result object containing evaluation results and a loss value.
- Parameters:
data_points – batch of sentences
gold_label_type (
str
) – if evaluate_all is False, specify the task to evaluate by the task_id.out_path (
Union
[str
,Path
,None
]) – if not None, predictions will be created and saved at the respective file.main_evaluation_metric (
Tuple
[str
,str
]) – Specify which metric to highlight as main_scoreevaluate_all (
bool
) – choose if all tasks should be evaluated, or a single one, depending on gold_label_type**evalargs – arguments propagated to
flair.nn.Model.evaluate()
- Return type:
Result
Returns: Tuple of Result object and loss value (float)
- get_used_tokens(corpus, context_length=0, respect_document_boundaries=True)View on GitHub#
- Return type:
Iterable
[List
[str
]]
- property label_type#
Each model predicts labels of a certain type.
- classmethod load(model_path)View on GitHub#
Loads the model from the given file.
- Parameters:
model_path (
Union
[str
,Path
,Dict
[str
,Any
]]) – the model file or the already loaded state dict- Return type:
Returns: the loaded text classifier model