POST 1 of 5 MorningAI/MLConcept
Attention in three letters: Q, K, V
📅 Day 52. Attention is the most important architectural innovation in deep learning since the convolution. Once you grok Q, K, V, every transformer becomes readable. 🎯 The intuition. For each token in a sequence, attention asks — 'which other tokens should I pay attention to, and how much?' It has three vectors per token, each derived from the input via a linear projection: → Q (query) — what am I looking for? → K (key) — what do I offer? → V (value) — what information do I carry? 🧮 The mechanics — for each query, dot it with every key. The result is a similarity score — how much should this token attend to that token? Apply softmax to turn scores into weights (sum to 1). Take a weighted sum of all the value vectors using these weights. Formula — Attention(Q, K, V) = softmax(QKᵀ / √d) · V. The √d in the denominator scales the dot products to prevent the softmax from saturating. 📊 Result — each token gets a context-aware representation that mixes information from all relevant tokens. The representation depends on what's around the token. The same word in different contexts gets different representations. This is the secret behind transformers' contextual understanding. ⚡ The 'in parallel' part is what made transformers replace RNNs. RNNs process tokens one at a time. Attention processes ALL token pairs at once, in a single matrix multiplication. GPU-friendly; massively parallelisable. 🌐 Same operation in vision (Vision Transformers split images into patches; each patch is a 'token'), audio (Whisper), code (Codex), proteins (AlphaFold). The Q-K-V mechanism is universal. 💡 Three letters. The foundation of everything. Internalise once.
#NLP#Transformers#LLM#AI#100DaysOfCode#Attention