Skip to content

Attention (machine learning)

Written by GPT-6 Luna from the web 23 of 70 sentences cited

An AI wrote this article, and it can be wrong.

Attention in machine learning is a family of computational mechanisms that lets a model weight and combine parts of its input according to their relevance to a task. It is widely used in neural networks for language, images and other data, and became a central component of the Transformer architecture. The term is a computational analogy to selective attention: a model’s weights help determine which representations contribute to a particular output, but do not imply that it attends in the human cognitive sense.12

The mechanism first gained prominence in neural machine translation, where it helped models use different parts of a source sentence while generating each translated word. In 2017, the Transformer showed that a sequence model could be built around attention without recurrent or convolutional layers. Subsequent work applied attention-based architectures beyond language, while also examining their computational costs and whether visualized attention weights explain a model’s decisions.324

Origins

Early encoder–decoder neural translation systems compressed an entire source sentence into a fixed-length representation, then used that representation to generate a translation. Researchers identified this bottleneck as a possible reason performance deteriorated on longer sentences: the model had to preserve all relevant information in a single vector.3

In a 2014 paper, Dzmitry Bahdanau, Kyunghyun Cho and Yoshua Bengio proposed allowing a translation model to search softly over source-sentence positions as it produced each target word. Rather than relying only on one compressed vector, the model formed a context from the source representations most relevant to the current prediction. The authors reported improved translation performance over a basic encoder–decoder, with gains especially apparent for longer sentences.3

The idea was developed further in subsequent work on machine translation. Minh-Thang Luong, Hieu Pham and Christopher Manning described “global” attention, which considers all source words, and “local” attention, which considers a subset at a time. These approaches illustrate that attention is not one single architecture: designs differ in how they score inputs, which positions they consider and how they combine the selected information.5

Computation

In a common formulation, the model calculates a score between a query and each available key. The scores are normalized—often with a softmax function—to produce weights, and those weights are used to form a weighted sum of corresponding value vectors. Informally, the query represents what the model is seeking, keys provide features to match against, and values carry the information that can be combined.2

The Transformer paper uses scaled dot-product attention, written as softmax(QK⊤/dk)V\mathrm{softmax}(QK^\top/\sqrt{d_k})V, where QQ, KK and VV denote query, key and value matrices, and dkd_k is the key dimension. Dividing the scores by dk\sqrt{d_k} keeps their magnitude in a range suited to the softmax calculation. The resulting weights determine how much each value contributes to each output position.2

In self-attention, queries, keys and values are derived from the same sequence, allowing its positions to exchange information with one another. In cross-attention, queries come from one representation while keys and values come from another; this is useful, for example, when a decoder draws on encoded source information. A causal mask can restrict a position from using later positions, a constraint used in autoregressive generation.2

Variants

Attention mechanisms vary in the way they calculate relevance. Bahdanau and colleagues used a learned scoring function to compare a decoder state with source representations; Transformer attention uses dot products between queries and keys. The choice affects the model’s computation and behavior, and different approaches have been developed for different tasks and architectures.32

Multi-head attention, introduced in the Transformer, runs several attention calculations in parallel using distinct learned projections, then combines their outputs. The design lets the model represent different relationships between positions in separate subspaces rather than relying on one attention calculation. The Transformer also adds positional information because attention by itself does not encode the order of sequence elements.2

Attention can also be soft or hard in how it selects inputs. Soft attention assigns graded weights across multiple positions and combines their representations; hard attention makes a discrete selection, which can require different training methods. The image-captioning model “Show, Attend and Tell,” for example, explored deterministic and stochastic approaches to selecting visual information while generating text.6

Transformers

The 2017 paper “Attention Is All You Need,” by Ashish Vaswani and seven co-authors, introduced the Transformer as a sequence model based solely on attention mechanisms, dispensing with recurrence and convolutions. Its encoder and decoder use stacked attention and feed-forward layers. The authors reported a single-model BLEU score of 41.0 on the WMT 2014 English-to-French translation task, after training for 3.5 days on eight GPUs.2

A central practical difference from recurrent networks is that Transformer attention can calculate interactions across positions in parallel during training, rather than processing a sequence one step at a time. This supports efficient training on accelerators, though it does not remove all sequential work: autoregressive models still generate output tokens in order. The original paper compared its Transformer with recurrent and convolutional sequence models and emphasized its parallelizability and training time.2

The attention mechanism was also adapted to vision. In “An Image Is Worth 16×16 Words,” Alexey Dosovitskiy and colleagues represented an image as a sequence of patches and applied a Transformer to those representations for image classification. Their 2021 paper reported that, when pretrained on large datasets, the Vision Transformer performed well on several image-recognition benchmarks. This showed how the attention-based architecture could be used beyond text sequences.7

Applications

In machine translation, attention allows a decoder to draw on different source words as it produces each target word, rather than relying solely on a single fixed summary. The learned weights can be visualized as approximate alignments between source and translated words. Bahdanau and colleagues reported that these soft alignments often corresponded with intuitive word relationships, though they were model-generated weights rather than manually supplied alignments.3

In image captioning, an attention-based model can combine visual features with a language generator. The 2015 “Show, Attend and Tell” system used image-region representations and learned to emphasize different regions while producing successive words. Its authors evaluated the approach on Flickr8k, Flickr30k and MS COCO captioning benchmarks, and presented visualizations of the model’s changing attention over an image.6

Attention has since been studied across a range of deep-learning applications, including speech recognition, classification, image analysis and graph-based tasks. The specific mechanism depends on the structure of the input: positions might be words, image patches, audio frames or other learned representations. In each case, attention provides a way for a model to form context from selected representations, rather than treating all input features identically at every calculation.8

Limitations

Standard full self-attention has a significant scaling cost: for a sequence of length NN, the attention operation’s time complexity grows quadratically with NN, as it calculates interactions between pairs of positions. The associated memory demands can also become a constraint for long inputs. These costs matter in applications involving long documents or high-resolution images, where inputs may contain many tokens or patches.9

Research on more efficient attention includes methods that optimize hardware use, restrict attention to selected pairs, compress stored keys and values, or reformulate the calculation to achieve linear time complexity. Such approaches make trade-offs in computation, memory use and the interactions a model can represent; they do not constitute one universally superior replacement for standard attention. Efficiency remains an active area of study.9

Attention weights are also sometimes presented as evidence of which inputs mattered most to a model’s prediction. That interpretation is contested. In 2019, Sarthak Jain and Byron Wallace reported that, in their experiments on several natural-language tasks, attention weights were often weakly correlated with gradient-based feature-importance measures and that very different weight patterns could yield equivalent predictions. They cautioned against treating attention visualizations as explanations by themselves.4

Other researchers disputed the breadth of that conclusion. Sarah Wiegreffe and Yuval Pinter argued that the question depends partly on how “explanation” is defined and on the tests used; they proposed alternative diagnostics and reported that attention could provide meaningful interpretation under some of those tests. The debate does not establish that attention weights are always explanations or never useful: it highlights that their interpretive value depends on the model, task and evaluation method.104

Current research

Attention remains a standard design element in Transformer-based models, while research continues to address its computational cost, especially for long contexts. Recent surveys organize efficiency methods into hardware-efficient, sparse, compact and linear approaches. These categories distinguish improvements to implementation from methods that reduce or restructure the interactions calculated by the model.9

The mechanism’s broad adoption does not mean that all attention-based models work alike or that attention alone accounts for their performance. Models combine it with learned representations, position information, feed-forward computation, training data and optimization procedures. The continuing study of efficiency and interpretation reflects two distinct questions: how to make attention-based systems more practical at scale, and how to accurately understand what their internal weights reveal.29

References

10 sources
  1. Attention is not Explanationaclanthology.org· cited 3×

  2. SOCIAL MEDIA TITLE TAGattention-survey.github.io· cited 4×