Feedforward network — forward pass
5.6.7· Coding › Machine Learning (Aerospace Applications)
"Forward" kyun? Kyunki hum input se output ki taraf jaate hain (prediction direction). Baad mein, backpropagation gradients compute karne ke liye backward jayega. Forward pass woh tarika hai jisse tumhara trained model actually production mein inference run karta hai—yeh waise hi hai jaise airplane ka autopilot sensor readings se control commands compute karta hai.
The Architecture — Hum Kya Compute Kar Rahe Hain?
Layer ke neuron se layer ke neuron tak har connection ka ek weight hota hai. Layer ke har neuron ka ek bias hota hai.
Key insight: Ek neuron apne inputs ka weighted sum compute karta hai, bias add karta hai, phir ek nonlinear activation function apply karta hai. Nonlinearity ke bina, layers ko stack karna ek single linear transformation mein collapse ho jaata (complex aerospace dynamics ke liye useless).
The Forward Pass Algorithm — Step by Step Derivation
Starting Point: Hamare Paas Kya Hai?
Inputs:
- Input vector (sensor readings)
- Weight matrices jahan
- Bias vectors jahan
- Activation functions (aksar ReLU, sigmoid, tanh, ya linear)
Goal: Output compute karna.
Layer-by-Layer Propagation
Step 1: Initialize
Kyun? Layer 0 ki "activation" bas input data hi hai. Hum ise denote karte hain taaki recursion uniform rahe.
Step 2: Har layer ke liye:
(a) Pre-activation compute karo (weighted sum + bias)*
Yeh form kyun?
- Layer ka har neuron compute karta hai
- Matrix form mein: ka shape hai, hai, toh product hai
- Yeh previous layer ke output ka ek linear transformation hai
(b) Activation function element-wise apply karo
Activation kyun? ke bina, poora network yeh ho jaata: jo collapse hokar , ek single affine map, ban jaata. Aerospace systems nonlinear hote hain (stall, shock waves, compressibility)—hume unhe approximate karne ke liye nonlinearity chahiye.
Step 3: Output
Yahan kyun rokein? Final layer ki activation network ki prediction hai. Regression ke liye (continuous control surfaces predict karna), aksar linear (identity) hoti hai. Classification ke liye (fault detection: nominal/critical), softmax ho sakti hai.
Worked Examples — Concrete Calculations
Setup: 2 sensors se thrust adjustment predict karo (Mach number, altitude).
- Input: (Mach 0.8, 10k ft)
- Hidden layer: 3 neurons, ReLU activation
- Output layer: 1 neuron, linear activation
Weights aur biases (pretrained):
Second column ke weights itne tiny kyun hain? Altitude feature (10000) Mach (0.8) ke mukable bahut bada hai. Altitude column par chhote weights contributions ko balanced rakhte hain—isliye practice mein training se pehle hum inputs normalize karte hain.
Forward Pass Computation:
Layer 1 (Hidden):
Step 2a: Pre-activation
Yeh step kyun? Har neuron input features se evidence aggregate karta hai. Neuron 1 ko strong signal milta hai (1.5), neuron 2 ko weak (0.06), neuron 3 negative (-0.84).
Step 2b: Activation (ReLU: )
ReLU yahan kyun? Negative pre-activation ka matlab hai "yeh feature combination is neuron ko fire nahi karta." ReLU use zero kar deta hai, sparse representations create karta hai (bahut saare neurons off). Neuron 3 is input ke liye "dead" hai—usne ek aisa pattern detect kiya jo absent hai.
Layer 2 (Output):
Step 2a: Pre-activation
Yeh step kyun? Output neuron hidden features ko combine karta hai. Positive weights (1.0, 2.0) neurons 1 aur 3 ko amplify karte hain; negative weight (-0.5) neuron 2 ko suppress karta hai. Neuron 3 off hai, toh uska connection (2.0) kuch contribute nahi karta.
Step 2b: Activation (Linear: )
Linear output kyun? Regression ke liye, hume unbounded predictions chahiye. Network thrust adjustment 1.77 N predict karta hai (ya jo bhi units training mein use hue the).
Setup: Fault detection—flight state ko 3 categories mein classify karo: 0=Nominal, 1=Degraded, 2=Critical.
- Input: (airspeed m/s, throttle fraction, angle-of-attack deg)
- Hidden: 4 neurons, tanh activation
- Output: 3 neurons, softmax activation
Weights:
Forward Pass:
Layer 1 — Pre-activation ( row by row compute karte hain):
Har row check kyun karein? Matrix–vector product bas ek row per dot product hai. Inhe explicitly karne se sign errors pakad mein aate hain—jo bugs ka ek common source hai.
Step 2b: Activation (tanh)
Tanh kyun? Output range activations ko zero ke around center karti hai, gradient flow mein help karta hai. Aerospace ke liye, negative activations "opposite conditions" encode kar sakti hain (e.g., pitch-up vs pitch-down). Notice karo neuron 4 ke paas saturate ho jaata hai.
Layer 2 — Pre-activation ():
Softmax activation:
Sum , toh:
Interpretation: Network predict karta hai 97.8% probability of Nominal flight state (class 0), 1.5% Critical, 0.8% Degraded. Argmax class 0 hai.
Softmax kyun? Yeh arbitrary scores (logits) ko ek valid probability distribution mein convert karta hai (sab milake 1, sab positive). Classification ke liye critical hai—pilot ko raw scores nahi, confidence levels chahiye.
Computational Complexity — Real-Time Systems Mein Kyun Matters Hai
Flops per layer: Layer ke liye:
- Matrix multiply : multiplications + additions flops
- Bias add: additions
- Activation: function evaluations (ReLU: 1 flop each; tanh: ~10 flops)
Total forward pass: flops, jahan activation cost hai.
Example: Ek 10-100-100-5 network ke liye (10 inputs, DO hidden layers of 100, 5 outputs):
- Layer 1: flops
- Layer 2: flops
- Layer 3: flops
- Total: ~23k flops
Aerospace mein yeh kyun matters hai: Flight control computers high frequency par run karte hain (100-1000 Hz). Ek 1 GFlop processor par 23k-flop network 23 microseconds leta hai—1 ms control loop ke andar aaram se. Lekin ek 1000-1000-1000-5 network (2M flops) deadlines miss kar sakta hai. Model size embedded systems ke liye ek design constraint hai.
Common Mistakes — Confusion Ko Steel-Manning Karna
Galat approach: Hamesha output par ReLU apply karo.
Yeh sahi kyun lagta hai: "Nonlinearity ke liye har layer ko activation chahiye."
Fix: Output activation task par depend karta hai:
- Regression (continuous values): Linear (identity) ya kabhi kabhi ReLU agar outputs non-negative hone chahiye (e.g., thrust ≥ 0)
- Binary classification: Sigmoid (outputs probability in [0,1])
- Multi-class classification: Softmax (outputs probability distribution)
Regression output par ReLU apply karna negative predictions ko zero tak clip kar dega—yeh catastrophic hai agar tumhe negative elevator deflection predict karni ho (nose-down command).
Galat approach: ka shape hai (transposed).
Yeh sahi kyun lagta hai: "Weights previous layer (rows) se current layer (columns) tak connect karte hain."
Fix: Standard convention mein, hai taaki size ka column vector de. ki har row mein layer ke ek neuron ke incoming weights hote hain.
Check: Agar hai aur hona chahiye, toh product valid hone ke liye hona zaroori hai.
Galat approach: Backpropagation ke dauran, activations paane ke liye forward pass scratch se recompute karo.
Yeh sahi kyun lagta hai: "Mujhe gradient calculations ke liye values chahiye."
Fix: Forward pass ke dauran aur cache karo. Backprop ko yeh chahiye hote hain (khaaskar activation derivatives compute karne ke liye), aur recomputing cost double kar deta hai. Resource-constrained hardware par run karne wale aerospace applications mein, yeh memory-vs-compute tradeoff critical hai.
Implementation tip:
# Store in a dictionary during forward pass
cache = {}
cache[f'z{ell}'] = z
cache[f'a{ell}'] = a
# Access during backprop
z = cache[f'z{ell}']Mnemonic & Recall
Yaad rakho: Har layer ke liye W·A → Z → σ → A. Yeh cycle baar repeat hoti hai.
Recall Feynman's 12-Year-Old Explanation
Imagine karo tum ek video game khel rahe ho jisme tum ek plane control karte ho. Computer ko decide karna hai "kya mujhe naak upar ya neeche jhukani chahiye?" jo kuch yeh dekh raha hai uske basis par (altitude, speed, etc.).
Ek feedforward network ek row mein baithne wale decision-makers ki chain jaisi hai. Pehla aadmi (input layer) instruments ke raw numbers dekhta hai. Woh kuch simple math karta hai (weights se multiply karo, bias add karo) aur apne answers agli aadmi ko pass karta hai. Woh aadmi un answers par AUR math karta hai aur aage pass karta hai. Yeh kai logo (hidden layers) se hota rehta hai jab tak aakhri aadmi (output layer) final decision nahi chillata: "5 degrees upar jhukao!"
"Activation function" ek rule ki tarah hai: "Agar tumhara answer negative hai, toh bas zero keh do" (woh ReLU hai). Yeh math ko zyada interesting banata hai kyunki iske bina, chain mein woh saare log bas ek bada sa multiplication kar rahe hote, aur tumhe sirf ek hi aadmi ki zaroorat hoti.
"Forward pass" ka matlab hai hum shuruaat se