AI Model Wiki

    Skip to main content
    8 min read
    Lesson 4 of 5

    Transformers & the Attention Mechanism

    From the course Introduction to AI: Core Concepts

    The transformer architecture, introduced in Google's 2017 paper "Attention Is All You Need," is the single most important innovation behind modern AI. GPT, Claude, Gemini, BERT, Stable Diffusion — they all use transformers.

    The Problem Transformers Solved

    Before transformers, language models used RNNs (Recurrent Neural Networks), which processed text one word at a time, in sequence. This had two major problems:

    1. Slow: Processing is sequential — word 100 has to wait for words 1-99
    2. Forgetting: Long-range dependencies get lost (by word 500, the model has "forgotten" word 10)

    The Attention Mechanism

    Transformers solve this with "attention" — the ability to look at all words simultaneously and figure out which ones are important for understanding each word.

    Consider: "The cat sat on the mat because it was tired."

    What does "it" refer to? A transformer uses attention to determine that "it" relates to "cat" (high attention score) rather than "mat" (low attention score).

    Self-Attention Step by Step

    For each word in a sentence:

    1. Create three vectors: Query (what am I looking for?), Key (what do I contain?), Value (what information do I provide?)
    2. Compare the Query of the current word against the Keys of all other words
    3. High similarity = high attention score
    4. Use attention scores to create a weighted sum of Values
    5. Result: a representation of each word that's aware of the entire context

    This happens in parallel for all words simultaneously — that's why transformers are fast.

    Multi-Head Attention

    Transformers don't just compute attention once. They do it multiple times in parallel ("heads"), each focusing on different types of relationships:

    • One head might focus on grammatical relationships
    • Another on semantic similarity
    • Another on positional proximity
    • Another on coreference (pronoun resolution)

    The results are combined for a rich understanding of each word in context.

    Encoder vs. Decoder

    The original transformer had two parts:

    • Encoder — Reads and understands input text. Used in BERT (for classification, search, understanding tasks).
    • Decoder — Generates output text one token at a time. Used in GPT (for text generation).

    Modern LLMs like GPT-4 and GPT-5 are decoder-only transformers. They predict the next word given all previous words.

    Scale Is the Secret

    What makes modern AI remarkable isn't just the transformer architecture — it's scale:

    • GPT-3: 175 billion parameters
    • GPT-4: ~1.7 trillion parameters (estimated)
    • Training data: trillions of words from the internet
    • Compute: thousands of GPUs running for months

    The "scaling laws" suggest that larger models trained on more data generally perform better, though there are diminishing returns and practical limits.

    Beyond Language

    Transformers now power much more than text:

    • Vision Transformers (ViT) — Process images by treating patches as "tokens"
    • Diffusion transformers — Power image generation (Stable Diffusion, DALL-E 3)
    • Audio transformers — Power speech recognition and music generation
    • Multimodal transformers — Process text, images, and audio together (GPT-4o, Gemini)

    Key Takeaways

    • •Transformers replaced sequential RNNs by processing all words simultaneously
    • •The attention mechanism lets each word consider all other words for context
    • •Multi-head attention captures different types of relationships in parallel
    • •Modern LLMs like GPT are decoder-only transformers predicting the next token
    • •Transformers now power vision, audio, and multimodal AI beyond just language
    0 of 5 lessons marked complete

    Progress is kept in this browser only.