Self-Attention Explained: Queries, Keys & Values Step by Step | LLM Course #18
Self-attention is the heart of every GPT. In Lecture 18 of the LLM Masterclass we build it by hand, step by step, on our own sentence "After my morning chai, I feel ___": queries, keys and values as a search, Q times K transpose, why we divide by the square root of d, softmax weights, and the weighted sum of values. Then we write chotugpt/attention.py in NumPy. 💻 Code for this lecture (branch lecture-18): https://github.com/programmingline/chotugpt/tree/lecture-18 Lecture 18 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model. The token vectors and weight matrices in this lecture are illustrative numbers for teaching only; a real model learns its weights in training. In this lecture: • Attention as a search: the query is what you look for, keys are what each token contains, values are what it passes on • Seven tokens, four numbers each: X @ W_Q, X @ W_K, X @ W_V give every query, key and value at once • Q @ Kᵀ: a 7 × 7 grid of scores (row = the token looking, column = the token looked at) • Why divide by √d: dot products grow with d, big scores make softmax spiky and gradients tiny • softmax(1, 2, 3) = 9%, 24%, 67% vs softmax(8, 16, 24) ≈ 0%, 0%, 100% • Softmax weights: feel listens to chai (31%) and morning (21%) • The weighted sum of values gives every token a new vector • NumPy code: softmax() and attention() in chotugpt/attention.py Chapters: 00:00 Lecture 17 quiz answer 00:14 Self-attention, part 1 00:26 Attention is a search 00:48 The heart of every GPT 00:54 Seven tokens, four numbers each 01:36 Scores: Q times K transpose 01:59 Why divide by √d? 02:38 Softmax: scores to weights 02:57 The weighted sum of values 03:24 attention.py in NumPy 04:02 Quiz 04:26 A peek into the future 04:42 Recap & next lecture ✅ Lecture 17 quiz answer: A, attention alone ignores word order. Without positions, "morning chai" and "chai morning" look the same. ❓ Quiz: Why are attention scores divided by the square root of d? A) To keep scores from growing too large, so softmax stays smooth B) To make training faster C) To count the tokens. Comment your answer. I reveal it in Lecture 19! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 17 — Positional encoding ⏭ Next: Lecture 19 — Self-attention, part 2: causal mask and multi-head Sources: A. Vaswani et al., "Attention Is All You Need", NeurIPS 2017 (scaled dot-product attention, section 3.2.1). #LLM #SelfAttention #Transformer…more
Self-attention is the heart of every GPT. In Lecture 18 of the LLM Masterclass we build it by hand, step by step, on our own sentence "After my morning chai, I feel ___": queries, keys and values as a search, Q times K transpose, why we divide by the square root of d, softmax weights, and the weighted sum of values. Then we write chotugpt/attention.py in NumPy. 💻 Code for this lecture (branch lecture-18): https://github.com/programmingline/chotugpt/tree/lecture-18 Lecture 18 of 32 — LLM Masterclass · Module 4: Inside the Transformer ⚠️ ChotuGPT is a simplified teaching model. The token vectors and weight matrices in this lecture are illustrative numbers for teaching only; a real model learns its weights in training. In this lecture: • Attention as a search: the query is what you look for, keys are what each token contains, values are what it passes on • Seven tokens, four numbers each: X @ W_Q, X @ W_K, X @ W_V give every query, key and value at once • Q @ Kᵀ: a 7 × 7 grid of scores (row = the token looking, column = the token looked at) • Why divide by √d: dot products grow with d, big scores make softmax spiky and gradients tiny • softmax(1, 2, 3) = 9%, 24%, 67% vs softmax(8, 16, 24) ≈ 0%, 0%, 100% • Softmax weights: feel listens to chai (31%) and morning (21%) • The weighted sum of values gives every token a new vector • NumPy code: softmax() and attention() in chotugpt/attention.py Chapters: 00:00 Lecture 17 quiz answer 00:14 Self-attention, part 1 00:26 Attention is a search 00:48 The heart of every GPT 00:54 Seven tokens, four numbers each 01:36 Scores: Q times K transpose 01:59 Why divide by √d? 02:38 Softmax: scores to weights 02:57 The weighted sum of values 03:24 attention.py in NumPy 04:02 Quiz 04:26 A peek into the future 04:42 Recap & next lecture ✅ Lecture 17 quiz answer: A, attention alone ignores word order. Without positions, "morning chai" and "chai morning" look the same. ❓ Quiz: Why are attention scores divided by the square root of d? A) To keep scores from growing too large, so softmax stays smooth B) To make training faster C) To count the tokens. Comment your answer. I reveal it in Lecture 19! 👍 Like, subscribe and comment your quiz answer. ⏮ Previous: Lecture 17 — Positional encoding ⏭ Next: Lecture 19 — Self-attention, part 2: causal mask and multi-head Sources: A. Vaswani et al., "Attention Is All You Need", NeurIPS 2017 (scaled dot-product attention, section 3.2.1). #LLM #SelfAttention #Transformer