What is the attention mechanism, in one sentence, and what makes it structurally different from convolution and recurrence?
Attention is a learned soft retrieval where each token weights every other token directly. Unlike RNN/CNN, every token pair has a one hop gradient path.
Imagine a classroom where every student can instantly ask every other student a question and weight their answers by how relevant they are. A recurrent network is like passing a note around the room one student at a time: by the end the message is garbled. A convolution is like only being allowed to talk to your immediate neighbors. Attention skips all that: any student can reach any other student in one step. That direct reach is what we call attention.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Define Q/K/V and the softmax(QKᵀ/√d_k)V formula, contrast O(1) attention path vs O(n) recurrence vs O(log n) convolution, and explain why one hop gradient flow is the architectural reason transformers scaled.
import torch, torch.nn.functional as F
def scaled_dot_product_attention(Q, K, V):
# Q, K, V: (batch, n, d_k)
d_k = Q.size(-1)
scores = Q @ K.transpose(-2, -1) / (d_k ** 0.5)
weights = F.softmax(scores, dim=-1)
return weights @ VReal products, models, and research that use this idea.
- The Vaswani et al. 2017 paper 'Attention Is All You Need' replaced recurrence entirely in the Transformer encoder and decoder.
- GPT-1/2/3/4 and Llama-3 use stacks of causal self-attention layers as their sole sequence mixing primitive.
- BERT uses bidirectional self-attention with masked language modeling pretraining.
- Vision Transformers (ViT) demonstrated that the same attention primitive replaces convolution in images when given enough data.
What an interviewer would ask next. Try answering before peeking at the approach.
QIf attention has O(1) path length between any two tokens, why do we still stack many attention layers in practice?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Saying attention 'is parallel matmul', true, but that's a consequence of the architecture, not its defining property. The defining property is the direct pairwise gradient path.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.