Transformers and Attention Explained: The Tech Behind ChatGPT
Transformers power ChatGPT, Gemini and Claude. Learn the one idea that made them possible — attention — plus why they beat RNNs, with clear diagrams, a runnable NumPy demo, and real Hugging Face code.
Every tool that stunned the world — ChatGPT, Gemini, Claude, image generators, code assistants — runs on one architecture: the Transformer. It was introduced in a 2017 paper with the bold title "Attention Is All You Need," and it swept aside the RNNs you met in Lesson 18. If you understand this lesson, you understand the engine underneath the entire modern AI boom. And the core idea — attention — is genuinely intuitive.
The problem Transformers solved
RNNs read a sentence one word at a time, carrying a memory forward. Two weaknesses came with that:
- Slow. Word 10 can't be processed until words 1–9 are done. You can't parallelise it, which makes training on huge datasets painfully slow.
- Forgetful. Even LSTMs strain to connect words that are far apart in a long paragraph.
Transformers throw out the one-at-a-time loop entirely. They look at every word at once and let each word decide which other words matter to it. That mechanism is attention.
The key idea: attention
Consider the sentence "The cat sat because it was tired." What does "it" refer to? You instantly know it's the cat. Attention is how a Transformer does the same: for every word, it looks at all the other words and assigns each a weight — how much to "pay attention" to it. "it" attends strongly to "cat" and weakly to "because."
How attention actually works (in plain words)
For each word, the Transformer creates three vectors — with three easy nicknames:
| Vector | Think of it as… |
|---|---|
| Query (Q) | "What am I looking for?" — the current word's question. |
| Key (K) | "What do I offer?" — a label every word advertises. |
| Value (V) | "The information I carry" — passed on if I'm attended to. |
The word's Query is compared with every other word's Key. A good match = a high score. Those scores are turned into weights (with softmax, so they add to 1), and the output is a weighted blend of all the Values. "it" has a Query that matches "cat"'s Key strongly, so it pulls in mostly cat's Value. That's the entire mechanism.
See attention in NumPy (runnable)
Here is real self-attention over three words, in plain NumPy. Run it and read the attention weights — each row shows how much one word attends to the others.
import numpy as np
def softmax(x):
e = np.exp(x - x.max(axis=-1, keepdims=True))
return e / e.sum(axis=-1, keepdims=True)
np.random.seed(1)
words = ["The", "cat", "sat"]
# Each word starts as a 4-number vector (would be learned)
X = np.random.randn(3, 4)
# Learnable projections to Query, Key, Value
Wq = np.random.randn(4, 4)
Wk = np.random.randn(4, 4)
Wv = np.random.randn(4, 4)
Q, K, V = X @ Wq, X @ Wk, X @ Wv
# scores = how well each Query matches each Key
scores = Q @ K.T / np.sqrt(4)
weights = softmax(scores) # each row sums to 1
output = weights @ V # blend of Values
print("Attention weights:\n", np.round(weights, 2))
# Row i = how much word i attends to The / cat / sat.
That softmax(Q·Kᵀ)·V is the beating heart of every large language model. Everything else is scaling it up.
Why Transformers beat RNNs
Because attention looks at all words simultaneously, there's no left-to-right loop — the whole sentence is processed in parallel on the GPU, and any word can directly attend to any other, no matter how far apart.
The rest of a Transformer, briefly
Real Transformers wrap attention in a few extras — you'll hear these terms, so here's the one-line version of each:
- Multi-head attention: run attention several times in parallel, each "head" focusing on different relationships (grammar, meaning, references).
- Positional encoding: since there's no left-to-right loop, the model adds a signal marking each word's position so order isn't lost.
- Feed-forward layers & stacking: attention blocks are stacked dozens of times; big models just stack more and make them wider.
What real code looks like
You'll never hand-code all that. Libraries like Hugging Face Transformers load a pre-trained model in a few lines. (Needs pip install transformers; shown so you recognise it.)
from transformers import pipeline
# Download a pre-trained Transformer and use it in 2 lines
classifier = pipeline("sentiment-analysis")
print(classifier("I love learning AI on GyaanPost!"))
# [{'label': 'POSITIVE', 'score': 0.999...}]
From Transformers to ChatGPT
A large language model like ChatGPT is a giant Transformer trained on a huge slice of the internet to do one simple task: predict the next word. Attention lets it hold the whole conversation in context while it predicts, one token at a time. That's the direct line from this lesson to the tools everyone's talking about — and it's exactly where the next lesson picks up.
The one sentence to keep: attention lets every word look at every other word and decide what matters — and that single idea, scaled up massively, is what powers ChatGPT.
The first time I printed that attention-weights matrix and saw one row light up on exactly the word I'd expect it to "care" about, the mystery of large language models genuinely lifted for me — it went from magic to a mechanism I could reason about. I'd nudge you to run the NumPy snippet and change the words; watching the weights shift is the moment it clicks.
Key takeaways
- The Transformer is the architecture behind ChatGPT, Gemini and Claude; it replaced RNNs in 2017.
- Attention lets every word weigh how relevant every other word is — capturing context directly.
- It works via Query, Key, Value vectors: match queries to keys, then blend values (
softmax(Q·Kᵀ)·V). - Transformers process all words in parallel, making them fast to train and strong at long-range context.
- Multi-head attention, positional encoding and stacking turn this core idea into full models — and stacking it huge gives you an LLM.
Continue the series: ← Lesson 18: Recurrent Neural Networks (RNNs) and LSTMs · Next: Lesson 20 — Large Language Models (LLMs): How ChatGPT Really Works →
Frequently Asked Questions
What is a Transformer in AI?
A Transformer is a neural network architecture introduced in 2017 that processes all words in a sequence at once using a mechanism called attention. It powers modern AI systems like ChatGPT, Gemini and Claude, and it replaced older RNN and LSTM models for most language tasks.
What is attention in a Transformer?
Attention lets each word in a sentence look at every other word and assign a weight showing how relevant it is. For example, in 'the cat sat because it was tired', attention connects 'it' strongly to 'cat', giving the model context about what each word refers to.
What are Query, Key and Value in attention?
Each word produces three vectors: a Query (what it is looking for), a Key (what it offers) and a Value (the information it carries). A word's Query is compared to every Key to get attention scores, which are turned into weights that blend the Values into the output.
Why did Transformers replace RNNs?
RNNs read words one at a time, which is slow and struggles with long-range context. Transformers process all words in parallel and let any word attend directly to any other, so they train much faster on large datasets and handle long text far better.