FromZero2AI
CoursesPlaygroundBlog
Support on Ko-fi
Generative AI & LLMs • Module A: The Transformer RevolutionLesson 1: How Transformers and Attention Work
Next

Lesson 1: How Transformers and Attention Work

Demystify the architecture that powers modern LLMs like GPT-4 and Claude.

Built with AI for beginners. Free forever.

Support on Ko-fi•About•Blog•Privacy Policy•Terms of Service

Every modern Large Language Model—including ChatGPT, Claude, and Gemini—is built on a single, revolutionary neural network architecture: the Transformer. Let's demystify how it works.

Before the Transformer

Historically, text processing relied on Recurrent Neural Networks (RNNs). RNNs processed text word-by-word sequentially. This meant they were slow to train, struggled to remember long-range dependencies, and could not run in parallel. Each word had to wait for the previous word to finish processing—imagine reading a book where you can only look at one letter at a time.

The Self-Attention Mechanism

Published in the famous 2017 paper "Attention Is All You Need", the Transformer replaced sequential processing with the Attention Mechanism. Instead of reading word-by-word, a Transformer processes all words simultaneously. Through Self-Attention, the model determines how much "attention" each word in a sentence should pay to every other word. For example, in the sentence "The bank of the river", the word "bank" pays attention to "river" to determine its meaning, rather than "money".

Interactive: Self-Attention Heatmap

Click any word to see its attention distribution. Each cell shows how much the Query word (row) attends to each Key word (column).

Attention Head:
The
chicken
crossed
the
road
because
it
was
tired
The
chicken
crossed
the
road
because
it
0.08
0.28
0.05
0.07
0.05
0.13
0.10
0.13
0.13
was
tired
Attention distribution for "it"
The
0.08
chicken
0.28
crossed
0.05
the
0.07
road
0.05
because
0.13
it
0.10
was
0.13
tired
0.13
Key Insight

Notice how "it" attends most strongly to "chicken" — the model learns coreference resolution, understanding that "it" refers back to "chicken", not "road".

Queries, Keys, and Values

Mathematically, Self-Attention works like a database lookup. For every token (word/sub-word), the network projects three vector representations, namely the Query, Key, and Value vectors:

  • Query (Q): What the token is looking for.
  • Key (K): What the token offers in terms of content/context.
  • Value (V): The actual information the token contains.

The model computes dot products of the Query vector of one word with the Key vectors of all other words, applies a softmax function to get attention weights, and multiplies these weights by the Value vectors.

The Attention Equation
Attention(Q, K, V) = softmax(Q · KT / √dk) · V
where dk = dimension of key vectors (scaling factor to prevent large dot products)
Interactive: Q/K/V Decomposition

See how a token is decomposed into Query, Key, and Value vectors, and how dot product scores are computed and normalized via softmax.

Token
"chicken"
Query (Q)
+0.8
-0.3
+0.5
+0.1
Key (K)
+0.7
-0.2
+0.6
+0.0
Value (V)
+1.0
+0.2
-0.5
+0.3
Step 1: Compute Q · KT scores
Qchicken · Kchicken
(+0.8)×(+0.7)+(-0.3)×(-0.2)+(+0.5)×(+0.6)+(+0.1)×(+0)
= 0.92← high similarity!
Qchicken · Kroad
(+0.8)×(+0.1)+(-0.3)×(+0.4)+(+0.5)×(-0.3)+(+0.1)×(+0.8)
= -0.11← low similarity
Step 2: Scale by √dk and apply Softmax
Scaled scores (÷ √4 = ÷ 2):
s₁ = 0.46, s₂ = -0.06

Exercise: Tensors and Attention Math

Answer the following multiple choice question about the attention equation:

Attention(Q, K, V) = softmax((Q · KT) / √dk) · V

What does the term Q · KT compute?

  • Q · K^T calculates the average value vector of the sentence.
  • Q · K^T calculates the similarity score (attention weight) between the query token and all other key tokens.
  • The √d_k factor is used to double the output size of the vectors.

In the next lesson, we will see how we can guide these attention mechanisms to generate highly specific text outputs using prompt engineering!