mwptoolkit.module.Encoder.transformer_encoder

class mwptoolkit.module.Encoder.transformer_encoder.BertEncoder(hidden_size, dropout_ratio, pretrained_model_path)[source]

Bases: Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

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

token_resize(input_size)[source]
training: bool
class mwptoolkit.module.Encoder.transformer_encoder.GroupATTEncoder(layer, N)[source]

Bases: Module

Group attentional encoder, N layers of group attentional encoder layer.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(inputs, mask)[source]

Pass the input (and mask) through each layer in turn.

Parameters

inputs (torch.Tensor) – input variavle, shape [batch_size, sequence_length, hidden_size].

Returns

encoded variavle, shape [batch_size, sequence_length, hidden_size].

Return type

torch.Tensor

training: bool
class mwptoolkit.module.Encoder.transformer_encoder.TransformerEncoder(embedding_size, ffn_size, num_encoder_layers, num_heads, attn_dropout_ratio=0.0, attn_weight_dropout_ratio=0.0, ffn_dropout_ratio=0.0)[source]

Bases: Module

The stacked Transformer encoder layers.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x, kv=None, self_padding_mask=None, output_all_encoded_layers=False)[source]

Implement the encoding process step by step.

Parameters
  • x (torch.Tensor) – target sequence embedding, shape: [batch_size, sequence_length, embedding_size].

  • kv (torch.Tensor) – the cached history latent vector, shape: [batch_size, sequence_length, embedding_size], default: None.

  • self_padding_mask (torch.Tensor) – padding mask of target sequence, shape: [batch_size, sequence_length], default: None.

  • output_all_encoded_layers (Bool) – whether to output all the encoder layers, default: False.

Returns

output features, shape: [batch_size, sequence_length, ffn_size].

Return type

torch.Tensor

training: bool