Navigation

Introduction to AI

Machine Learning

Deep Learning

Generative AI

Tools & Frameworks

General

Transformers and Attention Mechanisms - The Architecture Behind Modern AI

Transformers represent a fundamental shift in deep learning architecture. By replacing recurrence with attention mechanisms, they enabled training on much larger scales and achieving better performance. This post explores how they work.

The RNN Limitation

RNNs process sequences sequentially:

x₁ → h₁ → x₂ → h₂ → x₃ → h₃ → ... → x_n → h_n
     ↑         ↑         ↑              ↑

Problems:

  • Can’t parallelize: Must process each token sequentially
  • Long-distance dependencies still difficult (despite LSTMs)
  • Slow for training on large datasets
  • Information bottleneck at hidden state

Key Insight: Don’t need recurrence to capture dependencies. Attention is enough.

Attention Mechanism Intuition

Core Idea: For each output, attend to (focus on) relevant input tokens.

Analogy: When reading a sentence, you don’t process every word equally. You focus on words relevant to understanding current meaning.

Example:

"The government is considering closing schools"

When processing "schools," you pay attention to:
- "government" (subject)
- "closing" (action)
Less attention to:
- "is" (auxiliary verb)
- "the" (article)

Self-Attention Mechanism

Self-attention lets each token attend to every other token in sequence.

The Process

For each token, compute:

  1. Query (Q): What am I looking for?
  2. Key (K): What information do I have?
  3. Value (V): What information to pass on?

Attention Score:

Attention = softmax(Q × K^T / √d_k) × V

Step-by-Step Example

Sentence: “The cat sat”

Step 1: Create Q, K, V

For each word, linear transform creates Q, K, V vectors:

"The"  → Q_1, K_1, V_1
"cat"  → Q_2, K_2, V_2
"sat"  → Q_3, K_3, V_3

Step 2: Compute Attention Scores

For “cat” token (query Q_2):

Scores = [
  Q_2 · K_1,    (attention to "The")
  Q_2 · K_2,    (attention to "cat")
  Q_2 · K_3     (attention to "sat")
]

Step 3: Normalize with Softmax

Convert scores to probabilities:

Attention_weights = softmax([s_1, s_2, s_3])
                  ≈ [0.1, 0.7, 0.2]
                    (70% focus on itself, 10% on "The", 20% on "sat")

Step 4: Weighted Sum of Values

Output for “cat”:

Output = 0.1 × V_1 + 0.7 × V_2 + 0.2 × V_3

Multi-Head Attention

Use multiple attention “heads” in parallel:

Input

Head 1:  Linear → Attention → Linear ↓
Head 2:  Linear → Attention → Linear ↓→ Concat → Linear
Head 3:  Linear → Attention → Linear ↓
Head 4:  Linear → Attention → Linear ↓

Intuition: Different heads focus on different aspects:

  • Head 1: Syntactic relationships
  • Head 2: Semantic relationships
  • Head 3: Long-range dependencies
  • Head 4: Position information

Benefit: Multiple perspectives captured simultaneously

Transformer Architecture

Complete Transformer Block

Input Embedding

    ├→ Multi-Head Attention
    │    ↓
    │    Add & Normalize (Residual)
    │    ↓
    └→─────────────┐

    ┌──→ Feed-Forward Network (2 layers)
    │         ↓
    │    Add & Normalize (Residual)
    │         ↓
    └────────────→ Output

Encoder-Decoder

Encoder: Process entire input sequence

  • Multiple transformer blocks
  • Self-attention to all input tokens
  • Produces context representations

Decoder: Generate output sequence

  • Multiple transformer blocks
  • Self-attention to previous outputs
  • Cross-attention to encoder outputs
  • Produces one token at a time

Positional Encoding

Problem: Attention is permutation-invariant. “cat sat dog” = “dog cat sat”

Solution: Add positional information

Positional Encoding Formula:

PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))

Add to embeddings before attention

Result: Model knows token positions in sequence

Why Transformers Are Revolutionary

Parallelization

RNN: Process sequentially

Step 1: x₁
Step 2: x₂ (depends on step 1)
Step 3: x₃ (depends on step 2)
Step 4: x₄ (depends on step 3)
Total: 4 time steps

Transformer: Process all simultaneously

All tokens in parallel!
Attention connections let each see all others
Total: 1 time step

Impact: Train on GPUs/TPUs efficiently with large batches

Longer Context

RNN: Information bottleneck through hidden state

Long distant words → Hidden state → Lost information

Transformer: Direct attention connections

Token 1 ←→ Token 50 ←→ Token 100
Direct pathways preserve information

Impact: Handle longer contexts, capture long-range dependencies better

Scalability

Transformers scale to:

  • Larger models (billions of parameters)
  • Larger datasets (terabytes of text)
  • Longer sequences (thousands of tokens)

Result: Emergent capabilities at scale

Large Language Models

Built on transformer architecture:

GPT (Generative Pre-trained Transformer)

Architecture:

  • Decoder-only transformer
  • Self-attention to all previous tokens
  • Autoregressive: Predicts next token based on previous

GPT Versions:

  • GPT-2 (1.5B params): Surprisingly capable
  • GPT-3 (175B params): Few-shot learning
  • GPT-3.5, GPT-4: State-of-the-art

BERT (Bidirectional Encoder Representations)

Architecture:

  • Encoder-only transformer
  • Attends to all tokens (both directions)
  • Pre-trained with masked language modeling

Use Case: Understanding, classification, question answering

T5 (Text-to-Text Transfer Transformer)

Architecture:

  • Encoder-decoder transformer
  • Frames all tasks as text-to-text

Use Case: Translation, summarization, question answering

Advantages and Disadvantages

Advantages

  • Parallel Training: Fast training on large datasets
  • Long Context: Handle longer dependencies
  • Scalable: Improve with more data and parameters
  • Transfer Learning: Pre-train, fine-tune on tasks
  • Interpretable Attention: Visualize what model focuses on

Disadvantages

  • Computational Cost: Attention O(n²) in sequence length
  • Memory Usage: Storing attention matrices expensive
  • Requires Large Data: Usually needs millions of examples
  • Energy Consumption: Training massive models expensive
  • Position Encoding Limits: Fixed maximum sequence length

Handling Long Sequences

Linear Attention

Approximate attention in O(n) instead of O(n²)

Sparse Attention

Only compute attention for subset of positions

Local Attention

Attention within local windows only

Hierarchical Attention

Multi-level attention at different scales

Practical Applications

Machine Translation

  • Input: Text in source language
  • Encoder: Understand meaning
  • Decoder: Generate translation

Summarization

  • Compress long text to key points
  • Encoder-decoder architecture

Question Answering

  • Input: Context + question
  • Output: Answer from context

Text Classification

  • Use encoder or classifier head
  • Fine-tune on labeled data

Code Generation

  • Input: Prompt/specification
  • Output: Code that accomplishes task

Training Transformers

Pre-training

Objective: Predict next token (for GPT) or masked token (for BERT)

Data: Massive text corpus (Wikipedia, books, web)

Duration: Weeks/months on TPU clusters

Fine-tuning

Process:

  1. Start with pre-trained model
  2. Replace task-specific layer
  3. Train on labeled task data
  4. Quick, requires little data

Impact: Enables small organizations to use powerful models

Conclusion

Transformers revolutionized AI by replacing recurrence with attention. Self-attention mechanisms let tokens attend to all other tokens simultaneously, enabling parallelization and longer context. The encoder-decoder architecture with positional encoding handles sequence tasks elegantly. Multi-head attention captures multiple aspects of dependencies. These innovations enabled training massive language models that achieve remarkable capabilities. Transformers remain the foundation of modern LLMs and continue to evolve with improvements in efficiency, length handling, and capabilities. Understanding transformers is essential for modern AI development.