encoder
encoder¶
- 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.
- Parameters
transformer – A
PreTrainedModelor an identifier of aPreTrainedModel.transformer_tokenizer – A
PreTrainedTokenizer.average_subwords –
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. If
None, then themax_position_embeddingsof the transformer will be used.ret_raw_hidden_states –
Trueto return hidden states of each layer.transformer_args – Extra arguments passed to the transformer.
trainable –
Falseto use static embeddings.training –
Falseto 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.
Note
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.