Lesson 1: How Transformers and Attention Work
Demystify the architecture that powers modern LLMs like GPT-4 and Claude.
By Bishwambhar Sen • Last updated • 90 min
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".
Click any word to see its attention distribution. Each cell shows how much the Query word (row) attends to each Key word (column).
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.
See how a token is decomposed into Query, Key, and Value vectors, and how dot product scores are computed and normalized via softmax.
Exercise: Tensors and Attention Math
Answer the following multiple choice question about the attention equation:
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!