mlpack
Public Member Functions | List of all members
mlpack::ann::MultiheadAttention< InputDataType, OutputDataType, RegularizerType > Class Template Reference

Multihead Attention allows the model to jointly attend to information from different representation subspaces at different positions. More...

#include <multihead_attention.hpp>

Public Member Functions

 MultiheadAttention ()
 Default constructor.
 
 MultiheadAttention (const size_t tgtSeqLen, const size_t srcSeqLen, const size_t embedDim, const size_t numHeads)
 Create the MultiheadAttention object using the specified modules. More...
 
void Reset ()
 Reset the layer parameters.
 
template<typename eT >
void Forward (const arma::Mat< eT > &input, arma::Mat< eT > &output)
 Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activity forward through f. More...
 
template<typename eT >
void Backward (const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
 Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backwards trough f. More...
 
template<typename eT >
void Gradient (const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &gradient)
 Calculate the gradient using the output delta and the input activation. More...
 
size_t WeightSize () const
 Get the size of the weights.
 
template<typename Archive >
void serialize (Archive &ar, const uint32_t)
 Serialize the layer.
 
size_t TgtSeqLen () const
 Get the target sequence length.
 
size_t & TgtSeqLen ()
 Modify the target sequence length.
 
size_t SrcSeqLen () const
 Get the source sequence length.
 
size_t & SrcSeqLen ()
 Modify the source sequence length.
 
size_t EmbedDim () const
 Get the embedding dimension.
 
size_t & EmbedDim ()
 Modify the embedding dimension.
 
size_t NumHeads () const
 Get the number of attention heads.
 
size_t & NumHeads ()
 Modify the number of attention heads.
 
OutputDataType const & AttentionMask () const
 Get the two dimensional Attention Mask.
 
OutputDataType & AttentionMask ()
 Modify the two dimensional Attention Mask.
 
OutputDataType const & KeyPaddingMask () const
 Get Key Padding Mask.
 
OutputDataType & KeyPaddingMask ()
 Modify the Key Padding Mask.
 
OutputDataType const & OutputParameter () const
 Get the output parameter.
 
OutputDataType & OutputParameter ()
 Modify the output parameter.
 
OutputDataType const & Delta () const
 Get the delta.
 
OutputDataType & Delta ()
 Modify the delta.
 
OutputDataType const & Gradient () const
 Get the gradient.
 
OutputDataType & Gradient ()
 Modify the gradient.
 
OutputDataType const & Parameters () const
 Get the parameters.
 
OutputDataType & Parameters ()
 Modify the parameters.
 
size_t InputShape () const
 

Detailed Description

template<typename InputDataType = arma::mat, typename OutputDataType = arma::mat, typename RegularizerType = NoRegularizer>
class mlpack::ann::MultiheadAttention< InputDataType, OutputDataType, RegularizerType >

Multihead Attention allows the model to jointly attend to information from different representation subspaces at different positions.

With a single attention head, averaging inhibits this. [arxiv.org:1706.03762v5]

The MultiheadAttention class takes concatenated form of query, key and value. The query, key and value are concatenated into single matrix and fed to the Forward function as input.

The query, key and value are matrices of shapes (embedDim * tgtSeqLen, batchSize), (embedDim * srcSeqLen, batchSize) and (embedDim * srcSeqLen, batchSize) respectively. The output is a matrix of shape (embedDim * tgtSeqLen, batchSize). The embeddings are stored consequently.

Template Parameters
InputDataTypeType of the input data (arma::colvec, arma::mat, arma::sp_mat or arma::cube).
OutputDataTypeType of the output data (arma::colvec, arma::mat, arma::sp_mat or arma::cube).
RegularizerTypeType of the regularizer to be used.

Constructor & Destructor Documentation

◆ MultiheadAttention()

template<typename InputDataType , typename OutputDataType , typename RegularizerType >
mlpack::ann::MultiheadAttention< InputDataType, OutputDataType, RegularizerType >::MultiheadAttention ( const size_t  tgtSeqLen,
const size_t  srcSeqLen,
const size_t  embedDim,
const size_t  numHeads 
)

Create the MultiheadAttention object using the specified modules.

Parameters
tgtSeqLenTarget sequence length.
srcSeqLenSource sequence length.
embedDimTotal dimension of the model.
numHeadsNumber of parallel attention heads.

Member Function Documentation

◆ Backward()

template<typename InputDataType , typename OutputDataType , typename RegularizerType >
template<typename eT >
void mlpack::ann::MultiheadAttention< InputDataType, OutputDataType, RegularizerType >::Backward ( const arma::Mat< eT > &  ,
const arma::Mat< eT > &  gy,
arma::Mat< eT > &  g 
)

Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backwards trough f.

Using the results from the feed forward pass.

Parameters
gyThe backpropagated error.
gThe calculated gradient.

◆ Forward()

template<typename InputDataType , typename OutputDataType , typename RegularizerType >
template<typename eT >
void mlpack::ann::MultiheadAttention< InputDataType, OutputDataType, RegularizerType >::Forward ( const arma::Mat< eT > &  input,
arma::Mat< eT > &  output 
)

Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activity forward through f.

Parameters
inputThe query matrix.
outputResulting output activation.

◆ Gradient()

template<typename InputDataType , typename OutputDataType , typename RegularizerType >
template<typename eT >
void mlpack::ann::MultiheadAttention< InputDataType, OutputDataType, RegularizerType >::Gradient ( const arma::Mat< eT > &  input,
const arma::Mat< eT > &  error,
arma::Mat< eT > &  gradient 
)

Calculate the gradient using the output delta and the input activation.

Parameters
inputThe input data used for evaluating specified function.
errorThe calculated error.
gradientThe calculated gradient.

The documentation for this class was generated from the following files: