How LLMs Work102 · Module II · Lesson 06 of 31
Visuals · 8 min

Attention, explained

Q, K, V — with a working diagram and worked numbers.

Visuals
Scaled dot-product attention, unrolled

A schematic. Inputs: query matrix Q, key matrix K, value matrix V, each [seq_len × d_head]. Step 1: compute QK^T → attention scores matrix [seq_len × seq_len]. Step 2: divide by sqrt(d_head), apply causal mask if decoder, softmax. Step 3: multiply attention weights by V → output [seq_len × d_head]. Below the diagram: 'Each output position is a weighted average of all input value vectors, where the weights come from Q·K similarity.'

Key Ideas
  • Attention: softmax(QK^T / √d) · V.
  • Each output is a weighted average of values, keyed by Q·K similarity.
Visuals

Diagrams and schematics accompanying this lesson.