4.1.8 · HinglishTransformer Architecture

Feed-forward network sublayers

2,648 words12 min readRead in English

4.1.8 · AI-ML › Transformer Architecture

Feed-Forward Network (FFN) kya hai?

Architecture Deep Dive

Expansion-Contraction Pattern kyun?

Chaliye derive karte hain ki hum pehle expand kyun karte hain phir contract, shuru karte hain uss cheez se jo chahiye:

Goal: Ek non-linear function seekhna jo complex patterns represent kar sake.

Step 1 — Universal Approximation

Universal approximation theorem ke mutabiq, ek two-layer network jisme kaafi hidden units hon, kisi bhi continuous function ko approximate kar sakta hai. Lekin kitne units chahiye?

Step 2 — Information Bottleneck

Agar ho, toh ek bottleneck hai—limited capacity. Hidden layer ko complex combinations capture karne ke liye input se zyada features represent karne padte hain.

Step 3 — The 4x Rule

Empirically, achha kaam karta hai. Kyun?

  • Expansion: 4x zyada neurons ka matlab hai ki hum alag feature combinations compute kar sakte hain
  • Sparsity: ReLU activations sparse hote hain (~50% neurons active), toh effective capacity hoti hai
  • Compression: Second layer wapas project karta hai, network ko useful high-dimensional features seekhne par majboor karta hai

Activation Functions: ReLU → GELU → SiLU

Sirf ReLU kyun nahi?

Shuru karte hain ReLU (Rectified Linear Unit) se:

Problem: Zero par hard cutoff dead neurons create karta hai (gradient = 0 for ).

Figure — Feed-forward network sublayers

GELU ki taraf evolution:

Intuition: Input ko us probability se multiply karo ki woh ek random Gaussian se bada hai. Chhoti negative values ko chhota lekin non-zero gradient milta hai (smooth!).

Approximation:

Modern SiLU/Swish:

Better kyun? Har jagah smooth hai, self-gated hai (output input magnitude par depend karta hai), empirically large models ke liye zyada powerful.

Position-Wise = Cross-Position Mixing Nahi

Mathematical statement: Sequence ke liye jahan :

Har same weights use karta hai lekin different input data ke saath.

Transformer Block Mein Role

Complete encoder block:

Yeh order kyun?

  1. Pehle Attention: Context aggregate karo (linear mixing)
  2. Phir FFN: Aggregated context se non-linear patterns extract karo
  3. 12-24 baar repeat karo: Har layer representations ko aur refine karta hai

Common Mistakes

Modern Variations

GLU Variants (Gated Linear Units)

Recent models (PaLM, LLaMA) standard FFN ko gated variants se replace karte hain:

Jahan element-wise multiplication hai.

Gating kyun?

  • ek learned gate ki tarah kaam karta hai (kaun se features pass karne hain)
  • features compute karta hai
  • Gate × Features = selective amplification
  • Benefit: Same performance ke liye 15-20% kam parameters (gates implicit regularization provide karte hain)

Mixture of Experts (MoE)

Single FFN ko multiple expert FFNs + router se replace karo:

Kyun?

  • Har expert specialize karta hai (jaise Expert 1 = syntax, Expert 2 = semantics)
  • Sparse activation: sirf top-k experts har token ke liye compute karte hain
  • Benefit: 10x parameters, 1.5x compute (zyaadaatar experts har token ke liye dormant rehte hain)

Connections

  • Multi-Head Attention: Attention information mix karta hai, FFN use process karta hai
  • Residual Connections: FFN output input mein add hota hai, deep networks enable karta hai
  • Layer Normalization: Residual ke baad apply hota hai, FFN training stabilize karta hai
  • Positional Encoding: FFN position encode nahi karta—input ke positional encoding par rely karta hai
  • Transformer Encoder Block: FFN attention ke baad second sub-layer hai
  • Activation Functions: Choice (ReLU/GELU/SiLU) performance par significantly impact karti hai
  • Universal Approximation Theorem: Two-layer FFN structure ko justify karta hai
  • Batch Normalization vs Layer Normalization: Sequences ke liye BatchNorm ki jagah LayerNorm use hota hai

Flashcards

#flashcards/ai-ml

Transformers mein feed-forward network sublayer kya hai? :: Ek position-wise fully connected network jo har token par independently apply hota hai, do linear layers se bana hota hai jiske beech ek non-linear activation hota hai, typically dimension 4x expand karta hai phir wapas project karta hai.

FFN model dimension se 4x tak expand kyun karta hai?
Complex non-linear patterns seekhne ke liye ek richer feature space create karne ke liye. Expansion universal approximation ke liye enough capacity provide karta hai, jabki ReLU sparsity (~50% active) effective capacity ko ~2x model dimension banata hai.
Standard Transformer FFN ka formula kya hai?
FFN(x) = max(0, xW₁ + b₁)W₂ + b₂, jahan W₁ dimensions expand karta hai (d_model → d_ff), activation apply hota hai, aur W₂ wapas project karta hai (d_ff → d_model).
FFN position-wise kyun apply hota hai?
Attention already positions ke beech information mix kar chuka hota hai. FFN position-independent feature extraction mein specialize karta hai already-mixed representations par, jo perfectly parallelize hota hai aur redundant cross-position modeling rokta hai.
Transformer parameters ka kitna percentage FFN mein hota hai?
Har layer ke parameters ka approximately 2/3. d_ff = 4×d_model ke saath, FFN ke paas ~8d²_model parameters hote hain vs attention ke ~4d²_model.
Models ReLU se GELU se SiLU ki taraf kyun gaye?
ReLU mein hard cutoff hai (x<0 ke liye dead neurons). GELU activation ko Gaussian probability weighting se smooth karta hai. SiLU (x·σ(x)) self-gated, har jagah smooth hai, aur empirically large models ke liye best perform karta hai.
FFN aur regular MLP mein kya fark hai?
FFN same network ko har sequence position par independently apply karta hai (1x1 convolution ki tarah), jabki MLP poore input ko ek single vector ki tarah process karta hai. FFN mein position-wise weight sharing hota hai.
GLU variant kya hai aur ise kyun use karte hain?
Gated Linear Unit: FFN_GLU(x) = (σ(xW₁) ⊙ xW₃)W₂, jahan ek path doosre ko gate karta hai. Learned selective amplification ke through same performance ke liye 15-20% parameter savings provide karta hai.
Recall Ek 12-saal ke bacche ko samjhao

Socho tum class mein ho aur teacher sabse ek interesting fact share karne ko kehti hai. Yeh attention ki tarah hai—sabne suna kya logon ne kaha aur apne dimaag mein un ideas ko mila liya.

Ab, har student ghar jaata hai aur un saari baaton ke baare mein gehraai se sochta hai jo sunni thi, unhe apni pehle ki jaankari se jodata hai. Shayad tune suna "dogs loyal hote hain" aur "wolves pack mein shikaar karte hain," aur ghar par tune realize kiya "oh, dogs wolves se evolve hue, isliye woh social hote hain!" Woh gehri soch akele ghar par hi feed-forward network hai.

Tu sochte waqt apne classmates ko call nahi karta (doosron ke saath koi mixing nahi). Tu sirf woh process karta hai jo tune seekha. Phir kal, tu class mein wapas aata hai behtar samajh ke saath, aur cycle repeat hoti hai.

"4x expansion" waise hai jaise ghar par sochte waqt 4x zyada brain space milna—tu bahut zyada connections aur ideas consider kar sakta hai apna final thought class mein wapas laane se pehle.

Concept Map

positions ke beech info mix karta hai

sirf linear weighted averages

provided by

har token par apply

structure

W1 expansion

activation sigma

W2 projection

typical ratio

justified by

ReLU sparsity ~50%

params dominate ~8 d_model squared

Attention sublayer

Feed-forward network

Need for non-linearity

Position-wise processing

Expand then contract

Hidden layer d_ff

ReLU / GELU / SiLU

Output d_model

d_ff = 4x d_model

Universal approximation

Effective capacity ~2x

Parameter count