flair.models.TARSTagger#
- 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.
- __init__(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#
Initializes a TarsTagger.
- Parameters:
task_name (
Optional
[str
]) – a string depicting the name of the tasklabel_dictionary (
Optional
[Dictionary
]) – dictionary of labels you want to predictlabel_type (
Optional
[str
]) – label_type: name of the labelembeddings (
Union
[TransformerWordEmbeddings
,str
]) – name of the pre-trained transformer model e.g., ‘bert-base-uncased’num_negative_labels_to_sample (
Optional
[int
]) – number of negative labels to sample for each positive labels against a sentence during training. Defaults to 2 negative labels for each positive label. The model would sample all the negative labels if None is passed. That slows down the training considerably.prefix (
bool
) – if True, the label will be concatenated at the start, else on the end.**tagger_args – The arguments propagated to
FewshotClassifier.__init__()
Methods
__init__
([task_name, label_dictionary, ...])Initializes a TarsTagger.
add_and_switch_to_new_task
(task_name, ...[, ...])Adds a new task to an existing TARS model.
add_module
(name, module)Add a child module to the current module.
apply
(fn)Apply
fn
recursively to every submodule (as returned by.children()
) as well as self.bfloat16
()Casts all floating point parameters and buffers to
bfloat16
datatype.buffers
([recurse])Return an iterator over module buffers.
children
()Return an iterator over immediate children modules.
compile
(*args, **kwargs)Compile this Module's forward using
torch.compile()
.cpu
()Move all model parameters and buffers to the CPU.
cuda
([device])Move all model parameters and buffers to the GPU.
double
()Casts all floating point parameters and buffers to
double
datatype.eval
()Set the module in evaluation mode.
evaluate
(data_points, gold_label_type[, ...])Evaluates the model.
extra_repr
()Set the extra representation of the module.
float
()Casts all floating point parameters and buffers to
float
datatype.forward
(*input)Define the computation performed at every call.
forward_loss
(data_points)Performs a forward pass and returns a loss tensor for backpropagation.
get_buffer
(target)Return the buffer given by
target
if it exists, otherwise throw an error.get_current_label_dictionary
()get_current_label_type
()get_extra_state
()Return any extra state to include in the module's state_dict.
get_parameter
(target)Return the parameter given by
target
if it exists, otherwise throw an error.get_submodule
(target)Return the submodule given by
target
if it exists, otherwise throw an error.get_used_tokens
(corpus[, context_length, ...])half
()Casts all floating point parameters and buffers to
half
datatype.ipu
([device])Move all model parameters and buffers to the IPU.
is_current_task_multi_label
()list_existing_tasks
()Lists existing tasks in the loaded TARS model on the console.
load
(model_path)Loads a Flair model from the given file or state dictionary.
load_state_dict
(state_dict[, strict, assign])Copy parameters and buffers from
state_dict
into this module and its descendants.modules
()Return an iterator over all modules in the network.
mtia
([device])Move all model parameters and buffers to the MTIA.
named_buffers
([prefix, recurse, ...])Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
named_children
()Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
named_modules
([memo, prefix, remove_duplicate])Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
named_parameters
([prefix, recurse, ...])Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
parameters
([recurse])Return an iterator over module parameters.
predict
(sentences[, mini_batch_size, ...])Predict sequence tags for Named Entity Recognition task.
predict_zero_shot
(sentences, candidate_label_set)Make zero shot predictions from the TARS model.
print_model_card
()This method produces a log message that includes all recorded parameters the model was trained with.
register_backward_hook
(hook)Register a backward hook on the module.
register_buffer
(name, tensor[, persistent])Add a buffer to the module.
register_forward_hook
(hook, *[, prepend, ...])Register a forward hook on the module.
register_forward_pre_hook
(hook, *[, ...])Register a forward pre-hook on the module.
register_full_backward_hook
(hook[, prepend])Register a backward hook on the module.
register_full_backward_pre_hook
(hook[, prepend])Register a backward pre-hook on the module.
register_load_state_dict_post_hook
(hook)Register a post-hook to be run after module's
load_state_dict()
is called.register_load_state_dict_pre_hook
(hook)Register a pre-hook to be run before module's
load_state_dict()
is called.register_module
(name, module)Alias for
add_module()
.register_parameter
(name, param)Add a parameter to the module.
register_state_dict_post_hook
(hook)Register a post-hook for the
state_dict()
method.register_state_dict_pre_hook
(hook)Register a pre-hook for the
state_dict()
method.requires_grad_
([requires_grad])Change if autograd should record operations on parameters in this module.
save
(model_file[, checkpoint])Saves the current model to the provided file.
set_extra_state
(state)Set extra state contained in the loaded state_dict.
set_submodule
(target, module)Set the submodule given by
target
if it exists, otherwise throw an error.share_memory
()See
torch.Tensor.share_memory_()
.state_dict
(*args[, destination, prefix, ...])Return a dictionary containing references to the whole state of the module.
switch_to_task
(task_name)Switches to a task which was previously added.
to
(*args, **kwargs)Move and/or cast the parameters and buffers.
to_empty
(*, device[, recurse])Move the parameters and buffers to the specified device without copying storage.
train
([mode])Populate label similarity map based on cosine similarity before running epoch.
type
(dst_type)Casts all parameters and buffers to
dst_type
.xpu
([device])Move all model parameters and buffers to the XPU.
zero_grad
([set_to_none])Reset gradients of all model parameters.
Attributes
T_destination
call_super_init
dump_patches
label_type
Each model predicts labels of a certain type.
model_card
- static_label_type = 'tars_label'#
- tars_model: SequenceTagger#
- separator: str#
- 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 a Flair model from the given file or state dictionary.
- Parameters:
model_path (
Union
[str
,Path
,dict
[str
,Any
]]) – Either the path to the model (as string or Path variable) or the already loaded state dict.- Return type:
- Returns:
The loaded Flair model.
- training: bool#