WHY: Raw tokens discrete symbols hote hain. Humein continuous vectors chahiye jo dono meaning (embedding) aur position (kyunki attention mein inherently koi order nahi hota) capture karein.
Formula Derivation:
InputRepresentation(x,pos)=Embed(x)+PE(pos)
Positional encoding sine/cosine functions use karta hai:
PE(pos,2i)=sin(100002i/dmodelpos)PE(pos,2i+1)=cos(100002i/dmodelpos)
WHY ye specific functions?
Sinusoids model ko relative positions seekhne dete hain: PE(pos+k) ko PE(pos) ke linear function ke roop mein express kiya ja sakta hai
Alag-alag frequencies (100002i/dmodel ke through) alag-alag scales par position encode karti hain
Training ke dauran unseen sequence lengths tak extrapolate karta hai
Similarity scores compute karo: QKT∈Rn×n (har token har token ko attend karta hai)
dk se scale karo WHY? Dot products dimension ke saath badhte hain; bade values softmax ko saturation regions mein push karte hain jahan gradients tiny hote hain
Row-wise softmax apply karo: attention weights har query ke liye 1 tak sum hote hain
Values ka weighted sum: har token ka output sabhi tokens ke value vectors ka weighted combination hota hai
Multi-head attention h=8 attention operations parallel mein run karta hai:
MultiHead(Q,K,V)=Concat(head1,…,headh)WOheadi=Attention(QWiQ,KWiK,VWiV)
Har head ka dk=dv=dmodel/h=512/8=64 dimensions hote hain.
WHY multiple heads? Alag-alag heads alag-alag types ke relationships (syntactic, semantic, positional) seekhte hain. Ek head subject-verb agreement par focus kar sakta hai, doosra long-range dependencies par.
WHY? Attention linear hai (weighted sums). FFN non-linearity add karta hai aur model capacity badhata hai. Har position par independently apply hota hai (same network positions ke across, alag positions yahan interact nahi karte).
Har sub-layer ke around Residual Connection + Layer Norm:
LayerNorm(x+Sublayer(x))
WHY residuals? Deep networks (6 layers = encoder mein 12 sub-layers) mein gradient flow enable karte hain. Residuals ke bina, gradients vanish ho jaate hain.
WHY LayerNorm? Training stabilize karta hai har example ke liye features ke across normalize karke:
LayerNorm(x)=σ2+ϵx−μ⋅γ+β
jahan μ,σ2 har token ke liye dmodel dimensions ke over compute hote hain.
Encoder self-attention jaisa hi lekin causal masking ke saath:
maskij={0−∞if i≥jif i<j
Softmax se pehle apply hota hai: softmax(QKT/dk+mask)
WHY? Training ke dauran, decoder puri target sequence dekhta hai. Masking position i ko future positions j>i attend karne se rokta hai, autoregressive generation ensure karta hai (sirf past tokens use karke next token predict karo).
WHY? Yahan decoder source sequence access karta hai! Decoder encoder ki input representation ko query karta hai. Translation "Le chat" → "The cat" ke liye, "cat" generate karte waqt decoder encoder output mein "chat" ko attend karta hai.
Parallelization: Sabhi positions simultaneously process hote hain (sequential RNNs ke mukable)
Constant path length: Koi bhi token pair directly attention ke through connected hai (RNN mein O(n) steps ke mukable)
Learnable relationships: Attention weights data ke according adapt hote hain (fixed CNN kernels ke mukable)
Scalability: Compute badhne par aur layers/heads/dimensions stack karo
Recall Ek 12-Saal Ke Bachche Ko Explain Karo
Socho tum ek lambi kahani padh rahe ho. Tumhara brain ek word padhta hai, use bhool jaata hai, phir agla padhta hai — aisa nahi hota. Tumhari aankhein jump kar sakti hain — tum ek word padh sakte ho, phir shuru ki koi cheez yaad kar sakte ho, phir end se connect kar sakte ho. Transformers yehi karte hain!
Purane AI models (RNNs) aise the jaise ek tiny sliding window se padhna: ek word dekho, agla move karo, lekin pehla bhool jao. Transformers POORA sentence ek saath dekhte hain. Har word har doosre word ko dekhta hai aur decide karta hai "Is word ko samajhne ke liye mujhe us word par kitna dhyan dena chahiye?"
Do parts hain: Encoder (input sentence padhta hai, jaise French mein "Le chat noir") aur Decoder (output likhta hai, jaise "The black cat"). Encoder figure out karta hai ki sab kuch kya matlab rakhta hai. Decoder jawab banata hai, lekin ek special trick ke saath: word3 likhte waqt, ye sirf words 1 aur 2 dekh sakta hai (future words nahi dekhna!) — bilkul jaise tum apni diary likhte waqt kal nahi dekh sakte.
Jadui ingredient hai attention — ek formula jo "Word A aur Word B kitne related hain?" compute karta hai har pair ke liye. Ye lakhon baar training ke dauran karo, aur AI language patterns seekh leta hai!
Transformer decoder ke har layer mein teen sub-layers kya hain?
(1) Masked multi-head self-attention, (2) Encoder-decoder cross-attention, (3) Position-wise feed-forward network. Har ek mein residual connection + LayerNorm hota hai.
Attention formula mein √d_k se scaling kyun use hoti hai?
Dot products dimension d_k ke saath magnitude mein badhte hain. Bade values softmax ko saturation regions mein push karte hain jahan gradients bahut chhote hote hain, learning hindered hoti hai. √d_k se divide karne par variance stable rehta hai.
Decoder mein causal masking ka purpose kya hai?
Position i ko training ke dauran future positions j > i attend karne se rokta hai. Ye autoregressive generation enforce karta hai: token i predict karne ke liye sirf tokens 1 se i-1 use ho sakte hain, future tokens nahi.
Transformer base model mein approximately kitne parameters hain?
~65 million parameters, input embedding, output embedding, aur pre-softmax projection ke beech weight tying use karke.
Original paper mein use ki gayi learning rate warmup schedule kya hai?
lr = d_model^(-0.5) × min(step^(-0.5), step × warmup_steps^(-1.5)) with warmup_steps=4000. 4000 steps tak linear increase, phir inverse square root decay.
Positional encoding add karne se pehle embeddings ko √d_model se multiply kyun karte hain?
Embeddings ki variance ~1 hoti hai, positional encodings bounded [-1,1] hoti hain. √d_model (~22.6 for d=512) se scale karna ensure karta hai ki embeddings initially dominate karein jabki PE positional information add kare bina semantic content ko overwhelm kiye.
Base model mein har attention head ki dimension kya hoti hai?
d_k = d_v = d_model / h = 512 / 8 = 64 dimensions per head.
Learned embeddings ki jagah sinusoidal positional encoding kyun use karte hain?
Sinusoids model ko training ke dauran dekhi gayi sequence lengths se lambi lengths tak extrapolate karne dete hain. PE(pos+k) ko PE(pos) ke linear function ke roop mein express kiya ja sakta hai, jo model ko relative positions seekhne mein help karta hai.
Base model mein FFN inner dimension d_ff kya hai?
2048 (model dimension 512 ka 4 guna). Upar project karta hai, ReLU apply karta hai, wapas neeche project karta hai.
Encoder-decoder attention, self-attention se kaise alag hai?
Cross-attention mein, Q decoder se aata hai, lekin K aur V encoder output se aate hain. Ye decoder ko target sequence generate karte waqt source sequence attend karne deta hai.
Label smoothing ε_ls=0.1 aur vocabulary size K ke saath, true class ko kya target probability milti hai?
1 − ε_ls + ε_ls/K. K=4 ke liye ye hai 0.9 + 0.025 = 0.925; baaki har class ko ε_ls/K = 0.025 milta hai.