5.6.3 · D4 · HinglishMachine Learning (Aerospace Applications)

ExercisesRegularization — L1 (lasso), L2 (ridge), dropout

3,326 words15 min read↑ Read in English

5.6.3 · D4 · Coding › Machine Learning (Aerospace Applications) › Regularization — L1 (lasso), L2 (ridge), dropout

Yeh page Regularization — L1, L2, dropout ke liye ek graded workout hai. Yahaan use kiye gaye har symbol ko parent note mein build kiya gaya hai; agar koi bhool jaaye, wapas jump karo. Har problem ko uska solution kholne se pehle solve karo — poora point yahi hai ki apne khud ke gaps pakdo.

Quick symbol refresher, taaki pehli line samajh aa sake:

Recall Woh chhe symbols jo tumhe chahiye
  • (ya ) ::: ek weight — ek tunable number jo model kisi feature se multiply karta hai.
  • (eta) ::: learning rate — gradient descent kitna bada step leta hai.
  • (lambda) ::: regularization strength — hum bade weights ko kitna hard punish karte hain.
  • ::: data gradient — plain (unpenalized) loss ka slope.
  • ::: dropout probability — training ke dauran ek neuron ke switch off hone ka chance.
  • ::: dropout mask — ek coin-flip variable, , jo probability ke saath (neuron rakhna) aur probability ke saath (drop karna) hota hai.

Woh chaar update rules jin par neeche sab kuch tika hua hai. Har ek ke saath why padho — penalty ki shape hi rule hai:


Level 1 — Recognition

Exercise 1.1 (L1 · Recognition)

Har phrase ke liye technique ka naam batao (L1 / L2 / dropout): (a) "weights ke squared magnitude ko penalize karta hai" (b) "training ke dauran randomly neurons ko off switch karta hai" (c) "bahut saare weights ko exactly zero par drive karta hai" (d) "weight decay bhi kehte hain ise"

Recall Solution

(a) L2 (ridge) — squared term . (b) dropout — activations ki Bernoulli masking. (c) L1 (lasso) — diamond corners zeroed coordinates par land karte hain. (d) L2 (ridge) factor har step mein weights shrink karta hai, yaani decay.

Exercise 1.2 (L1 · Recognition)

Ek network test time par hai. Standard inverted dropout ke under, activations ke saath kya karte ho?

Recall Solution

Kuch nahin. Test time full network use karta hai bina mask aur bina scaling ke, kyunki scaling training ke dauran already apply ho chuki thi. Toh .


Level 2 — Application

Exercise 2.1 (L2 · Application)

, , weight , aur data gradient diya gaya hai. Ek L2 update step compute karo.

Recall Solution

use karo.

  • Decay factor: .
  • . Weight dono se shrank: decay se () aur data gradient se ().

Exercise 2.2 (L2 · Application)

Same , , lekin ab data gradient band karo (). se shuru karke, hone mein kitne steps lagte hain? (Yeh dikhata hai ki pure decay geometric hoti hai.)

Recall Solution

Bina data gradient ke, har step se multiply karta hai: . Chahiye . Kyunki poore steps count karta hai, ki value matlab hai step 27 abhi enough nahin — humein upar round karna hoga next integer par: steps. (Jab "ek threshold tak kitne discrete steps" solve karo toh hamesha ceiling lo — fractional step exist nahin karta.) Isliye L2 weight shrink karta hai lekin kabhi zero nahin karta — geometric decay zero approach karta hai bina pahunche. (Note: yeh tab hi hold karta hai jab ; agar toh geometric picture break ho jaati hai — Exercise 5.2 dekho.)

Exercise 2.3 (L1 · Application)

, diya, toh . Har weight par soft-threshold rule apply karo (pure penalty step): .

Figure — Regularization — L1 (lasso), L2 (ridge), dropout
Recall Solution

Rule: .

  • : .
  • : , , sign .
  • : .
  • : , sign . Result: . Do weights exactly kill ho gaye, bacche hue same constant se shrank.

Figure s01 (described): horizontal axis incoming weight hai, vertical axis uski new value hai. Ek dashed black diagonal "do nothing" identity line hai (). Red curve soft-threshold output hai: yeh poore band par (shaded red — "dead zone") zero par stuck flat line hai, phir identity line ke parallel split hoti hai lekin zero ki taraf shifted. Red band ke andar land hone wala koi bhi weight exactly zero par snap ho jaata hai; bahar wala survive karta hai lekin closer to zero pull hota hai.


Level 3 — Analysis

Exercise 3.1 (L3 · Analysis)

Do weights par equal start karte hain. Ek L2 se train hota hai ( per step), doosra L1 se (constant per step, soft-threshold). Data gradient ignore karo. Exactly 6 steps ke baad, kaun sa chhota hai, aur kaun sa (agar koi) zero hai?

Figure — Regularization — L1 (lasso), L2 (ridge), dropout
Recall Solution

L2: — abhi bhi positive, kabhi exactly zero nahin. L1: har step subtract karo: 6 steps ke baad hum subtract karte, lekin pehle zero hit hota hai.

  • . Yeh subtractions hain, step 6 par exactly par land karte hain. Toh L1 chhota hai (=0); L2 hai. Yahi poori story hai: L1 sparsify karta hai, L2 sirf shrink karta hai.

Figure s02 (described): horizontal axis training step hai (0 se 6), vertical axis weight value hai. Ek black curve (circles) L2 hai: yeh bend karta hai, har point pichle ka , flatten hota jaata hai zero line approach karte karte — lekin kabhi pahunchta nahin. Ek red line (squares) L1 hai: ek perfectly straight downhill ramp jo har step drop karta hai, exactly par zero axis par strike karta hai, jahaan ek red dot landing mark karta hai. Straight red ramp vs bending black curve ek picture mein poora L1-vs-L2 contrast hai.

Exercise 3.2 (L3 · Analysis)

Constraint-region picture use karke explain karo, kyun L1 coordinates zero karta hai lekin L2 nahin karta. Diamond vs circle reference karo.

Figure — Regularization — L1 (lasso), L2 (ridge), dropout
Recall Solution

Regularization = data loss minimize karo subject to weights par ek budget. Budget region hai:

  • L2: — ek smooth circle (koi special points nahin).
  • L1: — ek diamond jiske corners axes par baithe hain (ek coordinate ). Data-loss contours ellipses hain jo tab tak expand karte hain jab tak pehli baar budget region ko touch na kar lein. Ek smooth circle generic point par touch hota hai → dono weights nonzero. Ek diamond ka sharp corner se touch hone ki highest probability hai → ek weight exactly zero. Yahi corner-catching geometric sparsity hai.

Figure s03 (described): origin par centred do black budget shapes hain — ek solid circle (L2) aur ek dashed diamond jiske chaar corners exactly aur axes par baithe hain (L1). Off to the right faint black concentric ellipses hain, data-loss contours, apne centre se baahir grow karte hue. Jahan growing ellipse pehli baar diamond se milti hai wahan ek red dot -axis par bilkul baithe hai, yaani — ek sparse solution. Corner baahir reach karke contour "pakad" leta hai; smooth circle off-axis point par graze hota jahan dono weights nonzero hote.


Level 4 — Synthesis

Exercise 4.1 (L4 · Synthesis)

Ek hidden layer activation output karta hai. Tum inverted dropout ke saath use karte ho. (a) Training ke dauran, jab neuron kept hai, kaun si value aage flow karta hai? (b) Verify karo ki expected training activation raw ke barabar hai. (c) Test time par kya aage flow karta hai?

Recall Solution

Keep probability . Inverted rule: kept value . (a) . (b) . ✓ Expectation preserved hai — training ke dauran scale up karne ka yahi poora reason hai. (c) Test time koi mask nahin, koi scaling nahin use karta: .

Exercise 4.2 (L4 · Synthesis)

Tumhe 50 candidate features se aircraft drag predict karna hai, lekin tum jaante ho ki sirf kuch physically relevant hain aur tum chahte ho ki model tumhe bataye kaun se hain. Tum survivors par smooth, stable coefficients bhi chahte ho. Kaun sa regularizer(s) choose karoge, aur kyun? cross-validation ke saath kya tune karoge?

Recall Solution

L1 (ya Elastic Net = L1 + L2) use karo:

  • L1 automatic feature selection perform karta hai — irrelevant 45-ish sensors zero kar deta hai, feature selection free mein karta hai aur interpretable results deta hai.
  • Ek small L2 term add karna surviving coefficients stable rakhta hai jab features correlated hain (pure L1 arbitrarily do correlated sensors mein se ek pick karta hai). (aur L1/L2 mix) ko cross-validation se tune karo: sweep karo, woh value pick karo jo validation error minimize kare. Confirm karo ki overfit nahin ho rahe overfitting detection se (train-vs-validation gap).

Level 5 — Mastery

Exercise 5.1 (L5 · Mastery)

Ek fault-detection network overfit kar raha hai: train accuracy 99%, test 78%. Tum fully-connected layers par dropout add karte ho aur train 92%, test 89% milta hai. (a) Generalization gap (train − test) kitne points se shrank? (b) Training accuracy giri. Explain karo yeh expected kyun hai, bug nahin. (c) Connections list se ek alternative regularizer aur ek complementary diagnostic ka naam lo.

Recall Solution

(a) Pehle: gap pts. Baad mein: gap pts. Shrinkage points. (b) Har training step ab ek random sub-network run karta hai, toh model exact examples memorize nahin kar sakta — training accuracy zaroor giregi. Hum thoda training fit trade karte hain ek bade test gain ke liye: regularization ka poora objective yahi hai. (c) Alternative regularizer: L2 weight decay same fully-connected layers par (ya ensemble methods — dropout khud sub-networks ka ek implicit ensemble hai, toh real ensemble banana generalization same direction push karta hai). Complementary diagnostic: overfitting detection — train-vs-validation accuracy curves plot karo aur gap dekho; isse cross-validation ke saath pair karo taaki dropout rate actually choose kar sako, guess karne ki jagah.

Exercise 5.2 (L5 · Mastery)

Plain gradient-descent update se, L2 "weight decay" form derive karo, aur precisely woh condition state karo par jiske liye decay stable ho (weights blow up ya oscillate na karein).

Recall Solution

Total loss se shuru karo (sum sirf weights par — bias convention se exclude hai). ke w.r.t. iska gradient: (woh exponent ka cancel karta hai — isliye hum wahan rakhte hain). mein plug karo: Factor weight decay hai. Pure-decay part ki stability : ratio ke saath geometric.

  • Monotonically ki taraf decay karta hai jab .
  • Boundary : , toh weight ek hi step mein exactly zero par collapse ho jaata hai — usable lekin drastic (poora decay, old value ki koi memory nahin).
  • Agar toh : weight har step sign flip karta hai (shrink hote hue oscillate); agar toh aur yeh diverge karta hai. Toh clean shrinkage ke liye rakho; ko knife-edge treat karo aur usse upar kuch bhi unsafe maano.
Recall Self-check summary

L1 vs L2 ek line mein? ::: L1 ek constant subtract karta hai (→ sparsity), L2 se multiply karta hai (→ smooth shrinkage, kabhi zero nahin). kya hai aur kyun matter karta hai? ::: Formally undefined (subgradient mein kahin bhi); optimizers ise set karte hain taaki zero par pahuncha weight wahan ruk jaaye — isliye soft-thresholding safe form hai. Test time par dropout? ::: Kuch nahin — inverted dropout ne training ke dauran already scale kar diya. Training ke dauran se divide kyun karte hain? ::: Expected activation preserve karne ke liye () taaki test par koi rescaling na chahiye. L2 stability range? ::: Clean decay ke liye ; weight ko ek step mein zero par collapse karta hai; diverge karta hai. Kya hum bias regularize karte hain? ::: Nahin — convention se penalty se exclude hai kyunki yeh shift karta hai, scale nahin, aur overfitting cause nahin kar sakta.