Transformers & Attention 🧠

β€” 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.

RNN β€” sequential, one word waits for the last "The" "cat" "sat" "on..." last word by word 50, info from word 1 has faded β€” "vanishing memory" + can't parallelize β†’ slow training (must wait your turn!)
fig 1 β€” the sequential bottleneck
πŸ’‘ 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:

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.

Sentence: "The animal didn't cross the street because IT was tired" β€” what does "IT" refer to? Q: "IT" asks β€” K: "animal" K: "street" K: "tired" 0.7 (high match!) 0.05 0.25 V: "animal" V: "street" V: "tired" weighted sum (mostly "animal") β†’ new vector for "IT" βœ“ resolved!
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.

same sentence, viewed through different "lenses": Head 1 tracks pronouns Head 2 subject–verb link Head 3 nearby adjectives Head 8... long-range topic concat + linear mix β†’ one rich output per word
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.

word embedding + position signature = position-aware input "dog" embed + pos = 2 wave = "dog" @ pos 2 (unique per position, smooth so nearby = similar)
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.

ENCODER stack (Γ—N) Self-Attention (sees whole sentence, both directions) Feed-Forward Layer ↕ repeated N times, + residual/norm encodes input β†’ rich "understanding" vectors passed as K, V DECODER stack (Γ—N) Masked Self-Attention (only looks left / past words) Cross-Attention Q = decoder, K/V = encoder Feed-Forward Layer ↕ repeated N times generates output, one token at a time Input: "Le chat est noir" Output: "The cat is black" encoder understands the source; decoder writes the target, peeking at the encoder via cross-attention at every step
fig 5 β€” encoder = understand, decoder = generate (with a bridge between them)
βš™οΈ 3 flavours of attention here
  • Encoder self-attention β€” bidirectional, sees full input
  • Decoder masked self-attention β€” causal, can't peek at future tokens (else it'd cheat!)
  • Cross-attention β€” decoder's Query meets encoder's Key/Value β€” "translation alignment"

β‘₯BERT vs GPT β€” the classic interview flip side

Both are built only from one half of the original transformer:

BERT = Encoder-only The [MASK] sat looks BOTH ways ↔ predicts masked word using full context GPT = Decoder-only The cat ? looks ONLY left β†’ predicts next word from past words only
fig 6 β€” bidirectional understanding vs left-to-right generation
BERTGPT
Built from Encoder stack only Decoder stack only (self-attn part, no cross-attn since there's no separate encoder)
Attention direction Bidirectional β€” sees full sentence, left & right of a word Unidirectional / causal β€” masked, can only see past tokens
Pre-training task Masked Language Modeling β€” guess the hidden word (+ Next Sentence Prediction) Next-token prediction β€” guess the next word, repeatedly
Core strength Understanding β€” building a deep contextual representation of given text Generation β€” producing new, coherent text token by token
Typical use-cases Classification, NER, sentiment, question-answering (extractive), sentence embeddings 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