Attention Architecture is a foundational concept in modern deep learning, especially in natural language processing (NLP) and vision tasks. At its core, attention allows a model to weight different parts of the input data differently, depending on their relevance to the current task.
The most prominent use of attention is in the Transformer architecture, introduced by Vaswani et al. in the 2017 paper “Attention is All You Need”. Let’s break down the components and mechanics of attention architecture, focusing on the Transformers, where attention is the primary mechanism.
🔍 What Is Attention?
Attention allows a model to focus on relevant parts of the input sequence when producing each part of the output. For example, when translating a sentence from English to French, the model may need to focus on different English words depending on which French word it’s generating.
🧠 Types of Attention
-
Self-Attention (or Intra-Attention):
- Each word (token) in the sequence attends to all other words in the same sequence, producing contextual representations.
- Used wildly in transformers for encoding and decoding sequences.
-
Cross-Attention:
- Occurs in the decoder of a transformer. The decoder attends to the encoder’s outputs.
- Example: In translation, the decoder attends to the encoded source sentence.
-
Scaled Dot-Product Attention (core computation):
- The foundation of attention used in Transformers.
- Calculates attention weights using dot-products, scaled by the dimension of the vectors.
🔧 Components of Attention (Scaled Dot-Product)
Given input vectors:
- Query (Q)
- Key (K)
- Value (V)
Mathematical Steps:
-
Compute attention scores:
[
\text{Scores} = \frac{QK^T}{\sqrt{d_k}}
]
where ( d_k ) is the dimension of the key vectors.
-
Apply softmax to scores to get attention weights:
[
\text{weights} = \text{softmax}\left( \frac{QK^T}{\sqrt{d_k}} \right)
]
-
Weight the values based on attention:
[
\text{Attention}(Q, K, V) = \text{softmax}\left( \frac{QK^T}{\sqrt{d_k}} \right) V
]
🔁 Multi-Head Attention
Instead of computing a single attention function, Multi-Head Attention runs multiple attention operations ("heads") in parallel:
[
\text{MultiHead}(Q, K, V) = \text{Concat}(head_1, \dots, head_h)W^O
]
Where each head:
[
\text{head}_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)
]
This allows the model to learn information from different representation subspaces at different positions.
🏗️ Transformer Block Architecture
Putting it together in a Transformer model (like BERT, GPT, etc.):
Encoder Block:
- Input Embedding + Positional Encoding
- Multi-Head Self-Attention
- Add & Norm (residual connection + LayerNorm)
- Feed-Forward Neural Network (FFN)
- Add & Norm
Decoder Block:
- Masked Multi-Head Self-Attention (to prevent attending to future tokens)
- Multi-Head Cross-Attention (attends to encoder outputs)
- Add & Norm
- Feed-Forward Neural Network
- Add & Norm
🧱 Variants of Attention
-
Soft vs. Hard Attention:
- Soft: differentiable (used in Transformers).
- Hard: discrete, non-differentiable (used occasionally, harder to train).
-
Additive Attention (Bahdanau Attention):
- Uses a feed-forward network to compute attention scores.
- Slower but was used in earlier seq2seq models.
-
Sparse Attention:
- Optimize memory and computation by only attending to a subset of positions. Used in models like Longformer, BigBird, etc.
📘 Diagram of Self-Attention (Textual Version)
Input embeddings --> Q, K, V projection --> Scaled Dot Product Attention --> Weighted output
Each token contributes to the attention calculation of every other token, enabling rich contextual understanding.
🚀 Applications of Attention
- NLP: Translation, summarization, language modeling (GPT, BERT)
- Computer Vision: Vision Transformers (ViT)
- Multimodal models: CLIP, Flamingo, Gemini (text + image), where attention can be used across modalities.
- Reinforcement Learning: To learn value-attention mappings.
🧠 Intuition
Think of attention like "looking up relevant information in a memory", where:
- Queries are your questions,
- Keys are possible indices of interest,
- Values are contents indexed by keys.
The attention score tells you how relevant each piece of information (value) is to your current task (query).
Let me know if you'd like visual diagrams or code examples (like in PyTorch or TensorFlow).