Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
In machine learning, attention is a method that determines the importance of each component in a sequence relative to others. Unlike static weights computed during training, attention weights are "soft"—they are computed dynamically in the forward pass and change with every input sequence.
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 (RNNs). Recurrent neural networks favor information contained in words at the end of a sentence (recency bias), thereby tending to attenuate the significance of information earlier in the sentence.
Attention allows a token equal access to any part of a sequence directly, rather than only bottlenecking through successive hidden states. By shifting from recurrence to parallel self-attention, the Transformer model solved key training performance limitations, making it the foundation for models like BERT, T5, and GPT.

Figure: The query vector q attends over the key matrix K to calculate soft similarity weights α, combining value matrix V into a context vector.
Observe how alignment matching maps words dynamically between languages during decoding (e.g., translating "I love you" to French "je t'aime").
On the first pass of the decoder, 94% of the attention weight is focused on the source word 'I'. This guides the model to produce the translation 'je'.

Alignment Matrix: High attention weights (dark cells) reveal cross-lingual mapping.
Click on any part of the mathematical formula below to view its dimensions, purpose, and role in computing self-attention.
For queries matrix Q ∈ ℝ^(m×d_k), keys matrix K ∈ ℝ^(n×d_k) and values matrix V ∈ ℝ^(n×d_v), the scores represent similarity projections. Equivariant to queries re-ordering and invariant to key-value shuffling.
Attention(Q, K, V) = softmax( QKᵀ / √d_k ) · V ∈ ℝ^(m×d_v)

Both encoder and decoder states are needed to compute attention weights. Score defined by: w_ij = x_i · h_j

Queries are projected from decoder states, while keys and values are projected from encoder outputs.[44]

Decoders are omitted entirely. Queries, keys, and values are computed strictly from the encoder input states (self-attention).[46]
No decoder used. With only 1 sequence input, the weight matrix is calculated via an auto-correlation: w_ij = x_i · x_j.[45]
Computes attention scores using parameterized feedforward linear layers instead of raw dot-product similarity metrics.[47]
Standard attention has quadratic time/memory complexity O(N²). FlashAttention partitions computations into blocks fitting inside faster GPU SRAM, avoiding the storage of massive intermediate matrices.[48]
A PyTorch-native flexible API developed by Meta allowing users to customize and modify attention score values before softmax while maintaining FlashAttention execution speed.[49]
Visualizing attention weights as saliency heatmaps is standard practice to inspect decision making in Vision Transformers (ViT). Since models are often trained self-supervised, maps are not native class-sensitive.
Development of the Cocktail Party Effect (focusing on target auditory stimuli while filtering out background noise) and the filter model of attention.
Early models of fast weight controllers and dynamic links between neurons, anticipating key-value routing mechanisms.
Bahdanau et al. introduced learned attention to handle translation of long sentences in sequence-to-sequence recurrent neural networks.
The landmark paper 'Attention Is All You Need' formalized scaled dot-product self-attention: A = softmax(QKᵀ / √d_k)V, discarding recurrence entirely.
Linformer, Reformer, FlashAttention, and FlexAttention introduced to reduce quadratic overhead and optimize GPU execution for long context windows.