The Transformer Masterclass: How AI Stopped Reading Word by Word
A visual, story-driven walkthrough of the architecture behind GPT, Claude, and Gemini — written by a learner, for learners.
The Transformer Masterclass: How AI Stopped Reading Word by Word
A visual, story-driven walkthrough of the architecture behind GPT, Claude, and Gemini — written by a learner, for learners.
~12 min read
Why this exists
When I started learning Deep Learning, Transformers felt intimidating.
Everywhere I looked — equations, diagrams, jargon. Attention. Queries. Keys. Values. Multi-head attention. Positional encoding. Layer norm. Residual connections. The architecture looked complex before it ever looked beautiful.
What I eventually realised was something simple but freeing:
Transformers are not difficult because the ideas are complicated. They are difficult because they are usually explained out of order.
So I rebuilt the entire journey from first principles — not as a researcher, but as a learner. I asked one question at every step: what problem was each component invented to solve?
That challenge became a 100+ page masterclass. This article is the condensed, story-driven version of that journey. If you want the full deep-dive (with all derivations, citations, and visual deconstructions), the link is at the bottom.
Part 1 — The Problem: A Machine That Read Word by Word
Before 2017, the state of the art for language was the RNN (and its smarter cousin, the LSTM). RNNs read text the way a human reads through a keyhole: one word at a time, carrying a running summary of what they had seen so far.
This design had three fatal flaws:
- Sequential by design. You cannot process token N until you’ve finished token N−1. GPUs, which are massively parallel, sat idle waiting for their turn. We were running a Ferrari engine at bicycle speed.
- Amnesia. The “running summary” — the hidden state — had finite capacity. By the time the RNN reached the end of a long paragraph, the meaning of the first sentence had been overwritten many times. Long-range context simply faded away.
- Vanishing gradients. During training, the error signal had to travel backward through every single timestep. Multiplying small numbers together hundreds of times shrinks them to zero — the model literally couldn’t learn from long sequences.
Property RNN LSTM Transformer Parallelizable? No No Yes ✓ Long context? Poor Better Excellent ✓ GPU utilization <5% <5% >90% ✓ Powers modern LLMs? No No GPT · Claude · Gemini ✓
The fix had to be radical. We needed a mechanism that let every token look at every other token simultaneously, with no sequential dependency.
That mechanism is Attention.
Part 2 — The Breakthrough: “Attention Is All You Need”
In June 2017, eight researchers at Google Brain published a paper with one of the most confident titles in machine learning history: “Attention Is All You Need.”
The radical idea: throw away RNNs entirely. Let every word look at every other word — all at once, in parallel.
It didn’t just work. It unlocked an entirely new scaling regime:
Model Year Parameters Note GPT-1 2018 117M First proof of concept GPT-2 2019 1.5B OpenAI initially refused to release it GPT-3 2020 175B Emergent capabilities appeared GPT-4 2023 ~1T Passes the bar exam
None of this was possible with RNNs. Throw more data, more compute, more parameters at a Transformer — and capabilities keep emerging. Without Transformers: no GPT, no ChatGPT, no Claude, no Gemini, no Copilot.
The architecture didn’t just solve one problem. It unlocked the entire modern AI era.
Part 3 — The Heart of Attention: Q, K, V
Every modern AI model — without exception — is powered by three vectors per token: Query, Key, and Value.
The cleanest way to understand them is to think of a Google search:
- Q (Query) — what this token is looking for right now. (Your search box.)
- K (Key) — what this token advertises to others. (The titles of indexed web pages.)
- V (Value) — the actual content shared if there’s a match. (The full page that gets returned.)
A token’s Query is matched against every other token’s Key. Strong matches pull in those tokens’ Values; weak matches contribute almost nothing.
Here’s the part that confuses people on first read: Q, K, and V are not separate inputs. They all come from the same input embedding X, passed through three different learned matrices:
Q = X · W_Q
K = X · W_K
V = X · W_V
W_Q, W_K, W_V are weight matrices the model learns during training. They are three different lenses on the same data — what to search for, what to match against, what to return.
Every token simultaneously acts as a searcher, an index entry, and a content provider.
Part 4 — The Formula Behind Every Modern AI Model
With Q, K, V in hand, attention is one line of math:
Attention(Q,K,V)=softmax (Q⋅KTdk)⋅V\text{Attention}(Q, K, V) = \text{softmax}!\left(\frac{Q \cdot K^T}{\sqrt{d_k}}\right) \cdot VAttention(Q,K,V)=softmax(dkQ⋅KT)⋅V
Let me unpack it in four steps:
**Q · Kᵀ* — Take the dot product of every Query with every Key. This is just a similarity score: how aligned are these two tokens?* For T tokens, you get aT × Tmatrix.**÷ √dₖ** — Scale down by the square root of the key dimension. Without this, the dot products grow huge for high-dimensional vectors and push softmax into saturation (one token gets weight ~1, everything else ~0). Scaling keeps gradients healthy.**softmax(...)— Convert each row of scores into probabilities that sum to 1. Each row is now an attention distribution**: "of all the tokens I can see, here's how much I should listen to each one."**× V— Take a weighted sum of the Value vectors using those probabilities. The output is a blended meaning** — each token's new representation is a mixture of all the tokens it found relevant.
That’s it. That’s the secret behind GPT-4, Claude, and Gemini. One equation.
Part 5 — Self-Attention in Action: The Pronoun Mystery
Let’s make this concrete. Consider the sentence:
“The animal didn’t cross the road because it was tired.”
How does the model know what “it” refers to? In English, “it” could grammatically point to “the road” or “the animal.” Roads don’t get tired. Animals do. Humans use world knowledge. So does a Transformer — but it discovers this from data alone.
When we compute the attention weights from “it” → every other word, this is roughly what we see in a well-trained model:
Word Attention from “it” The 2% animal 76% ← didn’t 2% cross 1% the 2% road 3% because 3% it 7% was 3% tired 2%
Seventy-six percent of “it”’s attention flows to “animal.” No grammar rules. No hand-coded coreference resolver. No lookup table. Just learned attention patterns from billions of sentences.
This is what people mean when they say Transformers build contextual embeddings. After the attention layer, the vector representing “it” now carries the meaning of “animal.” The word “it” has effectively become “the animal” inside the model.
Part 6 — Multi-Head Attention: The Team of Experts
One attention pattern is not enough. Language has multiple kinds of relationships happening simultaneously — grammatical, semantic, positional, referential. Forcing one attention computation to capture all of them blurs every distinction.
The solution is elegant: run attention many times in parallel, with different learned weight matrices each time. These parallel runs are called heads.
The original Transformer used 8 heads. When researchers later inspected what each head was doing, they found something remarkable — heads had spontaneously specialised:
- Head 1 — Grammar (subject ↔️ verb agreement)
- Head 2 — Coreference (“it” ↔️ “animal”)
- Head 3 — Local structure (adjacent token patterns)
- Head 4 — Long-range dependencies
- Head 5 — Semantic clustering
- Head 6 — Positional rhythm
- Head 7 — Phrase attachment
- Head 8 — Topic and sentiment
The formula:
MultiHead(Q, K, V) = Concat(head₁, head₂, ..., head₈) · W_O
With d_model = 512 and 8 heads, each head gets a 64-dimensional subspace (8 × 64 = 512). Same total compute, eight times richer representation.
Here’s the critical part: no engineer ever wrote a “grammar head” or a “coreference head.” These heads emerge spontaneously from training. The model discovers these linguistic structures on its own, by optimising one single objective: next-token prediction on billions of sentences.
This is one of the most remarkable results in deep learning interpretability.
Part 7 — Positional Encoding: Teaching Order to an Order-Blind Mechanism
Attention has a surprising weakness: it is order-blind. The math treats input tokens as a set, not a sequence. Without intervention, the Transformer would see “dog bit man” and “man bit dog” as identical.
The original paper solved this with sinusoidal positional encoding, adding sine and cosine waves of different frequencies to each token’s embedding:
PE(pos, 2i) = sin(pos / 10000^(2i/d))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d))
Think of each dimension as a clock hand spinning at a different speed. Combine many speeds and you get a unique fingerprint for every position.
The field has moved on since 2017. Here is the modern landscape:
Method Year Mechanism Used In Sinusoidal 2017 Add sin/cos waves to embeddings Original Transformer Learned 2018 Train a position table BERT, GPT-2 RoPE ★ 2021 Rotate Q and K vectors by position LLaMA · GPT-4 · Claude ALiBi 2022 Add a distance penalty to attention scores MPT, Falcon
RoPE (Rotary Position Embedding) is now the standard in virtually all frontier models. It encodes relative position directly into the Q and K vectors — which lets models generalise to context windows longer than they were trained on. This is the trick behind 128K, 200K, and even 1M token context windows you see today.
Part 8 — The Full Block: Encoder, Decoder, and Everything Between
A Transformer is not just attention. It’s a block stacked many times. The block has two halves:
Attention — “group meeting.” Every token shares context with every other token.
Feed-Forward Network (FFN) — “private desk.” Each token, now enriched with context, sits alone and processes its new understanding through a small MLP (typically d → 4d → d).
Wrap each half with Add & Norm (residual connection + layer normalization), then stack:
- BERT base: 6 blocks
- GPT-2: 12 blocks
- GPT-3: 96 blocks
That’s the whole model. Stacked attention + FFN with residuals and norms.
The original Transformer had two flavors of this block — an Encoder (which sees the full input bidirectionally, used for understanding) and a Decoder (which sees only past tokens via a causal mask, used for generation).
During training, decoders use teacher forcing — they see the correct previous tokens at every step, so the whole sequence can be processed in parallel. During inference, they generate one token at a time, feeding their own predictions back in. This is why a 4-second prompt to ChatGPT takes 4 seconds to answer — generation is fundamentally sequential, even though training is parallel.
This train-vs-inference gap is also why KV caching matters so much in production: instead of recomputing Keys and Values for every previous token at every new step, we store them once and reuse them.
Part 9 — Three Families, One Architecture: BERT vs GPT vs T5
Once you understand the block, the whole zoo of modern language models snaps into focus. Every major architecture is just a different way of arranging the same Transformer block:
BERT GPT T5 Architecture Encoder-only Decoder-only Encoder–Decoder Direction Bidirectional Left-to-right (causal) Both (conditional) Training objective Masked language modeling (fill in the blanks) Next-token prediction (autoregressive) Text-to-text (span corruption) Best at Classification, search, Q&A Generation, chat, code Translation, summarisation
All three share the same Transformer core. They differ only in architecture choice (encoder, decoder, or both) and training objective (what task you optimise for).
GPT-style decoder-only models won the race for chat because next-token prediction at massive scale turned out to be a stunningly general way to compress human knowledge. But BERT still powers most of Google Search, and T5-style models dominate translation.
On top of these cores, a layer of engineering tricks makes modern LLMs practical:
- Flash Attention — reduces attention memory from
O(n²)toO(n)by being clever about GPU memory hierarchy. The reason 128K context windows are even possible. - KV Cache — reuse previous Keys and Values during generation instead of recomputing them.
- LoRA — fine-tune large models by training only ~0.78% of their parameters. Democratised customization.
- RLHF — Reinforcement Learning from Human Feedback. The reason ChatGPT feels helpful instead of just “completing your text.”
Part 10 — The Whole Journey in One Diagram
Here’s what happens when you type a prompt into a Transformer-based model:
Raw Text
↓
Tokenizer (split into sub-words)
↓
Embedding + Positional Encoding
↓
┌─────────────────────────────┐
│ Linear Projections: Q, K, V │
│ ↓ │
│ Self-Attention │
│ (scaled dot-product) │
│ ↓ │
│ Concat heads · W_O │
│ ↓ │
│ Add & Norm │
│ ↓ │
│ Feed-Forward (d → 4d → d) │
│ ↓ │
│ Add & Norm │
└─────────────────────────────┘
↓
(repeat × N layers)
↓
Linear + Softmax
↓
Next-token probabilities
Every word starts as a generic embedding. Layer by layer, attention enriches it with context from every other word. By the final layer, the representation of each token carries the meaning of the whole sentence — and the model uses that to predict what comes next.
Words as agents in a meeting, refining their understanding by listening to each other. That’s the entire mental model.
What I want you to take away
When I started, I could use the terms. I could copy the diagrams. I could even run the code. But I couldn’t explain why any of it existed — why Query, Key, Value? Why divide by √dₖ? Why stack 12 layers?
Then it clicked:
- Attention exists because RNNs were slow and forgetful.
- Q, K, V exists because we needed a learnable way to ask “who is relevant to whom?”
- Scaling by √dₖ exists because raw dot products explode in high dimensions.
- Multi-head exists because one attention pattern is too few.
- Positional encoding exists because attention is order-blind.
- Residuals and layer norm exist because deep networks don’t train without them.
- Decoder masking exists because you can’t cheat during training by looking at future tokens.
None of these are arbitrary. Every component is a fix for a specific problem. Once you see the problems in the right order, the architecture stops being a wall of jargon and becomes a sequence of careful, intentional choices.
The future of AI will be built by people who understand the fundamentals deeply. Not by those who memorise APIs, but by those who understand why the math is shaped the way it is.
Keep building. Keep questioning. Keep learning.
Why I wrote this
When I first encountered Transformers, the explanations I found were either too math-heavy or too superficial. I wanted to build a mental model from the ground up. So I took on a challenge: learn each part of the architecture so well that I could teach it back.
This guide is the result of that journey. It’s not a research paper or lecture notes — it’s a story-driven walkthrough, written while I was learning.
Writing it was also an exercise in technical communication. It forced me to turn foggy concepts into clear analogies, diagrams, and concise math. If this helps even one person understand Transformers more deeply than I did, the effort was worth it.
— Prachi Yadav · CSE-AIML · AI Engineer & Educator
Go deeper
This article is the condensed version. The full 100+ page Transformer Masterclass contains:
- All 13 parts with full derivations
- 28 hand-crafted diagrams
- Tensor shapes at every step
- Common misconceptions debunked
- Advanced topics: Flash Attention, Speculative Decoding, LoRA, GQA, RAG
- A complete mental-model recap
📘 Full PDF on GitHub: github.com/10Prachi2006/Transformers
If this article helped you, clap, share, and tag someone learning AI. The best way to support free educational content is to help it reach the next person who needs it.
Tags: #MachineLearning #DeepLearning #Transformers #LLM #AI #ArtificialIntelligence #NeuralNetworks #GPT
메타데이터
- post_id
- cd51e531aff6
- slug
- a-deep-learning-learning-document-cd51e531aff6
- url
- https://medium.com/@starletprachi10/a-deep-learning-learning-document-cd51e531aff6
- canonical_url
- https://medium.com/@starletprachi10/a-deep-learning-learning-document-cd51e531aff6
- author_url
- https://medium.com/@starletprachi10
- status
- ok
- fetched_at
- 2026-06-09 15:37:30