GPT, LLaMA, Claude — every modern LLM
Decoder-only transformers stack 32-100 layers of masked multi-head attention + MLP. Every token in every layer is a softmax-weighted average of prior tokens — that's the whole architecture.
Name things for the reader, not the writer.
Name things for the reader, not the writer.
Deep Learning Foundations
Attention is the operation that powers every modern transformer — the architecture behind GPT, Claude, BERT, and essentially every state-of-the-art language and vision model. At its mathematical core, attention is astonishingly simple: a soft-weighted lookup in a learned key-value store. Once you se
Attention = softmax over scaled query-key dot products, then weighted sum of values.
Without attention, transformers don't exist. RNNs and CNNs would still be state of the art, with all the limits that implies for long-range dependencies.
Attention is the operation that powers every modern transformer — the architecture behind GPT, Claude, BERT, and essentially every state-of-the-art language and vision model. At its mathematical core, attention is astonishingly simple: a soft-weighted lookup in a learned key-value store. Once you see it as scaled dot-products plus a softmax, the mystique evaporates and the linear-algebra structure becomes obvious.
Start with three sets of vectors derived from the input: queries , keys , and values . Each query asks 'which keys best match me?' by computing dot products — a similarity score for every key. Softmax these scores into a probability distribution over keys, then take the value-weighted average. The whole operation is one formula:
Why divide by ? When have iid components with unit variance, — dot products grow linearly with dimension. Without scaling, large pushes softmax into saturated regions where one entry is and the rest , killing gradients. Dividing by keeps the dot-product variance constant () regardless of dimension.
Multi-head attention replicates this operation times in parallel with different projections, then concatenates: . Each head learns to attend to a different relational pattern — syntax, semantics, position. Total compute is the same as one full-dimension head, but the model gets multiple specialized attentions.
Two structural facts shape what attention can and cannot do. Attention is permutation-equivariant: shuffling the rows of shuffles the rows of the output identically, so vanilla attention has no notion of order. Transformers fix this with positional encodings. Attention is also quadratic: costs — fine for small contexts, bottleneck for million-token inputs. The race for sub-quadratic attention (Performer, FlashAttention, Mamba) is the most active topic in efficient ML.
Worked example — single-query attention: , two keys , two values , . Scores: . Scaled: . Softmax: . Output: .
Python (in browser)
Expected: Each row of weights sums to 1; output is shape (4, 8)
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Python (in browser)
Expected: Lower-triangular weight matrix — token i never attends to j>i
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Worked example — variance check on the scaling: Generate vectors with iid entries. Without scaling, has variance → std . Differences are common, saturating softmax. With , std drops to . This single scalar made attention trainable.
Decoder-only transformers stack 32-100 layers of masked multi-head attention + MLP. Every token in every layer is a softmax-weighted average of prior tokens — that's the whole architecture.
Slice an image into 16×16 patches, treat each as a token, apply attention. Matches CNNs on ImageNet at scale and is the backbone of SAM, DINO, CLIP.
T5, original Transformer, BART use cross-attention where queries come from the decoder, keys/values from the encoder — the mechanism behind translation and summarization.
Stable Diffusion, Imagen, and DALL·E all use attention blocks inside the U-Net denoiser. Cross-attention from text embeddings is what makes them prompt-conditioned.
Put your understanding to the test. Score + streak + speed all count.
3 quick questions. Get 2 right to mark this lesson complete.