- Attention Mechanism
-
Attention is a neural network mechanism that allows a model to focus on relevant
parts of the input when producing each output. It computes a weighted combination
of input representations, where weights indicate importance. Unlike fixed pooling,
attention weights are learned and input-dependent. Attention eliminates the
information bottleneck of recurrent networks by creating direct connections
between any input and output positions.
- Self-Attention
-
Self-attention (or intra-attention) relates different positions within the same
sequence to compute a representation. Each position queries all positions
(including itself) to determine what information to gather. This allows modeling
long-range dependencies with constant path length. Self-attention is the core
building block of transformer models and enables parallel processing of sequences.
- Query, Key, Value (Q, K, V)
-
The attention mechanism frames its computation using three vectors: Query (what
information am I looking for?), Key (what information do I have?), and Value
(what do I return when matched?). Attention scores are computed as Query-Key
dot products, normalized with softmax, then used to weight Values. In
self-attention, Q, K, and V are all derived from the same input sequence
through learned linear projections.
- Scaled Dot-Product Attention
-
Scaled dot-product attention computes attention as: Attention(Q,K,V) = softmax(QK^T/√d_k)V.
The scaling factor √d_k prevents dot products from growing too large for high-dimensional
vectors, which would push softmax into regions with tiny gradients. This formulation
is computationally efficient because it uses matrix multiplication operations that
are highly optimized on modern hardware.
- Multi-Head Attention
-
Multi-head attention runs multiple attention functions in parallel, each with
different learned projections. This allows the model to jointly attend to
information from different representation subspaces at different positions.
The outputs are concatenated and projected. Typical transformers use 8-16 heads,
with each head using a fraction of the total dimension.
- Transformer
-
The transformer is a neural architecture introduced in "Attention Is All You Need"
(2017) that relies entirely on attention mechanisms, eliminating recurrence. It
consists of stacked encoder and/or decoder blocks, each containing multi-head
attention, feed-forward networks, residual connections, and layer normalization.
Transformers enable massive parallelization and have become the dominant
architecture for NLP and increasingly for other modalities.
- Positional Encoding
-
Since self-attention is permutation-invariant (order doesn't affect computation),
transformers add positional encodings to inject sequence order information. The
original transformer uses sinusoidal functions at different frequencies; modern
models often learn positional embeddings. Without positional information, the
model cannot distinguish "dog bites man" from "man bites dog."
- BERT (Bidirectional Encoder Representations from Transformers)
-
BERT is an encoder-only transformer pre-trained with masked language modeling
(predicting randomly masked tokens) and next sentence prediction. Its bidirectional
nature—each token attends to all tokens—makes it powerful for understanding tasks
like classification and question answering. BERT pioneered the pre-train then
fine-tune paradigm that now dominates NLP.
- GPT (Generative Pre-trained Transformer)
-
GPT is a decoder-only transformer trained autoregressively to predict the next
token. Causal masking ensures each position only attends to previous positions.
GPT excels at text generation and, at large scale (GPT-3, GPT-4), demonstrates
emergent capabilities like few-shot learning through prompting. The GPT family
showed that scale dramatically improves model capabilities.
- Causal Masking
-
Causal masking (or look-ahead masking) prevents attention from "seeing" future
positions by setting attention weights to zero for positions ahead in the sequence.
This is essential for autoregressive generation: when generating token t, the
model should only use information from tokens 1 through t-1. Causal masking is
implemented by adding large negative values before softmax.
- Fine-Tuning
-
Fine-tuning adapts a pre-trained model to a specific task by training on
task-specific data, usually with a small learning rate. For transformers, this
typically involves adding a task-specific head (classification layer) and
training the entire model end-to-end. Fine-tuning is far more sample-efficient
than training from scratch because the model starts with general language
understanding.
- Layer Normalization
-
Layer normalization normalizes activations across the feature dimension (rather
than batch dimension like batch norm). For each sample, it computes mean and
variance across features and normalizes. This stabilizes training by reducing
internal covariate shift. Transformers apply layer norm after residual
connections, and this proves essential for training deep transformer stacks.