Attention Mechanism
The attention mechanism is a computational method that lets a model dynamically allocate weights by importance when processing information, that is, it assigns each position in a sequence a learnable importance score and then aggregates information weighted by those scores. It is one of the most central building blocks of modern deep learning, and Transformer, BERT, GPT and almost all large language models are built on it.
Core Principles
The basic form of attention can be summarized by three elements: query, key and value. The model projects the position currently being processed into a query vector (Query), projects each position to be searched into a key vector (Key) and a value vector (Value), then takes the dot product of the query with the keys to obtain similarity scores, scales and normalizes them into weights, and uses those weights for a weighted sum of the value vectors to obtain the new representation of that position. This process can be understood as a "soft retrieval": rather than hard-selecting only the most similar record, it mixes and retrieves information from all records according to similarity.
In scaled dot-product attention the dot product is divided by the square root of the dimension, because at higher dimensions the variance of the dot product grows with the dimension, and without scaling softmax would enter its saturated region where gradients are close to zero. Multi-head attention, in turn, splits the representation space into several subspaces that perform attention in parallel and then concatenates the results, letting the model attend to different kinds of relations at the same time, for example some heads focusing on nearby words and others on syntactic dependencies.
Compared with recurrent networks, the key advantage of attention is that the path length between any two positions is constant, so long-distance dependencies need no multi-step propagation, which both alleviates vanishing gradients and brings high parallelism.
Key Points
The first point is that computational complexity grows with the square of the sequence length, which is the structural cost of attention. Doubling the sequence length makes the attention matrix four times as large, so long-text modeling often needs variants such as sparse attention, sliding-window attention and low-rank approximation to reduce the overhead.
The second point is that positional information must be injected explicitly. Pure attention is insensitive to input order and gives an unchanged result when the input is permuted, so position encoding is required; from the early sinusoidal encoding and learnable position vectors to rotary position embedding (RoPE) and relative position bias, all of them exist to supply this information.
The third point is that attention weights are not the same thing as interpretability. Attention scores are often used for visualization, but studies have shown that they do not necessarily reflect the model's true causal basis, so treating them as "the focus the model is looking at" requires caution.
The fourth point is the difference between self-attention and cross-attention. In self-attention the query, key and value come from the same sequence and it models relations within the sequence; in cross-attention the keys and values come from another sequence, and it is commonly used in encoder-decoder structures to let the decoding side read information from the source side.
Common Misconceptions
The most common misconception is to believe the attention mechanism possesses genuine "understanding" or "selection" ability, when in reality it is only a weighted average driven by dot-product similarity, with weights shaped by the training objective and not corresponding to human attention. Another is to believe attention can extend context without limit, when in reality memory and compute cost quickly become a bottleneck. One more point is often ignored: in multi-head attention more heads is not always better, since the gain from additional heads saturates and too many heads instead create redundancy.
Application Scenarios
Large language models and machine translation, acoustic modeling in speech recognition, visual attention in images, multimodal image-text alignment, user behavior modeling in recommender systems, protein structure prediction, long-document retrieval and summarization, and code generation and understanding.