Skip to the document
Madhuopen lab
Outcome School · Transformers and architecture13 min read

by Amit Shekhar · 3 April 2026

Math behind Attention - Q, K, and V

The math behind Attention - Query(Q), Key(K), and Value(V) with a step-by-step numeric example.

2,492 words#math#llm#ai#machine-learning3 recall cards

Math behind Attention - Q, K, and V
Read on Outcome School ↗then come back to lock it in
Before you read, guess

What is the initial calculation performed in the attention mechanism?

Ten seconds, a guess, then read — a wrong guess still makes the answer stick.

What this article covers

  1. The Attention Formula
  2. Setting Up: From Words to Vectors
  3. Creating Q, K, and V Matrices
  4. Computing Attention Scores (Q x K^T)
  5. Scaling the Scores
  6. Applying Softmax
  7. Computing the Final Output (Attention Weights x V)
  8. Putting It All Together

The article lives on outcomeschool.com. Read it there, then come back: the tutor in the margin has read it and will answer questions, and the questions below check what stayed.

Before you go

In one sentence, what was this chapter about?

From memory, without scrolling up. Writing it is what makes it yours; the grade is only to show you what you had.

How sure?