FoundationsChecking saved investigationReading browser-local route memory before showing a continuation.

Foundation Lab

Scaled Dot-Product Attention & Transformer Layers

GPT-4, Claude, Gemini, Llama: giant stacks of decoder-only transformer blocks with causal self-attention

Concept 2 of 100Core TrainingPhase 1
#2AttentionCore Training
key equation\text{Attn}(Q,K,V) = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

Selected Foundation Object

Keep the equation fixed; move through the evidence.

Concept 2 of 100AttentionCore Training / Phase 1: Core probabilistic training + transformers
Current question

Geometric picture of Q–K dot products as measuring angles between feature directions, and how softmax turns those into a distribution of "who to copy from"

\text{Attn}(Q,K,V) = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V
PredictionCommit before the demo.

Ask what should change when the equation is manipulated, then let the visualization test that expectation.

EvidenceCompare local witness and source.

Use the runnable panel, the key equation, and canonical papers as separate forms of evidence for the same object.

InvariantName what survives notation changes.

The useful learning product is the reusable mechanism you can carry into another model, paper, or engineering tradeoff.

Next moveContinue in the newer notebook.

This atlas page keeps the working demo; the domain notebook carries the fuller Intuition -> Math -> Code -> Demo sequence.

Why It Matters for Modern Models

  • GPT-4, Claude, Gemini, Llama: giant stacks of decoder-only transformer blocks with causal self-attention
  • Stable Diffusion: U-Net with self- and cross-attention between image latents and text embeddings
  • Sora: diffusion transformer operating on spacetime patches (video tokens)

What Tutorials Skip

What is still poorly explained in textbooks and papers:

  • Geometric picture of Q–K dot products as measuring angles between feature directions, and how softmax turns those into a distribution of "who to copy from"
  • How multi-head attention effectively builds a set of learned kernels over positions/features, and why this is strictly more flexible than fixed kernels

Interactive Visualization

Core Math (Optional Deep Dive)

If you want intuition first, start with the key equation and the visualization. Come back here for the full walkthrough.

Key Equation
Attn(Q,K,V)=softmax ⁣(QKdk)V\text{Attn}(Q,K,V) = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

Single attention head:

Attn(Q,K,V)=softmax ⁣(QKdk)V\text{Attn}(Q,K,V) = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

where Q=XWQ, K=XWK, V=XWVQ = XW_Q,\ K = XW_K,\ V = XW_V. Multi-head attention concatenates several such heads.

A standard transformer block:

H=MHA(LN(H))+HHout=MLP(LN(H))+H\begin{aligned} H' &= \mathrm{MHA}(\mathrm{LN}(H)) + H \\ H^{\text{out}} &= \mathrm{MLP}(\mathrm{LN}(H')) + H' \end{aligned}

Canonical Papers

Attention Is All You Need

Vaswani et al.2017NeurIPS
Read paper →

Connections