Attention (machine learning)
Machine learning technique
In machine learning, attention is a method that determines the importance of, or "attends to", each component in a sequence relative to the other components in that sequence. In natural language processing, importance is represented by 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.
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, which has largely replaced RNN in frontier models since BERT, T5 and GPT.
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
Academic reviews of the history of the attention mechanism are provided in Niu et al. and Soydaner.
Predecessors
Selective attention in humans had been well studied in neuroscience and cognitive psychology. In 1953, Colin Cherry studied selective attention in the context of audition, known as the cocktail party effect.
In 1958, Donald Broadbent proposed the filter model of attention. Selective attention of vision was studied in the 1960s by George Sperling's partial report paradigm. It was also noticed that saccade control is modulated by cognitive processes, insofar as the eye moves preferentially towards areas of high salience. As the fovea of the eye is small, the eye cannot sharply resolve the entire visual field at once. The use of saccade control allows the eye to quickly scan important features of a scene.
These research developments inspired algorithms such as the Neocognitron and its variants. Meanwhile, developments in neural networks had inspired circuit models of biological visual attention. One well-cited network from 1998, for example, was inspired by the low-level primate visual system. It produced saliency maps of images using handcrafted (not learned) features, which were then used to guide a second neural network in processing patches of the image in order of reducing saliency.
A key aspect of attention mechanism can be written (schematically) as where the angled brackets denote dot product. This shows that it involves a multiplicative operation. Multiplicative operations within artificial neural networks had been studied under the names of higher-order neural networks, multiplication units, sigma-pi units, fast weight controllers, and hyper-networks.
In fast weight controller, one of its two networks has "fast weights" or "dynamic links" (1981). A slow neural network learns by gradient descent to generate keys and values for computing the weight changes of the fast neural network which computes answers to queries. This is dot-product attention without softmax.
Recurrent attention
During the deep learning era, attention mechanism was developed to solve similar problems in encoding-decoding.
In machine translation, the seq2seq model, as it was proposed in 2014, would encode an input text into a fixed-length vector, which would then be decoded into an output text. If the input text is long, the fixed-length vector would be unable to carry enough information for accurate decoding. An attention mechanism was proposed to solve this problem.
An image captioning model was proposed in 2015, citing inspiration from the seq2seq model. that would encode an input image into a fixed-length vector. (Xu et al 2015), citing (Bahdanau et al 2014), applied the attention mechanism as used in the seq2seq model to image captioning.
Transformer
One problem with seq2seq models was their use of recurrent neural networks, which are not parallelizable as both the encoder and the decoder must process the sequence token-by-token. Decomposable attention attempted to solve this problem by processing the input sequence in parallel, before computing a "soft alignment matrix" (alignment is the terminology used by Bahdanau et al) in order to allow for parallel processing.
The idea of using the attention mechanism for self-attention, instead of in an encoder-decoder (cross-attention), was also proposed during this period, such as in differentiable neural computers and neural Turing machines. It was termed intra-attention where an LSTM is augmented with a memory network as it encodes an input sequence.
These strands of development were brought together in 2017 with the Transformer architecture, published in the Attention Is All You Need paper.
Dot-product attention
Given three matrices: and , the scaled dot-product attention is defined as:where denotes transpose and the softmax function is applied independently to every row of its argument. is a scalar. It is usually set to .
The matrix contains queries, while matrices jointly contain key-value pairs. Value vectors in matrix are weighted using the weights resulting from the softmax operation, so that the rows of the -by- output matrix are confined to the convex hull of the points in given by the rows of .
The attention mechanism without positional encoding is permutation-invariant.
Masking
For decoder self-attention, all-to-all attention is inappropriate, because during the autoregressive decoding process, the decoder cannot attend to future outputs that have yet to be decoded. This can be solved by forcing the attention weights for all , called "causal masking". This attention mechanism is the "causally masked self-attention".
Formally,whereThe effect of adding is to zero out the corresponding attention weight, after applying the softmax.
Other forms of masking are possible, to enforce different types of attention weight patterns.
Multi-head
Several dot-product attention mechanisms can be combined, resulting in the Multi-Head Attention (MHA):where each head is computed with QKV attention as:and , and are learnable matrices.
Self-attention
Self-attention is essentially the same as cross-attention, except that query, key, and value vectors all come from the same model. Both encoder and decoder can use self-attention, but with subtle differences.
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 . These can then be applied to a dot-product attention mechanism, to obtainor more succinctly, . 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.
Historical variants
Lernmatrix, or correlation matrix memory, or linear attention, is dot-product attention, with causal masking, without softmax.
Bahdanau et al (2014) proposed an additive form of attentionwhere are learnable matrices, and is a learnable vector.
Luong et al (2015) attempted to improve upon Bahdanau et al (2014). They tested at least two different forms of attentions, two of which are the modern form of dot-product attention. They worked on seq2seq machine translation. A multilayered LSTM processes an input sequence (such as a sequence of words in an English sequence) into a sequence of vectors . It then receives a special token <EOS> ("end of sequence") to mark as , the beginning of the output sequence. It then proceeds as follows:
- Given another output , run the LSTM for another step, obtaining output .
- pays attention to input key-value pairs. The key and value vectors are the same: .
- The attention scores are calculated as either , as in modern dot-product attention, or as , as in Bahdanau et al (2014).
- The output vector is .
- The output word is sampled from .
Linear attention
Consider a sequence of length . The dot-product softmax attention mechanism costs to process the entire sequence. After that, the attention mechanism needs another to process each new token. Consequently, many variants have been proposed to save the cost.
Correlation matrix memory
Removing the softmax results in the Lernmatrix, or correlation matrix memory. In the standard causally masked dot-product attention mechanism, the input sequence of vectors are converted to a triple sequence of query-key-value vectors via matrices , and the t-th output vector is .
A naive linear attention mechanism is to take off the softmax: . The thing in the brackets is a memory matrix , such that , and the memory updates via .
More generally, memory can decay via , where are parameters that may depend on themselves.
Kernel method
The main difference between correlation matrix memory and dot-product attention is that neither the exponential sum nor the exponentially-weighted sum is a matrix product. This can be converted to a correlation matrix memory by using the kernel method.
Specifically, define the function . It is a kernel function, so it can be reproduced as the inner product in a Hilbert space (the Reproducing Kernel Hilbert Space) . Let be the feature function, then , and both sums factor:Since both are iteratively updated, computing a new token takes time and space, and computing the full length- sequence takes time and space.
The main issue is that has infinitely many dimensions. Nevertheless, it can be approximated in finitely many dimensions. That is, there exists for some such that . Since is just a scaled version of the RBF kernel, and the RBF kernel has been studied extensively, there are many approximations to choose from.
Random Fourier features
A particular choice uses random Fourier features of the RBF, which uses the exact identity where is the normal distribution. Consequently, by taking finitely many samples of , we haveRandom Fourier features may have negative entries, but the RBF kernel is always positive.
Performer
Performer (2020) improves on it by exploiting two other identities:These give two random feature maps. In either one, all features are positive. It also samples by orthogonal random features, a low-discrepancy method. They are first independently sampled from the normal distribution , then orthogonalized by Gram–Schmidt process, then scaled to a length according to the chi distribution . If , then this process is repeated for times, generating up to vectors at each generation until vectors are generated.
Other matrix memories
In DeltaNet, a form of forgetting is implemented, by subtraction. Beginning with the memory matrix update , forgetting is implemented by the following modification:where measures how much the memory matrix updates. This delta-update rule satisfies the following properties:
- For any value of , and any perpendicular to , .
- When , .
- When , , which satisfies when is a unit vector.
In DeltaNet, is computed by a linear-sigmoid layer. That is, , where is a learnable weight vector, is a learnable bias, and is a sigmoid activation function, usually the logistic function.
Mamba uses the decaying update , with scalar , and output . The scalar is designed to be a linear-softplus function of input . That is, where is a learned vector, and is a learned scalar. The activation function is picked to be softplus, because must be positive. The scalar is designed to be , where is a learned positive scalar.
More generally, can both be diagonal matrices. A diagonal allows different coordinates in the previous value vectors to be decayed away at different rates, and a diagonal allows different coordinates in the new value vector to be remembered at different rates.
RetNet implements a form of relative positional encoding by positional update with a matrix form in . When is constant multiple of identity, it is an exponentially decaying relative positional encoding. When is a constant rotational matrix, it is a form of rotary positional encoding.
Kimi Delta Attention combines Mamba with Deltawhere the decay-rate is a diagonal matrix, and the memory update rate is a scalar, calculated by:where are learned matrices of the appropriate sizes, and is a learned positive scalar. The functions (the sigmoid function), exponent, and softplus are applied componentwise.
Optimizations
Flash attention
The size of the attention matrix is proportional to the square of the number of input tokens. Therefore, when the input is long, calculating the attention matrix requires a lot of GPU memory. Flash attention is an implementation that reduces the memory needs and increases efficiency without sacrificing accuracy. It achieves this by partitioning the attention computation into smaller blocks that fit into the GPU's faster on-chip memory, reducing the need to store large intermediate matrices and thus lowering memory usage while increasing computational efficiency.
KV cache
Applications
Attention is widely used in natural language processing, computer vision, and speech recognition. In natural language processing, it improves context understanding in tasks like question answering and summarization. In vision, visual attention helps models focus on relevant image regions, enhancing object detection and image captioning.
Attention maps as explanations for vision transformers
From the original paper on vision transformers (ViT), visualizing attention scores as a heat map (called saliency maps or attention maps) has become an important and routine way to inspect the decision making process of ViT models. One can compute the attention maps with respect to any attention head at any layer, while the deeper layers tend to show more semantically meaningful visualization. Attention rollout is a recursive algorithm to combine attention scores across all layers, by computing the dot product of successive attention maps.
Because vision transformers are typically trained in a self-supervised manner, attention maps are generally not class-sensitive. When a classification head is attached to the ViT backbone, class-discriminative attention maps (CDAM) combines attention maps and gradients with respect to the class [CLS] token. Some class-sensitive interpretability methods originally developed for convolutional neural networks can be also applied to ViT, such as GradCAM, which back-propagates the gradients to the outputs of the final attention layer.
Using attention as basis of explanation for the transformers in language and vision is not without debate. While some pioneering papers analyzed and framed attention scores as explanations, higher attention scores do not always correlate with greater impact on model performances.
Adapted from the Wikipedia article, CC BY-SA 4.0. Continue on Wikipedia
Go deeper
Ideas this article links to.
- Symbolic artificial intelligenceMethods in artificial intelligence research
- AI data centerSpecialized data centers designed for artificial intelligence
- T5 (language model)Series of large language models developed by Google AI
- Knowledge representation and reasoningField of artificial intelligence
- Statistical learning theoryFramework for machine learning