β the notes that actually get asked in interviews β
#AI-ML-2026#must-know#concepts-not-code
β Why did we need Attention? (RNN's problem)
Before Transformers, sequence models = RNNs / LSTMs. They read a sentence one word at a time, carrying a "memory" (hidden state) forward.
fig 1 β the sequential bottleneck
Long-range dependency loss β word 1 barely influences word 50's understanding; the hidden state is squeezed through every step.
Vanishing/exploding gradients β backprop through many time-steps makes gradients shrink or blow up.
No parallelism β step t needs step tβ1's output, so GPUs sit idle; training is slow on long sequences.
Fixed bottleneck vector β in seq2seq, the whole input sentence had to be squashed into one final context vector before decoding β a huge compression loss for long sentences.
π‘ the fix
Let the decoder "look back" at all encoder words directly, and learn which ones matter right now β that's Attention. Transformers (Vaswani et al., 2017) took this idea and threw away recurrence entirely: "Attention is All You Need."
interview line: "Attention removes the sequential bottleneck & lets every token see every other token directly, in parallel"
β‘Self-Attention β the Query/Key/Value intuition
Think of it like a library search, done by every word, for every word:
Query (Q) β "what am I looking for?" β the word asks a question about context
Key (K) β "what do I contain?" β a label each word attaches to itself, advertising its content
Value (V) β "here's my actual content" β what actually gets passed along once picked
Every word compares its Query against every other word's Key (a dot-product "match score"), turns those scores into weights (softmax), then takes a weighted average of all Values β that weighted mix is the word's new, context-aware representation.
fig 2 β Q from "IT" matches K of "animal" strongly β output leans on V of "animal"
π in one line
score = QΒ·K (similarity) β softmax β weights β weighted average of V = output. Every word does this for every other word, all at once (matrix ops), which is why it's parallel and captures long-range links in a single step β no chain of 50 hops needed.
β’Multi-Head Attention
One attention "head" learns one kind of relationship (say, subjectβverb). Language has many relationship types at once β so we run several attention heads in parallel, each with its own learned Q/K/V projections, each free to specialise.
fig 3 β heads run in parallel β concatenated β projected back down
why it works: 1 head = 1 view; 8 heads = 8 simultaneous views, richer representation
β£Positional Encoding β why order matters
Self-attention on its own is a "bag of words" β it computes weighted sums with no idea of order. "Dog bites man" and "Man bites dog" would look identical to raw attention!
Fix: inject a unique position signal (built from sine/cosine waves of different frequencies, or learned) and add it to each word's embedding before attention β so the model gets both "what" and "where" at once.
fig 4 β position encoding gives attention a sense of sequence
β€The Full Transformer β EncoderβDecoder
Original Transformer (built for translation) = a stack of Encoders feeding a stack of Decoders.
fig 5 β encoder = understand, decoder = generate (with a bridge between them)
βοΈ 3 flavours of attention here
Encoder self-attention β bidirectional, sees full input
Chatbots, text completion, summarization, code generation, open-ended writing
Analogy
A reader who highlights & studies a whole page at once
A writer composing a sentence word by word, never peeking ahead
gotcha to remember: BERT can't generate text (no causal masking = would cheat by seeing the answer); GPT can still "understand" via next-token prediction, just less directly than BERT