What is the attention mechanism, in one sentence, and what makes it structurally different from convolution and recurrence?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
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.