flair.embeddings.transformer#
- flair.embeddings.transformer.pad_sequence_embeddings(all_hidden_states)View on GitHub#
- Return type:
Tensor
- Return type:
Tensor
- flair.embeddings.transformer.combine_strided_tensors(hidden_states, overflow_to_sample_mapping, half_stride, max_length, default_value)View on GitHub#
- Return type:
Tensor
- flair.embeddings.transformer.fill_masked_elements(all_token_embeddings, sentence_hidden_states, mask, word_ids, lengths)View on GitHub#
- flair.embeddings.transformer.insert_missing_embeddings(token_embeddings, word_id, length)View on GitHub#
- Return type:
Tensor
- flair.embeddings.transformer.fill_mean_token_embeddings(all_token_embeddings, sentence_hidden_states, word_ids, token_lengths)View on GitHub#
- flair.embeddings.transformer.document_mean_pooling(sentence_hidden_states, sentence_lengths)View on GitHub#
- flair.embeddings.transformer.document_max_pooling(sentence_hidden_states, sentence_lengths)View on GitHub#
- flair.embeddings.transformer.remove_special_markup(text)View on GitHub#
- class flair.embeddings.transformer.TransformerBaseEmbeddings(name, tokenizer, embedding_length, context_length, context_dropout, respect_document_boundaries, stride, allow_long_sentences, fine_tune, truncate, use_lang_emb, cls_pooling, is_document_embedding=False, is_token_embedding=False, force_device=None, force_max_length=False, feature_extractor=None, needs_manual_ocr=None, use_context_separator=True)View on GitHub#
Bases:
Embeddings
[Sentence
]Base class for all TransformerEmbeddings.
This base class handles the tokenizer and the input preparation, however it won’t implement the actual model. This can be further extended to implement the model in either a pytorch, jit or onnx way of working.
- to_args()View on GitHub#
- classmethod from_params(params)View on GitHub#
- to_params()View on GitHub#
- classmethod create_from_state(**state)View on GitHub#
- property embedding_length: int#
Returns the length of the embedding vector.
- property embedding_type: str#
- prepare_tensors(sentences, device=None)View on GitHub#
- class flair.embeddings.transformer.TransformerOnnxEmbeddings(onnx_model, providers=[], session_options=None, **kwargs)View on GitHub#
Bases:
TransformerBaseEmbeddings
- to_params()View on GitHub#
- classmethod from_params(params)View on GitHub#
- Return type:
- create_session()View on GitHub#
- remove_session()View on GitHub#
- optimize_model(optimize_model_path, use_external_data_format=False, **kwargs)View on GitHub#
Wrapper for onnxruntime.transformers.optimizer.optimize_model.
- quantize_model(quantize_model_path, use_external_data_format=False, **kwargs)View on GitHub#
- classmethod collect_dynamic_axes(embedding, tensors)View on GitHub#
- classmethod export_from_embedding(path, embedding, example_sentences, opset_version=14, providers=None, session_options=None)View on GitHub#
- embeddings_name: str = 'TransformerOnnxEmbeddings'#
- class flair.embeddings.transformer.TransformerJitEmbeddings(jit_model, param_names, **kwargs)View on GitHub#
Bases:
TransformerBaseEmbeddings
- to_params()View on GitHub#
- classmethod from_params(params)View on GitHub#
- Return type:
- classmethod create_from_embedding(module, embedding, param_names)View on GitHub#
- classmethod parameter_to_list(embedding, wrapper, sentences)View on GitHub#
- Return type:
Tuple
[List
[str
],List
[Tensor
]]
- embeddings_name: str = 'TransformerJitEmbeddings'#
- class flair.embeddings.transformer.TransformerJitWordEmbeddings(**kwargs)View on GitHub#
Bases:
TokenEmbeddings
,TransformerJitEmbeddings
- embeddings_name: str = 'TransformerJitWordEmbeddings'#
- class flair.embeddings.transformer.TransformerJitDocumentEmbeddings(**kwargs)View on GitHub#
Bases:
DocumentEmbeddings
,TransformerJitEmbeddings
- embeddings_name: str = 'TransformerJitDocumentEmbeddings'#
- class flair.embeddings.transformer.TransformerOnnxWordEmbeddings(**kwargs)View on GitHub#
Bases:
TokenEmbeddings
,TransformerOnnxEmbeddings
- embeddings_name: str = 'TransformerOnnxWordEmbeddings'#
- class flair.embeddings.transformer.TransformerOnnxDocumentEmbeddings(**kwargs)View on GitHub#
Bases:
DocumentEmbeddings
,TransformerOnnxEmbeddings
- embeddings_name: str = 'TransformerOnnxDocumentEmbeddings'#
- class flair.embeddings.transformer.TransformerEmbeddings(model='bert-base-uncased', fine_tune=True, layers='-1', layer_mean=True, subtoken_pooling='first', cls_pooling='cls', is_token_embedding=True, is_document_embedding=True, allow_long_sentences=False, use_context=False, respect_document_boundaries=True, context_dropout=0.5, saved_config=None, tokenizer_data=None, feature_extractor_data=None, name=None, force_max_length=False, needs_manual_ocr=None, use_context_separator=True, transformers_tokenizer_kwargs={}, transformers_config_kwargs={}, transformers_model_kwargs={}, peft_config=None, peft_gradient_checkpointing_kwargs={}, **kwargs)View on GitHub#
Bases:
TransformerBaseEmbeddings
- onnx_clsView on GitHub#
alias of
TransformerOnnxEmbeddings
- embeddings_name: str = 'TransformerEmbeddings'#
- property embedding_length: int#
Returns the length of the embedding vector.
- property embedding_type: str#
- classmethod from_params(params)View on GitHub#
- to_params()View on GitHub#
- forward(input_ids, sub_token_lengths=None, token_lengths=None, attention_mask=None, overflow_to_sample_mapping=None, word_ids=None, langs=None, bbox=None, pixel_values=None)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.
- export_onnx(path, example_sentences, **kwargs)View on GitHub#
Export TransformerEmbeddings to OnnxFormat.
- Parameters:
path (
Union
[str
,Path
]) – the path to save the embeddings. Notice that the embeddings are stored as external file, hence it matters if the path is an absolue path or a relative one.example_sentences (
List
[Sentence
]) – a list of sentences that will be used for tracing. It is recommended to take 2-4 sentences with some variation.**kwargs – the parameters passed to
TransformerOnnxEmbeddings.export_from_embedding()
- Return type: