class hanlp.layers.transformers.encoder.TransformerEncoder(transformer: Union[transformers.modeling_utils.PreTrainedModel, str], transformer_tokenizer: transformers.tokenization_utils.PreTrainedTokenizer, average_subwords=False, scalar_mix: Optional[Union[hanlp.layers.scalar_mix.ScalarMixWithDropoutBuilder, int]] = None, word_dropout=None, max_sequence_length=None, ret_raw_hidden_states=False, transformer_args: Optional[Dict[str, Any]] = None, trainable=typing.Union[bool, typing.Tuple[int, int], NoneType], training=True)[source]

A pre-trained transformer encoder.

  • transformer – A PreTrainedModel or an identifier of a PreTrainedModel.

  • transformer_tokenizer – A PreTrainedTokenizer.

  • average_subwordsTrue to 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. If None, then the max_position_embeddings of the transformer will be used.

  • ret_raw_hidden_statesTrue to return hidden states of each layer.

  • transformer_args – Extra arguments passed to the transformer.

  • trainableFalse to use static embeddings.

  • trainingFalse to skip loading weights from pre-trained transformers.

forward(input_ids: torch.LongTensor, attention_mask=None, token_type_ids=None, token_span=None, **kwargs)[source]

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 Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.