3.1.3Neural Network Fundamentals

Forward propagation computation

1,913 words9 min readdifficulty · medium

WHY does forward propagation exist?

WHAT problem does it solve? A neural network is a function y^=f(x)\hat{y} = f(x) with millions of tunable knobs (weights). Before we can learn those knobs (backprop), we must be able to compute the prediction for a given input. Forward propagation is that computation — the "run the network" step.

WHY this design (linear + nonlinear)?

  • A pure stack of linear layers collapses: W2(W1x)=(W2W1)xW_2(W_1 x) = (W_2 W_1)x — still linear, no matter how deep. So depth would be useless.
  • Inserting a nonlinear activation between layers breaks that collapse, letting the network approximate curved, complex functions. This is the whole reason deep nets are powerful.

HOW: building it from first principles

Step 1 — A single neuron

A neuron receives inputs x1,,xnx_1,\dots,x_n, each with importance wiw_i, plus a bias bb.

Why the bias? Without bb, every neuron's decision boundary must pass through the origin. The bias shifts the activation left/right, giving freedom to fire even when inputs are zero.

Step 2 — Vectorize one layer

Stacking mm neurons in a layer, each has its own weight row. Collect them:

z=Wx+b,a=ϕ(z)\mathbf{z} = W \mathbf{x} + \mathbf{b}, \qquad \mathbf{a} = \phi(\mathbf{z})

Why does row jj of WW contain neuron jj's weights? Because matrix–vector multiply computes each output as a dot product of a row with the input. Row jj · input =zj= z_j — exactly the weighted sum for neuron jj.

Step 3 — Chain the whole network

With input a[0]=x\mathbf{a}^{[0]} = \mathbf{x}, repeat for =1,2,,L\ell = 1,2,\dots,L:

a[0]=x    z[1],a[1]        a[L]=y^\mathbf{a}^{[0]} = \mathbf{x} \;\longrightarrow\; \mathbf{z}^{[1]},\mathbf{a}^{[1]} \;\longrightarrow\;\cdots\;\longrightarrow\; \mathbf{a}^{[L]} = \hat{y}

The final activation a[L]\mathbf{a}^{[L]} is the prediction. For classification ϕ[L]\phi^{[L]} is usually softmax; for regression it's often identity.

Figure — Forward propagation computation

Step 4 — Batch form (why we use matrices)

Instead of one input vector, process NN examples at once as columns of XX (n×Nn\times N): Z[]=W[]A[1]+b[],A[]=ϕ(Z[])Z^{[\ell]} = W^{[\ell]}A^{[\ell-1]} + \mathbf{b}^{[\ell]},\qquad A^{[\ell]} = \phi(Z^{[\ell]}) The bias b[]\mathbf{b}^{[\ell]} (m×1m\times 1) is broadcast across all NN columns. Why batch? One big matmul uses hardware (GPU) far more efficiently than NN small ones.


Common activations (and WHY each)

Common hidden activations: ReLU ϕ(z)=max(0,z)\phi(z)=\max(0,z) (cheap, no vanishing gradient for z>0z>0), sigmoid ϕ(z)=11+ez\phi(z)=\frac{1}{1+e^{-z}} (squashes to (0,1)(0,1)), tanh (squashes to (1,1)(-1,1)).


Worked Example 1 — a tiny 2-layer network by hand

Input x=[12]\mathbf{x} = \begin{bmatrix}1\\2\end{bmatrix}. Hidden layer (2 units), ReLU: W[1]=[0.50.511],b[1]=[01]W^{[1]}=\begin{bmatrix}0.5 & -0.5\\ 1 & 1\end{bmatrix},\quad \mathbf{b}^{[1]}=\begin{bmatrix}0\\-1\end{bmatrix} Output layer (1 unit), identity: W[2]=[21],b[2]=0.5W^{[2]}=\begin{bmatrix}2 & -1\end{bmatrix},\quad b^{[2]}=0.5

Layer 1 pre-activationWhy? Combine inputs with weights + bias: z[1]=[0.5(1)+(0.5)(2)+01(1)+1(2)1]=[0.52]\mathbf{z}^{[1]}=\begin{bmatrix}0.5(1)+(-0.5)(2)+0\\ 1(1)+1(2)-1\end{bmatrix}=\begin{bmatrix}-0.5\\2\end{bmatrix}

Layer 1 activationWhy? Apply ReLU (clip negatives to 0): a[1]=[max(0,0.5)max(0,2)]=[02]\mathbf{a}^{[1]}=\begin{bmatrix}\max(0,-0.5)\\ \max(0,2)\end{bmatrix}=\begin{bmatrix}0\\2\end{bmatrix}

Layer 2Why? Same recipe, identity activation: z[2]=2(0)+(1)(2)+0.5=1.5,y^=1.5z^{[2]}=2(0)+(-1)(2)+0.5=-1.5,\qquad \hat{y}=-1.5

Worked Example 2 — softmax output

Logits z[L]=[210]\mathbf{z}^{[L]}=\begin{bmatrix}2\\1\\0\end{bmatrix}. Why exponentiate? To turn scores into positive, comparable weights: e2=7.389,  e1=2.718,  e0=1,sum=11.107e^{2}=7.389,\; e^{1}=2.718,\; e^{0}=1,\quad \text{sum}=11.107 y^=[0.6650.2450.090]\hat{y}=\begin{bmatrix}0.665\\0.245\\0.090\end{bmatrix} Why divide by the sum? So the outputs form a probability distribution (=1\sum=1). Class 0 is most likely.


Forecast-then-Verify


Common mistakes (Steel-manned)


Recall Explain to a 12-year-old (Feynman)

Imagine a bucket-brigade passing water. Each person grabs water from the people behind them, but they trust some neighbours more than others — so they scoop more from trusted ones (that's the weights). They also always add a fixed splash of their own (the bias). Then they pour it through a funny-shaped funnel that changes how much comes out (the activation). The water gets passed forward person by person until the last person shows the answer. Forward propagation is just water flowing forward through the brigade once.


Flashcards

What are the two operations in one layer's forward pass?
A linear step z=Wa[1]+b\mathbf z = W\mathbf a^{[\ell-1]}+\mathbf b, then a nonlinear activation a=ϕ(z)\mathbf a = \phi(\mathbf z).
Why must layers have a nonlinear activation?
Otherwise stacked linear layers collapse into a single linear map, so depth adds no expressive power.
For a layer with nn inputs and mm units, what is the shape of WW?
m×nm \times n (output rows × input columns), so that WaW\mathbf a yields an mm-vector.
What does the bias term do geometrically?
It shifts the pre-activation, letting a neuron fire even when inputs are zero (decision boundary need not pass through origin).
Why use exponentials in softmax?
They are positive and monotonic, so larger logits → larger probabilities; dividing by their sum makes outputs a valid distribution summing to 1.
What is the pre-activation zz?
The weighted sum plus bias, z=iwixi+bz=\sum_i w_i x_i + b, before applying the activation.
In batch forward prop, why is the bias broadcast?
The same m×1m\times1 bias applies to every one of the NN example-columns of Z=WA+bZ=WA+\mathbf b.
Which activation is typical for the output of a classifier vs a regressor?
Softmax for multi-class classification; identity (linear) for regression.
Why do we prefer batched matrix multiplies over per-sample loops?
A single large matmul uses GPU/BLAS hardware far more efficiently than many small ones.

Connections

Concept Map

feeds

mix signals

shifts boundary

bent through

breaks linear collapse

becomes input to

chained L times

final layer output

rows = neuron weights

enables learning of

lets net approximate

Input x

Pre-activation z = Wx + b

Weights W

Bias b

Activation a = phi z

Nonlinearity phi

Next layer

Prediction y-hat

Row j dot input = z_j

Backprop knob tuning

Complex curved functions

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Forward propagation ka matlab bas itna hai: input ko network ke andar aage ki taraf bhejna, layer-by-layer, jab tak prediction na mil jaye. Har layer do kaam karti hai — pehle ek linear step (z=Wa+bz = W a + b, yaani inputs ko weights se multiply karke bias add karna), phir ek nonlinear activation (a=ϕ(z)a = \phi(z), jaise ReLU ya softmax). Weight batata hai kaunsa input kitna important hai, aur bias ek fixed shift deta hai taaki neuron zero input pe bhi fire kar sake.

Sabse important baat — activation kyun zaroori hai? Agar tum sirf linear layers stack karo, toh poora network end me ek hi linear equation ban jaata hai, chahe kitni bhi layers ho. Nonlinearity hi network ko "curved" aur powerful banati hai. Isliye ReLU/tanh/softmax ko kabhi skip mat karna.

Shapes ka dhyan rakho: agar layer me nn inputs aur mm neurons hain, toh WW ka shape m×nm\times n hota hai (output rows, input columns), taaki WaWa ka answer mm-length vector aaye. Yeh ek badhiya sanity check hai — agar dimensions match nahi kar rahe, toh tumhara WW ulta hai. Batch me kaam karte time saare examples ko columns bana ke ek bade matrix multiply se process karte hain, jo GPU pe bahut fast chalta hai.

Yaad rakhne ka mantra: "Weigh, Bias, Bend — Repeat". Bas yahi cheez har layer me repeat hoti hai jab tak last layer se y^\hat y (prediction) nahi nikal aata. Yeh step baad me backpropagation ke liye foundation banta hai — kyunki gradients nikaalne ke liye humein forward pass ke zz aur aa values chahiye hoti hain.

Go deeper — visual, from zero

Test yourself — Neural Network Fundamentals

Connections