Attention

Layer 2 · Mechanics

Attention

The exact scaled dot-product formula, why the scaling factor exists, and how masking and multi-head splitting actually work mechanically.

10 min read70 XP

  1. X

    input embeddings, (n, d)

  2. Q, K, V

    three learned linear projections

  3. QKᵀ

    raw similarity scores, (n, n)

  4. ÷√d_k, mask, softmax

    turn scores into a weight distribution

  5. × V

    weighted sum of values

Scaled dot-product attention, mechanically, step by step.

Given an input matrix of shape tokens, each a -dimensional embedding — three learned weight matrices project it into Queries, Keys, and Values: , , . Attention is then a single formula that turns those three matrices into an output the same shape as .

Go deeper: L3 Code