Attention (machine learning)
In machine learning, attention is a method that determines the importance of each component in a sequence relative to the other components in that sequence. In natural language processing, importance is represented by "soft" weights assigned to each word in a sentence. More generally, attention encodes vectors called token embeddings across a fixed-width sequence that can range from tens to millions of tokens in size. Unlike "hard" weights, which are computed during the backwards training pass, "soft" weights exist only in the forward pass and therefore change with every step of the input. Earlier designs implemented the attention mechanism in a serial recurrent neural network (RNN)... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
In machine learning, attention is a method that determines the importance of each component in a sequence relative to the other components in that sequence. In natural language processing, importance is represented by "soft" weights assigned to each word in a sentence. More generally, attention encodes vectors called token embeddings across a fixed-width sequence that can range from tens to millions of tokens in size. Unlike "hard" weights, which are computed during the backwards training pass, "soft" weights exist only in the forward pass and therefore change with every step of the input. Earlier designs implemented the attention mechanism in a serial recurrent neural network (RNN) language translation system, but a more recent design, namely the transformer, removed the slower sequential RNN and relied more heavily on the faster parallel attention scheme. Inspired by ideas about attention in humans, the attention mechanism was developed to address the weaknesses of using information from the hidden layers of recurrent neural networks. Recurrent neural networks favor information contained in words at the end of a sentence and thus deemed more recent, thereby tending to attenuate the significance and associated predictive weight assigned to information earlier in the sentence. Attention allows a token equal access to any part of a sentence directly, rather than only through the previous state. ==History== Additional surveys of the attention mechanism in deep learning are provided by Niu et al. and Soydaner. The major breakthrough came with self-attention, where each element in the input sequence attends to all others, enabling the model to capture global dependencies. This idea was central to the Transformer architecture, which replaced recurrence with attention mechanisms. As a result, Transformers became the foundation for models like BERT, T5 and generative pre-trained transformers (GPT). ==Overview== The modern era of machine attention was revitalized by grafting an attention mechanism (Fig 1. orange) to an Encoder-Decoder. |} Figure 2 shows the internal step-by-step operation of the attention block (A) in Fig 1. {{Image frame|content= |width=700|align=center|caption=Figure 2. The diagram shows the attention forward pass calculating correlations of the word "that" with other words in "See that girl run." Given the right weights from training, the network should be able to identify "girl" as a highly correlated word. Some things to note: This example focuses on the attention of a single word "that". In practice, the attention of each word is calculated in parallel to speed up calculations. Simply changing the lowercase "x" vector to the uppercase "X" matrix will yield the formula for this. Softmax scaling / prevents a high variance in that would allow a single word to excessively dominate the softmax resulting in attention to only one word, as a discrete hard max would do. Notation : the commonly written row-wise formula above assumes that vectors are rows, which runs contrary to the standard math notation of column vectors. More correctly, we should take the transpose of the context vector and use the column-wise , resulting in the more correct form \begin{align} (XW_v)^T * {[ (W_k X^T) * { (\underline{x}W_q)^T } ]_{sm} } \end{align} .}} \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V\in\mathbb{R}^{m\times d_v} where {}^T denotes transpose and the softmax function is applied independently to every row of its argument. The matrix Q contains m queries, while matrices K, V jointly contain an unordered set of n key-value pairs. Value vectors in matrix V are weighted using the weights resulting from the softmax operation, so that the rows of the m -by- d_v output matrix are confined to the convex hull of the points in \mathbb{R}^{d_v} given by the rows of V . To understand the permutation invariance and permutation equivariance properties of QKV attention, let A\in\mathbb{R}^{m\times m} and B\in\mathbb{R}^{n\times n} be permutation matrices; and D\in\mathbb{R}^{m\times n} an arbitrary matrix. The softmax function is permutation equivariant in the sense that: \text{softmax}(ADB) = A\,\text{softmax}(D)B By noting that the transpose of a permutation matrix is also its inverse, it follows that: \text{Attention}(AQ, BK, BV) = A\,\text{Attention}(Q, K, V) which shows that QKV attention is equivariant with respect to re-ordering the queries (rows of Q ); and invariant to re-ordering of the key-value pairs in K, V . These properties are inherited when applying linear transforms to the inputs and outputs of QKV attention blocks. For example, a simple self-attention function defined as: X\mapsto\text{Attention}(XT_q, XT_k, XT_v) is permutation equivariant with respect to re-ordering the rows of the input matrix X in a non-trivial way, because every row of the output is a function of all the rows of the input. Similar properties hold for multi-head attention, which is defined below. \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}+M\right)V where the mask, M\in\mathbb{\{0, -\infty\}}^{n\times n} is a strictly upper triangular matrix, with zeros on and below the diagonal and -\infty in every element above the diagonal. The softmax output, in \mathbb{R}^{n\times n} is then lower triangular, with zeros in all elements above the diagonal. The masking ensures that for all 1\le i , row i of the attention output is independent of row j of any of the three input matrices. The permutation invariance and equivariance properties of standard QKV attention do not hold for the masked variant. \text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, ..., \text{head}_h)W^O where each head is computed with QKV attention as: \text{head}_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V) and W_i^Q, W_i^K, W_i^V , and W^O are parameter matrices. The permutation properties of (standard, unmasked) QKV attention apply here also. For permutation matrices, A, B : \text{MultiHead}(AQ, BK, BV) = A\,\text{MultiHead}(Q, K, V) from which we also see that multi-head self-attention: X\mapsto\text{MultiHead}(XT_q, XT_k, XT_v) is equivariant with respect to re-ordering of the rows of input matrix X . \text{Attention}(Q, K, V) = \text{softmax}(\tanh(W_QQ + W_KK))V where W_Q and W_K are learnable weight matrices. \text{Attention}(Q, K, V) = \text{softmax}(QWK^T)V where W is a learnable weight matrix. For encoder self-attention, we can start with a simple encoder without self-attention, such as an "embedding layer", which simply converts each input word into a vector by a fixed lookup table. This gives a sequence of hidden vectors h_0, h_1, \dots . These can then be applied to a dot-product attention mechanism, to obtain \begin{aligned} h_0' &= \mathrm{Attention}(h_0 W^Q, HW^K, H W^V) \\ h_1' &= \mathrm{Attention}(h_1 W^Q, HW^K, H W^V) \\ &\;\,\vdots \end{aligned} or more succinctly, H' = \mathrm{Attention}(H W^Q, HW^K, H W^V) . This can be applied repeatedly, to obtain a multilayered encoder. This is the "encoder self-attention", sometimes called the "all-to-all attention", as the vector at every position can attend to every other. For decoder self-attention, all-to-all attention is inappropriate, because during the autoregressive decoding process, the decoder cannot attend to future outputs that has yet to be decoded. This can be solved by forcing the attention weights w_{ij} = 0 for all i , called "causal masking". This attention mechanism is the "causally masked self-attention".
Cite this
"Attention (machine learning)". 1790283505. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1376558019 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy