Saral Shiksha Yojna
Courses/Computer Vision

Computer Vision

CSE471
Prof. Makarand Tapaswi + Prof. Charu SharmaSpring 2025-264 credits

Attention Mechanism & Transformer Architecture

NotesStoryCaveman
Unit 10 — Attention & Transformers

Old whisper break when sentence too long

Long ago, tribe turn English into French with two magic boxes. First box read all English, squeeze it into ONE shiny stone. Second box hold that stone, grunt French words one by one. Ugg call this the Seq2Seq bottleneck — whole sentence crushed into single fixed vector. But one small stone cannot hold big hunt-story! "Cat that sat on mat" and "cat sat on mat" cannot both fit same stone. Long sentence, box forget, bad translate. This the sickness. Attention the cure.

Three cave-paintings that change everything

Ugg carve three names on wall, remember the chain:

  • Bahdanau 2015 ("Jointly Learning to Align and Translate") — first stick attention onto RNN as add-on.
  • Xu 2015 ("Show, Attend and Tell") — same attention, point at pictures, make captions.
  • Vaswani 2017 ("Attention Is All You Need") — throw RNN in fire! Whole box now only attention. This the Transformer. Every modern beast — ViT, GPT, PaliGemma — child of this paper.

Three hunt-tasks Ugg must name: image captioning (picture in, words out), sentiment classification (words in, one label out), machine translation (words in, words out).

How box read and write

Encoder RNN read source, make hidden stones . Old way keep only last stone . Decoder RNN start from it, spit one token, feed own token back, spit next — one at a time. This called autoregressive, a language model .

Attention — decoder look back at ALL stones

Bahdanau say: no crush everything in one stone! Every step, decoder LOOK BACK over all encoder stones, grab what it need. Four steps by the fire:

1. Alignment score — how good is stone for this step? $$ This a tiny brain (additive MLP) that sniff decoder stone and encoder stone together, grunt one number.

2. Softmax squash numbers into weights that sum to one: $$ These the attention weights.

3. Context vector — mix all encoder stones by their weight: $$ Big weight where it matter, small where not.

4. Combine with decoder stone, guess next word.

Magic: paint the weight map and it glow near-diagonal — French "cat" light up on English "cat" stone. Nobody teach this alignment! It just emerge. Guaranteed fire-story for exam.

Soft vs hard. Soft attention = smooth weighted mix, leave footprint (differentiable). Hard attention = pick ONE stone by dice-roll, no smooth footprint, must train with REINFORCE. Soft win, become standard.

Fire-side training tricks

Teacher forcing — feed decoder the TRUE last word in training. Fast, but breed exposure bias (at real hunt, decoder only ever see its own words). Student forcing — feed decoder its OWN last guess. Harder train, but honest. Ugg warn: at inference, ALWAYS student forcing — no true word exist yet!

Greedy = grab biggest token each step, quick but short-sighted. Beam search = keep top- trails alive (say ), grow all, keep best again, at end pick best whole trail. More fire-wood, better words.

Softmax temperature: . normal, big flat (wild), small peaky (sure).

The Transformer — throw RNN in fire

RNN slow: step 5 must wait step 4. Serial. If attention already look anywhere, why not let EVERY token look at EVERY token, all at once, no recurrence? That the Transformer. Old paper: encoder blocks + decoder blocks.

Self-attention the engine. Take input , make three roles with learned rock-matrices — Query (what I hunt?), Key (what I offer?), Value (what I carry?): , , . Then scaled dot-product attention: $QK^\topn\times nV$ = each token grab weighted mix of values.

**Why divide by ?** Ugg gold! Sum of unit-variance products has variance , so std . For , std — too big, softmax SATURATE, one logit eat all, gradient die. Divide by pull variance back to 1. Ugg warn: divide by , NOT !

Multi-head attention: run small heads, each , concat, project with . Each head sniff different bond — one grammar, one who-is-who, one local. Ugg WARN loud: more heads NO make more stones! together always cost no matter . Heads split the room, not add room.

Three kinds of look, and order-stones

Three flavours: encoder self-attention (Q,K,V all from input, no mask, look both ways); decoder masked self-attention (Q,K,V from output-so-far, causal mask stop peek at future); cross-attention (Q from decoder, K AND V from encoder — this Bahdanau in Q-K-V clothes!).

Causal mask = upper-triangle full of , so after softmax those become 0 — token only see tokens . Ugg warn: forget this and decoder cheat, copy future, fail at real hunt. Same-thing names: look-ahead, autoregressive, left-to-right. Another warn: cross-attention take BOTH K and V from encoder — not K from decoder!

Positional encoding. Self-attention is permutation-equivariant — shuffle tokens, output shuffle same way. It cannot tell "cat sat" from "sat cat"! Fix: add sinusoidal positional encoding to input embeddings: $\text{PE}(\text{pos}+k)\text{PE}(\text{pos})$, so brain read relative distance easy. Ugg warn: add PE to the input BEFORE block 1, not after! Sinusoid stretch to longer sentence; learned PE does not.

The block, and eye-that-look-wrong

One encoder block: ; . That "" mean add back the old stone (residual/skip connection) so gradient flow. Encoder block = 2 sub-layers (attention + MLP); decoder block = 3 (masked-attention + cross + MLP). MLP swell — the 4× rule. Old 2017 paper use Post-Norm ; modern use Pre-Norm for stable deep stack. Ugg warn: original was Post-Norm, NOT Pre!

Show, Attend and Tell: picture → CNN → grid of spot-features, LSTM attend over spots for each word, heatmap show where model "look". But careful — heatmap show WHERE eye point, not that eye SEE right. Red-light trick fool it.

Why Transformer beat everyone

  • Parallel — all token bonds at once, GPU happy, way faster than RNN.
  • Short path — every token touch every other in ONE layer, no long-distance forget (cost: pairs, the bill Flash Attention pay later).
  • Universal — same beast eat text, image (ViT), sound, video, even DNA — just change tokenise.

Ugg remember

  • One-stone bottleneck the disease; attention (look back at ALL encoder stones, weighted by softmax) the cure. Chain: Bahdanau 2015 → Show-Attend-Tell 2015 → Vaswani 2017 Transformer.
  • Scaled dot-product — divide by (NOT ) to stop softmax saturate.
  • Multi-head cost SAME stones as single-head (); heads split , not multiply. Encoder block 2 sub-layers, decoder block 3, MLP is .
  • Three looks: encoder self, decoder masked-self (causal mask stop future-peek), cross (Q from decoder, K+V from encoder = Bahdanau).
  • Self-attention blind to order → add sinusoidal PE to the input; sinusoid stretch to long, learned PE not. At inference, always student-forcing.
End of storyUnit 10 — Attention & Transformers · Attention Mechanism & Transformer Architecture