mlpack
|
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 |
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.
InputDataType | Type of the input data (arma::colvec, arma::mat, arma::sp_mat or arma::cube). |
OutputDataType | Type of the output data (arma::colvec, arma::mat, arma::sp_mat or arma::cube). |
RegularizerType | Type of the regularizer to be used. |
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.
tgtSeqLen | Target sequence length. |
srcSeqLen | Source sequence length. |
embedDim | Total dimension of the model. |
numHeads | Number of parallel attention heads. |
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.
gy | The backpropagated error. |
g | The calculated gradient. |
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.
input | The query matrix. |
output | Resulting output activation. |
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.
input | The input data used for evaluating specified function. |
error | The calculated error. |
gradient | The calculated gradient. |