Attention¶
To solve for symmetric self attention, we begin introducing assymmetry by forming Keys, Queries and Value vectors using their own matrices that are learnt while training
There will be bias parameters too, but we assume they are baked into the matrices themselves, with a single column of 1s added to the input matrix \(X\).
Scaled Attention¶
Typical formulations look as below
The term \(\sqrt{D}\) is based on the variance of the dot product of unit vectors with mean \(0\) and variance \(1\) (independent variables.)
The third part of this equation comes from the independence of the random variables.
Hence, to transform a random variable with Variance \(D\) to have a unit variance, we divide it by \(\sqrt{D}\).
Attention Heads¶
So far we have discussed a single attention heads. However, multiple heads are relevant at the same time. Consider natural language, where one head to relate to vocabulary, another to tense, another to prepositions etc.
We use multiple independent heads, each with its own separate learnable parameters. This is very similar to multiple filters that are used in a single layer of a Convolutional Neural Network (CNN).
Suppose we have \(1, ..., H\) heads
We concatenate these, and linearly trasform to get back the transformed value.
where \(W^{v}\) is also learnable.
\(D_{v} = D/H\) so that \(HD_{v} = D\), which is our output (and input) size.
Note that \(HD_{v}\) here represents the size of our concatenated attention blocks.
Deep Attention Networks¶
Now, we can stack several of these multi-head attention layers on top of one another to get deep networks. We also add residual connections along with layer normalization to improve the training efficiency.
This all is still a linear layer. To add non-linearity, we add a shared fully connected layer (or MLP) across the otutput vectors (i.e., each data point runs through the same neural network).
graph BT;
AN[Add & Normalize];
AN2[Add & Normalize];
Concat[Concat & Linear Transform];
X-->K;
X-->Q;
X-->V;
X-->AN;
AN-->Z;
Concat-->AN;
Z-->MLP;
MLP-->AN2;
AN2-->Y;
Z-->Y;
SDPA-->Concat;
subgraph PH[Multi-Head Attention]
SDPA[Scaled Dot Product Attention];
K-->SDPA;
Q-->SDPA;
V-->SDPA;
end
style MLP fill:#E0F2FE;
style AN fill:#FEF3C7;
style AN2 fill:#FEF3C7;
style PH fill:#FFEDD5;
Compute¶
The total compute cost of the network is approximately
where the first part comes from th edot product in self attention, and the second part comes from the fully connected neural network.
where FCN is the acronym for fully connected network.