TransformerEncoder(transformer: Union[transformers.modeling_utils.PreTrainedModel, str], transformer_tokenizer: transformers.tokenization_utils.PreTrainedTokenizer, average_subwords=False, scalar_mix: Union[hanlp.layers.scalar_mix.ScalarMixWithDropoutBuilder, int] = None, word_dropout=None, max_sequence_length=None, ret_raw_hidden_states=False, transformer_args: Dict[str, Any] = None, trainable=typing.Union[bool, typing.Tuple[int, int], NoneType], training=True)¶
A pre-trained transformer encoder.
transformer – A
PreTrainedModelor an identifier of a
transformer_tokenizer – A
Trueto average subword representations.
scalar_mix – Layer attention.
word_dropout – Dropout rate of randomly replacing a subword with MASK.
max_sequence_length – The maximum sequence length. Sequence longer than this will be handled by sliding window.
Trueto return hidden states of each layer.
transformer_args – Extra arguments passed to the transformer.
Falseto use static embeddings.
Falseto skip loading weights from pre-trained transformers.
forward(input_ids: torch.LongTensor, attention_mask=None, token_type_ids=None, token_span=None, **kwargs)¶
Defines the computation performed at every call.
Should be overridden by all subclasses.
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.