Attention mechanism
An attention mechanism is a component of machine learning models that allows a model to assign different levels of importance to different elements of an input when producing an output or representation.
Attention mechanisms are widely used in transformers and have applications in natural language processing, computer vision, speech processing, and other machine-learning tasks.
A simplified representation is:
The attention mechanism allows a model to focus computationally on information that is more relevant to the current prediction or representation.
Attention mechanism components
A standard attention mechanism uses three types of representations:
- queries;
- keys;
- values.
A simplified representation is:
where:
- = query representations;
- = key representations;
- = value representations.
The queries determine what information is being sought, the keys determine how information is matched against the queries, and the values contain the information that is aggregated.
Scaled dot-product attention
A commonly used form of attention is scaled dot-product attention.
It is defined as:
where:
- = query matrix;
- = key matrix;
- = value matrix;
- = dimensionality of the key vectors.
The matrix product produces similarity scores between queries and keys.
The scores are scaled by and passed through the softmax function to produce attention weights.
The resulting weights are applied to the value matrix.
Attention weights
For a particular query, attention weights can be represented as:
where represents the attention weight assigned by query to key-value pair .
The weights satisfy:
and are generally non-negative.
The output corresponding to query can therefore be represented as:
Thus, the output is a weighted combination of the value vectors.
Self-attention
Self-attention is a form of attention in which the queries, keys, and values are derived from the same input sequence.
A simplified representation is:
where:
- = input sequence representation;
- = query projection matrix;
- = key projection matrix;
- = value projection matrix.
Self-attention allows each element of a sequence to incorporate information from other elements.
For example, in natural language processing, a token can assign greater attention to other tokens that provide relevant contextual information.
Cross-attention
Cross-attention is an attention mechanism in which the queries and the keys and values originate from different representations.
A simplified representation is:
followed by:
Cross-attention allows one representation to selectively retrieve information from another representation.
It is used in architectures such as encoder–decoder transformers, where decoder representations can attend to representations produced by an encoder.
Multi-head attention
Multi-head attention applies multiple attention operations in parallel.
For each attention head:
The outputs are concatenated and projected:
where:
- = number of attention heads;
- , , and = learned projection matrices for head ;
- = output projection matrix.
Different attention heads can learn different relationships or patterns within the input.
Causal attention
Causal attention restricts the positions that a token can attend to so that information from future positions is not used when predicting the current token.
For an autoregressive model, a simplified condition is:
where represents the current position and represents an attended position.
This restriction allows the model to predict the next token using only previously available tokens.
Attention masking
Attention masks can be used to control which positions participate in an attention calculation.
A simplified masked attention operation is:
where is an attention mask.
Elements of the mask can be assigned values that prevent particular positions from receiving attention.
Masks can be used for purposes such as causal attention or ignoring padding tokens.
Attention in transformers
Attention is a central component of the transformer architecture.
A simplified transformer attention process is:
The attention mechanism allows transformer models to model relationships between different positions in a sequence without requiring recurrent processing.
Computational complexity
For standard self-attention, a sequence containing positions produces an attention score matrix with dimensions approximately .
Consequently, the number of pairwise attention relationships grows approximately as:
with sequence length.
This quadratic scaling can create computational and memory challenges for very long sequences.
Various attention variants have therefore been developed to reduce the computational requirements of long-context processing.
Applications
Attention mechanisms are used in a wide range of machine-learning applications, including:
- natural language processing;
- machine translation;
- text generation;
- question answering;
- information retrieval;
- speech processing;
- computer vision;
- image generation;
- multimodal learning;
- sequence-to-sequence modeling.
Advantages
Attention mechanisms provide several useful characteristics:
- they allow models to selectively weight information;
- they can capture relationships between distant elements;
- they can be combined with parallel computation;
- they can be adapted to different input modalities;
- they provide a general mechanism for relating one representation to another.
Limitations
Attention mechanisms also have limitations.
Standard self-attention has computational and memory requirements that increase quadratically with sequence length.
Other limitations can include:
- high computational requirements for large inputs;
- substantial memory requirements;
- sensitivity to the number and dimensionality of attention heads;
- difficulty interpreting attention weights as direct explanations of model decisions;
- increased computational cost when processing very long sequences.
The practical characteristics depend on the architecture and implementation.