Self-Attention, Step by Step
Queries, keys, and values.
Self-Attention, Step by Step is a free NLP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Self-Attention?
In self-attention, every word looks at every other word in the same sentence to build a richer, context-aware version of itself. 🔍
Three Roles per Word
Each word is projected into three vectors: a query, a key, and a value. These three roles drive the whole self-attention computation.
The Query
The query represents what a word is looking for. Think of it as the question this word asks about the rest of the sentence.
Keys and Values
Each word also offers a key that advertises what it contains, and a value holding the actual information to pass along if chosen.
Scoring With Dot Products
Compare a query to every key using a dot product. A larger product means the query and key align, so that word deserves more focus.
scores = query @ keys.TScale the Scores
Divide scores by the square root of the key dimension. This scaling keeps numbers stable so softmax does not become too sharp.
scores = scores / (d_k ** 0.5)Softmax for Weights
Run the scaled scores through softmax to get attention weights that are positive and sum to 1 across all words in the sentence.
weights = softmax(scores)Blend the Values
Multiply each value by its weight and add them up. The result is a new vector that blends information from the most relevant words.
output = weights @ valuesThe Full Formula
All steps combine into one tidy expression: scaled dot-product attention over queries, keys, and values, as introduced in the Transformer paper.
attn = softmax(Q @ K.T / d_k**0.5) @ VWhy Self, Not Cross
It is called self-attention because the queries, keys, and values all come from the same sequence, letting words attend to their own neighbors.
Resolving Ambiguity
In "it was tired," self-attention links it to the right noun by weighting nearby words, giving every token clearer context.
Quick Check
Let us confirm the self-attention steps.
Recap
You walked through self-attention: turn words into queries, keys, and values, score, scale, softmax, then blend the values. That is the engine. ⚙️
Frequently asked questions
Is the “Self-Attention, Step by Step” lesson free?
Yes — the full text of “Self-Attention, Step by Step” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Self-Attention, Step by Step”?
Queries, keys, and values. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start NLP Academy?
No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Self-Attention, Step by Step” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this NLP Academy lesson?
Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Idea of Attention
- Self-Attention, Step by Step
- Multi-Head Attention and Positions
- Inside the Transformer Block