4.1.4 · HinglishTransformer Architecture

Scaled dot-product attention

2,151 words10 min readRead in English

4.1.4 · AI-ML › Transformer Architecture

Scaled Dot-Product Attention Kya Hai?

Components breakdown:

  1. : Compatibility scores (har query kitna acha match karti hai har key se)
  2. : Scaling taaki gradient saturation na ho
  3. softmax: Scores ko probability distribution mein convert karta hai (weights ka sum 1 hota hai)
  4. se multiply karo: Attention weights ke basis par values ka weighted sum
Figure — Scaled dot-product attention

First Principles Se Derivation

Step 1: Similarity Ke Liye Dot Product Kyun?

Fundamental sawaal se shuru karo: Hum kaise measure karein ki query , key se match karti hai ya nahi?

Dot product alignment measure karta hai:

  • Agar aur same direction mein point karein: badi positive value
  • Agar orthogonal hain: zero
  • Agar opposite hain: badi negative value

Ye step kyun? Humein ek differentiable similarity metric chahiye. Dot product computationally cheap hai () aur embedding spaces mein semantic similarity naturally capture karta hai.

Step 2: Batch Processing Ke Liye Matrix Form

queries aur keys ke liye:

jahan query aur key ke beech compatibility score hai.

Ye step kyun? Modern hardware (GPUs/TPUs) matrix multiplication ke liye optimized hai. Saari queries ek saath batch karna looping se 100x faster hai.

Step 3: Scaling Problem

Observation: Agar aur ke entries se draw ki gayi hain, tab:

Toh standard deviation ke roop mein grow karta hai! Typical ke liye, dot products ka std hota hai.

Ye kyun matter karta hai: Softmax hai . Jab inputs ki magnitude badi ho:

  • Badi positive values: softmax one-hot ho jaata hai (gradient )
  • Relative differences shrink ho jaate hain: seekhna mushkil ho jaata hai ki kaunsi key thodi si better hai

Fix: se divide karo taaki variance wapas 1 ho jaaye:

Step 4: Softmax Normalization

Scores ko attention weights mein convert karne ke liye row-wise softmax apply karo:

jahan .

Ye step kyun?

  • Probabilistic interpretation: ki har row keys ke upar ek distribution hai ()
  • Differentiability: Backprop ke liye smooth function
  • Competition: Keys compete karti hain — ek ko zyada attend karne ka matlab doosre ko kam attend karna

Step 5: Weighted Value Aggregation

Aakhir mein, values ka weighted average lene ke liye attention weights use karo:

Matrix form mein:

Ye step kyun? Query "sawaal poochti hai," keys "sawaal match karti hain," aur values "jawab deti hain." Hum relevance ke basis par ek soft-blended jawab retrieve karte hain.

Worked Examples

Common Mistakes

Active Recall Flashcards

#flashcards/ai-ml

What is the formula for scaled dot-product attention? ::

Hum ki jagah se kyun scale karte hain?
Kyunki dot product ki variance ke saath grow karti hai, toh std ke saath grow karta hai. se divide karne par variance wapas 1 ho jaati hai, softmax ko uski sensitive gradient region mein rakhta hai.
ka shape kya hoga agar , hai aur , hai?
(har element query aur key ke beech similarity hai)
Bade ke saath dot product scale na karne par kya hota hai?
Softmax saturate ho jaata hai (almost one-hot ban jaata hai), gradients vanish ho jaate hain, aur model keys ke beech subtle differences nahi seekh sakta.
Attention mechanism mein Q, K, V kya represent karte hain?
Q (query): tum kya dhundh rahe ho; K (key): match karne ke liye kya available hai; V (value): actual content jo retrieve karna hai.
Attention mein softmax output kya represent karta hai?
Keys ke upar ek probability distribution — attention weights jo 1 tak sum karte hain, ye indicate karte hue ki har position ko kitna "dekha" jaaye.
Dot product dusre similarity metrics ki jagah kyun use karte hain?
Computationally efficient (), differentiable, embedding spaces mein alignment naturally capture karta hai, aur hardware-optimized hai (matrix multiplication).
queries aur keys ke saath attention ka output dimension kya hai?
(har query ke liye ek output vector, har ek dimension ka)

Mnemonic

Feynman Explanation

Recall Ek 12-saal ke bacche ko samjhao

Socho tum homework kar rahe ho aur apni textbook se sawaal pooch sakte ho. Lekin tumhari textbook BAHUT BADI hai — hundreds of pages.

Attention ek smart search system jaisi hai:

  1. Query (Q): Tum apna sawaal likhte ho: "Photosynthesis kya hai?"
  2. Keys (K): Textbook ke har page ka ek title/summary hai (jaise "Plants," "Cells," "Energy," "History")
  3. Comparison: Tum check karte ho ki tumhara sawaal har page title se kitna match karta hai. "Plants" page bahut acha match hai (score = 10), "Energy" theek hai (score = 5), "History" barely match karta hai (score = 1).
  4. Scaling Trick: Agar tumhari textbook BAHUT BADI hoti (hazaaron pages), toh scores bahut bade ho jaate (best match ke liye score = 1000). Isse tumhara dimaag kehta "SIRF is ek page ko dekho" aur baaki sab ignore karo. Scaling matlab hai "chalao scores 1-10 ke beech rakhein taaki main multiple pages consider karun."
  5. Softmax: Scores ko percentages mein convert karo: 70% Plants, 25% Energy, 5% History.
  6. Values (V): Har page mein actual content hai (plants, energy, etc. ke baare mein paragraphs)
  7. Output: Tum Plants page ka 70%, Energy page ka 25%, History page ka 5% padhte ho, aur unhe ek jawab mein blend karte ho.

Scaling kyun zaroori hai: Iske bina, tum ek robot ban jaate jo SIRF top match padhta hai aur baaki sab ignore karta hai — chahe second-best page mein bhi useful info ho! Scaling tumhare dimaag ko multiple sources ke liye open rakhti hai.

Connections

  • Multi-Head Attention — parallel mein multiple scaled dot-product attention operations use karta hai
  • Self-Attention — jab Q, K, V sab same sequence se aate hain
  • Cross-Attention — jab Q ek sequence se aata hai, K/V doosre se (encoder-decoder)
  • Softmax Function — normalization function jo scores ko probabilities mein convert karta hai
  • Gradient Descent — stable learning ke liye scaling kyun matter karta hai
  • Positional Encoding — sequence order information provide karta hai (attention permutation-invariant hai)
  • Query-Key-Value Model — conceptual framework jo information retrieval se liya gaya hai
  • Attention Mask — certain positions ko attend karne se rokne ke liye use hota hai (future tokens, padding)

Last updated: 2026-07-01 | Multi-head attention par jaane se pehle ye master karo

Concept Map

matches against

matched by

has variance d_k

std grows sqrt d_k

saturates

causes

prevents

divided by

feeds

weights

weighted sum

Query Q

Compatibility Scores QK^T

Key K

Variance grows with d_k

Large dot products

Softmax saturation

Vanishing gradients

Scale by 1 over sqrt d_k

Softmax weights sum to 1

Value V

Attention Output