flambe.nn

Package Contents

class flambe.nn.Module[source]

Bases: flambe.compile.Component, torch.nn.Module

Base Flambé Module inteface.

Provides the exact same interface as Pytorch’s nn.Module, but extends it with a useful set of methods to access and clip parameters, as well as gradients.

This abstraction allows users to convert their modules with a single line change, by importing from Flambé instead. Just like every Pytorch module, a forward method should be implemented.

named_trainable_params :Iterator[Tuple[str, nn.Parameter]]

Get all the named parameters with requires_grad=True.

Returns:Iterator over the parameters and their name.
Return type:Iterator[Tuple[str, nn.Parameter]]
trainable_params :Iterator[nn.Parameter]

Get all the parameters with requires_grad=True.

Returns:Iterator over the parameters
Return type:Iterator[nn.Parameter]
gradient_norm :float

Compute the average gradient norm.

Returns:The current average gradient norm
Return type:float
parameter_norm :float

Compute the average parameter norm.

Returns:The current average parameter norm
Return type:float
num_parameters(self, trainable=False)

Gets the number of parameters in the model.

Returns:number of model params
Return type:int
clip_params(self, threshold: float)

Clip the parameters to the given range.

Parameters:float – Values are clipped between -threshold, threshold
clip_gradient_norm(self, threshold: float)

Clip the norm of the gradient by the given value.

Parameters:float – Threshold to clip at
class flambe.nn.SoftmaxLayer(input_size: int, output_size: int, mlp_layers: int = 1, mlp_dropout: float = 0.0, mlp_hidden_activation: Optional[nn.Module] = None, take_log: bool = True)[source]

Bases: flambe.nn.module.Module

Implement an SoftmaxLayer module.

Can be used to form a classifier out of any encoder. Note: by default takes the log_softmax so that it can be fed to the NLLLoss module. You can disable this behavior through the take_log argument.

forward(self, data: torch.Tensor)

Performs a forward pass through the network.

Parameters:data (torch.Tensor) – input to the model of shape (*, input_size)
Returns:output – output of the model of shape (*, output_size)
Return type:torch.Tensor
class flambe.nn.MixtureOfSoftmax(input_size: int, output_size: int, k: int = 1, take_log: bool = True)[source]

Bases: flambe.nn.module.Module

Implement the MixtureOfSoftmax output layer.

pi

softmax layer over the different softmax

Type:FullyConnected
layers

list of the k softmax layers

Type:[FullyConnected]
forward(self, data: Tensor)

Implement mixture of softmax for language modeling.

Parameters:data (torch.Tensor) – seq_len x batch_size x hidden_size
Returns:out – output matrix of shape seq_len x batch_size x out_size
Return type:Variable
class flambe.nn.Embeddings(num_embeddings: int, embedding_dim: int, padding_idx: int = 0, max_norm: Optional[float] = None, norm_type: float = 2.0, scale_grad_by_freq: bool = False, sparse: bool = False, positional_encoding: bool = False, positional_learned: bool = False, positonal_max_length: int = 5000)[source]

Bases: flambe.nn.module.Module

Implement an Embeddings module.

This object replicates the usage of nn.Embedding but registers the from_pretrained classmethod to be used inside a Flambé configuration, as this does not happen automatically during the registration of PyTorch objects.

The module also adds optional positional encoding, which can either be sinusoidal or learned during training. For the non-learned positional embeddings, we use sine and cosine functions of different frequencies.

\[ext{PosEncoder}(pos, 2i) = sin(pos/10000^(2i/d_model)) ext{PosEncoder}(pos, 2i+1) = cos(pos/10000^(2i/d_model)) ext{where pos is the word position and i is the embed idx)\]
classmethod from_pretrained(cls, embeddings: Tensor, freeze: bool = True, padding_idx: int = 0, max_norm: Optional[float] = None, norm_type: float = 2.0, scale_grad_by_freq: bool = False, sparse: bool = False, positional_encoding: bool = False, positional_learned: bool = False, positonal_max_length: int = 5000, positonal_embeddings: Optional[Tensor] = None, positonal_freeze: bool = True)

Create an Embeddings instance from pretrained embeddings.

Parameters:
  • embeddings (torch.Tensor) – FloatTensor containing weights for the Embedding. First dimension is being passed to Embedding as num_embeddings, second as embedding_dim.
  • freeze (bool) – If True, the tensor does not get updated in the learning process. Default: True
  • padding_idx (int, optional) – Pads the output with the embedding vector at padding_idx (initialized to zeros) whenever it encounters the index, by default 0
  • max_norm (Optional[float], optional) – If given, each embedding vector with norm larger than max_norm is normalized to have norm max_norm
  • norm_type (float, optional) – The p of the p-norm to compute for the max_norm option. Default 2.
  • scale_grad_by_freq (bool, optional) – If given, this will scale gradients by the inverse of frequency of the words in the mini-batch. Default False.
  • sparse (bool, optional) – If True, gradient w.r.t. weight matrix will be a sparse tensor. See Notes for more details.
  • positional_encoding (bool, optional) – If True, adds positonal encoding to the token embeddings. By default, the embeddings are frozen sinusodial embeddings. To learn these during training, set positional_learned. Default False.
  • positional_learned (bool, optional) – Learns the positional embeddings during training instead of using frozen sinusodial ones. Default False.
  • positonal_embeddings (torch.Tensor, optional) – If given, also replaces the positonal embeddings with this matrix. The max length will be ignored and replaced by the dimension of this matrix.
  • positonal_freeze (bool, optional) – Whether the positonal embeddings should be frozen
forward(self, data: Tensor)

Perform a forward pass.

Parameters:data (Tensor) – The input tensor of shape [S x B]
Returns:The output tensor of shape [S x B x E]
Return type:Tensor
class flambe.nn.Embedder(embedding: Module, encoder: Module, pooling: Optional[Module] = None, embedding_dropout: float = 0, padding_idx: Optional[int] = 0, return_mask: bool = False)[source]

Bases: flambe.nn.module.Module

Implements an Embedder module.

An Embedder takes as input a sequence of index tokens, and computes the corresponding embedded representations, and padding mask. The encoder may be initialized using a pretrained embedding matrix.

embeddings

The embedding module

Type:Module
encoder

The sub-encoder that this object is wrapping

Type:Module
pooling

An optional pooling module

Type:Module
drop

The dropout layer

Type:nn.Dropout
forward(self, data: Tensor)

Performs a forward pass through the network.

Parameters:data (torch.Tensor) – The input data, as a float tensor of shape [S x B]
Returns:Tuple[Tuple[Tensor, Tensor], Tensor] The encoded output, as a float tensor. May return a state if the encoder is an RNN and no pooling is provided. May also return a tuple if return_mask was passed in as a constructor argument.
Return type:Union[Tensor, Tuple[Tensor, Tensor],
class flambe.nn.MLPEncoder(input_size: int, output_size: int, n_layers: int = 1, dropout: float = 0.0, output_activation: Optional[nn.Module] = None, hidden_size: Optional[int] = None, hidden_activation: Optional[nn.Module] = None)[source]

Bases: flambe.nn.module.Module

Implements a multi layer feed forward network.

This module can be used to create output layers, or more complex multi-layer feed forward networks.

seq

the sequence of layers and activations

Type:nn.Sequential
forward(self, data: torch.Tensor)

Performs a forward pass through the network.

Parameters:data (torch.Tensor) – input to the model of shape (batch_size, input_size)
Returns:output – output of the model of shape (batch_size, output_size)
Return type:torch.Tensor
class flambe.nn.RNNEncoder(input_size: int, hidden_size: int, n_layers: int = 1, rnn_type: str = 'lstm', dropout: float = 0, bidirectional: bool = False, layer_norm: bool = False, highway_bias: float = 0, rescale: bool = True, enforce_sorted: bool = False, **kwargs)[source]

Bases: flambe.nn.module.Module

Implements a multi-layer RNN.

This module can be used to create multi-layer RNN models, and provides a way to reduce to output of the RNN to a single hidden state by pooling the encoder states either by taking the maximum, average, or by taking the last hidden state before padding.

Padding is dealt with by using torch’s PackedSequence.

rnn

The rnn submodule

Type:nn.Module
forward(self, data: Tensor, state: Optional[Tensor] = None, padding_mask: Optional[Tensor] = None)

Performs a forward pass through the network.

Parameters:
  • data (Tensor) – The input data, as a float tensor of shape [B x S x E]
  • state (Tensor) – An optional previous state of shape [L x B x H]
  • padding_mask (Tensor, optional) – The padding mask of shape [B x S], dtype should be bool
Returns:

  • Tensor – The encoded output, as a float tensor of shape [B x S x H]
  • Tensor – The encoded state, as a float tensor of shape [L x B x H]

class flambe.nn.PooledRNNEncoder(input_size: int, hidden_size: int, n_layers: int = 1, rnn_type: str = 'lstm', dropout: float = 0, bidirectional: bool = False, layer_norm: bool = False, highway_bias: float = 0, rescale: bool = True, pooling: str = 'last')[source]

Bases: flambe.nn.module.Module

Implement an RNNEncoder with additional pooling.

This class can be used to obtan a single encoded output for an input sequence. It also ignores the state of the RNN.

forward(self, data: Tensor, state: Optional[Tensor] = None, padding_mask: Optional[Tensor] = None)

Perform a forward pass through the network.

Parameters:
  • data (torch.Tensor) – The input data, as a float tensor of shape [B x S x E]
  • state (Tensor) – An optional previous state of shape [L x B x H]
  • padding_mask (Tensor, optional) – The padding mask of shape [B x S]
Returns:

The encoded output, as a float tensor of shape [B x H]

Return type:

torch.Tensor

class flambe.nn.CNNEncoder(input_channels: int, channels: List[int], conv_dim: int = 2, kernel_size: Union[int, List[Union[Tuple[int, ...], int]]] = 3, activation: nn.Module = None, pooling: nn.Module = None, dropout: float = 0, batch_norm: bool = True, stride: int = 1, padding: int = 0)[source]

Bases: flambe.nn.module.Module

Implements a multi-layer n-dimensional CNN.

This module can be used to create multi-layer CNN models.

cnn

The cnn submodule

Type:nn.Module
forward(self, data: Tensor)

Performs a forward pass through the network.

Parameters:data (torch.Tensor) – The input data, as a float tensor
Returns:The encoded output, as a float tensor
Return type:Union[Tensor, Tuple[Tensor, ..]]
class flambe.nn.Sequential(**kwargs: Dict[str, Union[Module, torch.nn.Module]])[source]

Bases: flambe.nn.Module

Implement a Sequential module.

This class can be used in the same way as torch’s nn.Sequential, with the difference that it accepts kwargs arguments.

forward(self, data: torch.Tensor)

Performs a forward pass through the network.

Parameters:data (torch.Tensor) – input to the model
Returns:output – output of the model
Return type:torch.Tensor
class flambe.nn.FirstPooling[source]

Bases: flambe.nn.Module

Get the last hidden state of a sequence.

forward(self, data: torch.Tensor, padding_mask: Optional[torch.Tensor] = None)

Performs a forward pass.

Parameters:
  • data (torch.Tensor) – The input data, as a tensor of shape [B x S x H]
  • padding_mask (torch.Tensor) – The input mask, as a tensor of shape [B X S]
Returns:

The output data, as a tensor of shape [B x H]

Return type:

torch.Tensor

class flambe.nn.LastPooling[source]

Bases: flambe.nn.Module

Get the last hidden state of a sequence.

forward(self, data: torch.Tensor, padding_mask: Optional[torch.Tensor] = None)

Performs a forward pass.

Parameters:
  • data (torch.Tensor) – The input data, as a tensor of shape [B x S x H]
  • padding_mask (torch.Tensor) – The input mask, as a tensor of shape [B X S]
Returns:

The output data, as a tensor of shape [B x H]

Return type:

torch.Tensor

class flambe.nn.SumPooling[source]

Bases: flambe.nn.Module

Get the sum of the hidden state of a sequence.

forward(self, data: torch.Tensor, padding_mask: Optional[torch.Tensor] = None)

Performs a forward pass.

Parameters:
  • data (torch.Tensor) – The input data, as a tensor of shape [B x S x H]
  • padding_mask (torch.Tensor) – The input mask, as a tensor of shape [B X S]
Returns:

The output data, as a tensor of shape [B x H]

Return type:

torch.Tensor

class flambe.nn.AvgPooling[source]

Bases: flambe.nn.Module

Get the average of the hidden state of a sequence.

forward(self, data: torch.Tensor, padding_mask: Optional[torch.Tensor] = None)

Performs a forward pass.

Parameters:
  • data (torch.Tensor) – The input data, as a tensor of shape [B x S x H]
  • padding_mask (torch.Tensor) – The input mask, as a tensor of shape [B X S]
Returns:

The output data, as a tensor of shape [B x H]

Return type:

torch.Tensor

class flambe.nn.StructuredSelfAttentivePooling(input_size: int, attention_heads: int = 16, attention_units: Sequence[int] = (300, ), output_activation: Optional[torch.nn.Module] = None, hidden_activation: Optional[torch.nn.Module] = None, is_biased: bool = False, input_dropout: float = 0.0, attention_dropout: float = 0.0)[source]

Bases: flambe.nn.Module

Structured Self Attentive Pooling.

_compute_attention(self, data: torch.Tensor, mask: torch.Tensor)

Computes the attention

Parameters:
  • data (torch.Tensor) – The input data, as a tensor of shape [B x S x H]
  • mask (torch.Tensor) – The input mask, as a tensor of shape [B X S]
Returns:

The attention, as a tensor of shape [B x S x HEADS]

Return type:

torch.Tensor

forward(self, data: torch.Tensor, mask: Optional[torch.Tensor] = None)

Performs a forward pass.

Parameters:
  • data (torch.Tensor) – The input data, as a tensor of shape [B x S x H]
  • mask (torch.Tensor) – The input mask, as a tensor of shape [B X S]
Returns:

The output data, as a tensor of shape [B x H]

Return type:

torch.Tensor

class flambe.nn.GeneralizedPooling(input_size: int, attention_units: Sequence[int] = (300, ), output_activation: Optional[torch.nn.Module] = None, hidden_activation: Optional[torch.nn.Module] = None, is_biased: bool = True, input_dropout: float = 0.0, attention_dropout: float = 0.0)[source]

Bases: flambe.nn.pooling.StructuredSelfAttentivePooling

Self attention pooling.

forward(self, data: torch.Tensor, mask: Optional[torch.Tensor] = None)

Performs a forward pass.

Parameters:
  • data (torch.Tensor) – The input data, as a tensor of shape [B x S x H]
  • mask (torch.Tensor) – The input mask, as a tensor of shape [B X S]
Returns:

The output data, as a tensor of shape [B x H]

Return type:

torch.Tensor

class flambe.nn.Transformer(input_size, d_model: int = 512, nhead: int = 8, num_encoder_layers: int = 6, num_decoder_layers: int = 6, dim_feedforward: int = 2048, dropout: float = 0.1)[source]

Bases: flambe.nn.Module

A Transformer model

User is able to modify the attributes as needed. The architechture is based on the paper “Attention Is All You Need”. Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information Processing Systems, pages 6000-6010.

forward(self, src: torch.Tensor, tgt: torch.Tensor, src_mask: Optional[torch.Tensor] = None, tgt_mask: Optional[torch.Tensor] = None, memory_mask: Optional[torch.Tensor] = None, src_key_padding_mask: Optional[torch.Tensor] = None, tgt_key_padding_mask: Optional[torch.Tensor] = None, memory_key_padding_mask: Optional[torch.Tensor] = None)

Take in and process masked source/target sequences.

Parameters:
  • src (torch.Tensor) – the sequence to the encoder (required). shape: \((N, S, E)\).
  • tgt (torch.Tensor) – the sequence to the decoder (required). shape: \((N, T, E)\).
  • src_mask (torch.Tensor, optional) – the additive mask for the src sequence (optional). shape: \((S, S)\).
  • tgt_mask (torch.Tensor, optional) – the additive mask for the tgt sequence (optional). shape: \((T, T)\).
  • memory_mask (torch.Tensor, optional) – the additive mask for the encoder output (optional). shape: \((T, S)\).
  • src_key_padding_mask (torch.Tensor, optional) – the ByteTensor mask for src keys per batch (optional). shape: \((N, S)\)
  • tgt_key_padding_mask (torch.Tensor, optional) – the ByteTensor mask for tgt keys per batch (optional). shape: \((N, T)\).
  • memory_key_padding_mask (torch.Tensor, optional) – the ByteTensor mask for memory keys per batch (optional). shape” \((N, S)\).
Returns:

  • output (torch.Tensor) – The output sequence, shape: \((N, T, E)\).

  • Note ([src/tgt/memory]_mask should be filled with) – float(‘-inf’) for the masked positions and float(0.0) else. These masks ensure that predictions for position i depend only on the unmasked positions j and are applied identically for each sequence in a batch. [src/tgt/memory]_key_padding_mask should be a ByteTensor where False values are positions that should be masked with float(‘-inf’) and True values will be unchanged. This mask ensures that no information will be taken from position i if it is masked, and has a separate mask for each sequence in a batch.

  • Note (Due to the multi-head attention architecture in the) – transformer model, the output sequence length of a transformer is same as the input sequence (i.e. target) length of the decode.

    where S is the source sequence length, T is the target sequence length, N is the batchsize, E is the feature number

class flambe.nn.TransformerEncoder(input_size: int = 512, d_model: int = 512, nhead: int = 8, num_layers: int = 6, dim_feedforward: int = 2048, dropout: float = 0.1)[source]

Bases: flambe.nn.Module

TransformerEncoder is a stack of N encoder layers.

forward(self, src: torch.Tensor, memory: Optional[torch.Tensor] = None, mask: Optional[torch.Tensor] = None, padding_mask: Optional[torch.Tensor] = None)

Pass the input through the endocder layers in turn.

Parameters:
  • src (torch.Tensor) – The sequence to the encoder (required).
  • memory (torch.Tensor, optional) – Optional memory, unused by default.
  • mask (torch.Tensor, optional) – The mask for the src sequence (optional).
  • padding_mask (torch.Tensor, optional) – The mask for the src keys per batch (optional). Should be True for tokens to leave untouched, and False for padding tokens.
_reset_parameters(self)

Initiate parameters in the transformer model.

class flambe.nn.TransformerDecoder(input_size: int, d_model: int, nhead: int, num_layers: int, dim_feedforward: int = 2048, dropout: float = 0.1)[source]

Bases: flambe.nn.Module

TransformerDecoder is a stack of N decoder layers

forward(self, tgt: torch.Tensor, memory: torch.Tensor, tgt_mask: Optional[torch.Tensor] = None, memory_mask: Optional[torch.Tensor] = None, padding_mask: Optional[torch.Tensor] = None, memory_key_padding_mask: Optional[torch.Tensor] = None)

Pass the inputs (and mask) through the decoder layer in turn.

Parameters:
  • tgt (torch.Tensor) – The sequence to the decoder (required).
  • memory (torch.Tensor) – The sequence from the last layer of the encoder (required).
  • tgt_mask (torch.Tensor, optional) – The mask for the tgt sequence (optional).
  • memory_mask (torch.Tensor, optional) – The mask for the memory sequence (optional).
  • padding_mask (torch.Tensor, optional) – The mask for the tgt keys per batch (optional). Should be True for tokens to leave untouched, and False for padding tokens.
  • memory_key_padding_mask (torch.Tensor, optional) – The mask for the memory keys per batch (optional).
Returns:

Return type:

torch.Tensor

_reset_parameters(self)

Initiate parameters in the transformer model.

class flambe.nn.TransformerSRU(input_size: int = 512, d_model: int = 512, nhead: int = 8, num_encoder_layers: int = 6, num_decoder_layers: int = 6, dim_feedforward: int = 2048, dropout: float = 0.1, sru_dropout: Optional[float] = None, bidrectional: bool = False, **kwargs: Dict[str, Any])[source]

Bases: flambe.nn.Module

A Transformer with an SRU replacing the FFN.

forward(self, src: torch.Tensor, tgt: torch.Tensor, src_mask: Optional[torch.Tensor] = None, tgt_mask: Optional[torch.Tensor] = None, memory_mask: Optional[torch.Tensor] = None, src_key_padding_mask: Optional[torch.Tensor] = None, tgt_key_padding_mask: Optional[torch.Tensor] = None, memory_key_padding_mask: Optional[torch.Tensor] = None)

Take in and process masked source/target sequences.

Parameters:
  • src (torch.Tensor) – the sequence to the encoder (required). shape: \((N, S, E)\).
  • tgt (torch.Tensor) – the sequence to the decoder (required). shape: \((N, T, E)\).
  • src_mask (torch.Tensor, optional) – the additive mask for the src sequence (optional). shape: \((S, S)\).
  • tgt_mask (torch.Tensor, optional) – the additive mask for the tgt sequence (optional). shape: \((T, T)\).
  • memory_mask (torch.Tensor, optional) – the additive mask for the encoder output (optional). shape: \((T, S)\).
  • src_key_padding_mask (torch.Tensor, optional) – the ByteTensor mask for src keys per batch (optional). shape: \((N, S)\).
  • tgt_key_padding_mask (torch.Tensor, optional) – the ByteTensor mask for tgt keys per batch (optional). shape: \((N, T)\).
  • memory_key_padding_mask (torch.Tensor, optional) – the ByteTensor mask for memory keys per batch (optional). shape” \((N, S)\).
Returns:

  • output (torch.Tensor) – The output sequence, shape: \((T, N, E)\).

  • Note ([src/tgt/memory]_mask should be filled with) – float(‘-inf’) for the masked positions and float(0.0) else. These masks ensure that predictions for position i depend only on the unmasked positions j and are applied identically for each sequence in a batch. [src/tgt/memory]_key_padding_mask should be a ByteTensor where False values are positions that should be masked with float(‘-inf’) and True values will be unchanged. This mask ensures that no information will be taken from position i if it is masked, and has a separate mask for each sequence in a batch.

  • Note (Due to the multi-head attention architecture in the) – transformer model, the output sequence length of a transformer is same as the input sequence (i.e. target) length of the decode.

    where S is the source sequence length, T is the target sequence length, N is the batchsize, E is the feature number

class flambe.nn.TransformerSRUEncoder(input_size: int = 512, d_model: int = 512, nhead: int = 8, num_layers: int = 6, dim_feedforward: int = 2048, dropout: float = 0.1, sru_dropout: Optional[float] = None, bidirectional: bool = False, **kwargs: Dict[str, Any])[source]

Bases: flambe.nn.Module

A TransformerSRUEncoder with an SRU replacing the FFN.

forward(self, src: torch.Tensor, state: Optional[torch.Tensor] = None, mask: Optional[torch.Tensor] = None, padding_mask: Optional[torch.Tensor] = None)

Pass the input through the endocder layers in turn.

Parameters:
  • src (torch.Tensor) – The sequnce to the encoder (required).
  • state (Optional[torch.Tensor]) – Optional state from previous sequence encoding. Only passed to the SRU (not used to perform multihead attention).
  • mask (torch.Tensor, optional) – The mask for the src sequence (optional).
  • padding_mask (torch.Tensor, optional) – The mask for the src keys per batch (optional). Should be True for tokens to leave untouched, and False for padding tokens.
_reset_parameters(self)

Initiate parameters in the transformer model.

class flambe.nn.TransformerSRUDecoder(input_size: int = 512, d_model: int = 512, nhead: int = 8, num_layers: int = 6, dim_feedforward: int = 2048, dropout: float = 0.1, sru_dropout: Optional[float] = None, **kwargs: Dict[str, Any])[source]

Bases: flambe.nn.Module

A TransformerSRUDecoderwith an SRU replacing the FFN.

forward(self, tgt: torch.Tensor, memory: torch.Tensor, state: Optional[torch.Tensor] = None, tgt_mask: Optional[torch.Tensor] = None, memory_mask: Optional[torch.Tensor] = None, padding_mask: Optional[torch.Tensor] = None, memory_key_padding_mask: Optional[torch.Tensor] = None)

Pass the inputs (and mask) through the decoder layer in turn.

Parameters:
  • tgt (torch.Tensor) – The sequence to the decoder (required).
  • memory (torch.Tensor) – The sequence from the last layer of the encoder (required).
  • state (Optional[torch.Tensor]) – Optional state from previous sequence encoding. Only passed to the SRU (not used to perform multihead attention).
  • tgt_mask (torch.Tensor, optional) – The mask for the tgt sequence (optional).
  • memory_mask (torch.Tensor, optional) – The mask for the memory sequence (optional).
  • padding_mask (torch.Tensor, optional) – The mask for the tgt keys per batch (optional). Should be True for tokens to leave untouched, and False for padding tokens.
  • memory_key_padding_mask (torch.Tensor, optional) – The mask for the memory keys per batch (optional).
Returns:

Return type:

torch.Tensor

_reset_parameters(self)

Initiate parameters in the transformer model.