import Info from ’@/components/widgets/Info.astro’;
Step 0: The Micro-Network Blueprint
To understand backpropagation down to the metal, strip away high-dimensional matrices and trace a single training example through a minimal network: 2 inputs → 2 hidden neurons with ReLU → 2 output logits with Softmax → Cross-Entropy Loss.
Plaintext
[x₁] ──W₁₁──► (z₁ → a₁) ──V₁₁──► (ẑ₁ → p₁) ──┐
╳ ╳ ╳ ├──► Loss L
[x₂] ──W₂₂──► (z₂ → a₂) ──V₂₂──► (ẑ₂ → p₂) ──┘
Forward Variables & Notation
-
Inputs: x=[x1x2], Target: y=[y1y2] (one-hot, where y1=1,y2=0).
-
Hidden Layer weights (W) and biases (b):
z1=W11x1+W12x2+b1⟹a1=max(0,z1)
z2=W21x1+W22x2+b2⟹a2=max(0,z2)
-
Output Layer weights (V) and biases (c):
z^1=V11a1+V12a2+c1
z^2=V21a1+V22a2+c2
-
Softmax Probabilities:
p1=ez^1+ez^2ez^1,p2=ez^1+ez^2ez^2
-
Categorical Cross-Entropy Loss:
L=−(y1lnp1+y2lnp2)
Step 1: Deriving the Output Error ∂z^i∂L
The biggest algebraic mystery in deep learning is why ∂z^∂L=p−y. Here is the exact scalar proof using the quotient rule.
Case A: Derivative of Softmax pi with respect to its own logit z^i
Let S=ez^1+ez^2. Then p1=Sez^1.
Using the quotient rule (vu)′=v2u′v−uv′:
∂z^1∂p1=S2∂z^1∂(ez^1)⋅S−ez^1⋅∂z^1∂S=S2ez^1⋅S−ez^1⋅ez^1
Split the fraction:
∂z^1∂p1=Sez^1(SS−ez^1)=Sez^1(1−Sez^1)=p1(1−p1)
Case B: Derivative of Softmax pj with respect to a different logit z^i (j=i)
Look at p2=Sez^2 differentiated with respect to z^1:
∂z^1∂p2=S20⋅S−ez^2⋅∂z^1∂S=S2−ez^2⋅ez^1=−(Sez^1)(Sez^2)=−p1p2
Case C: Applying the Chain Rule to Cross-Entropy
Since z^1 alters both p1 and p2, and both enter L=−y1lnp1−y2lnp2:
∂z^1∂L=∂p1∂L∂z^1∂p1+∂p2∂L∂z^1∂p2
Substitute the loss derivatives ∂p1∂L=−p1y1 and ∂p2∂L=−p2y2:
∂z^1∂L=(−p1y1)⋅[p1(1−p1)]+(−p2y2)⋅[−p1p2]
Cancel terms:
∂z^1∂L=−y1(1−p1)+y2p1=−y1+y1p1+y2p1=p1(y1+y2)−y1
Because probabilities in one-hot vectors sum to 1 (y1+y2=1):
∂z^1∂L=p1(1)−y1=p1−y1
Define this scalar error signal as:
δ1[2]=p1−y1,δ2[2]=p2−y2
Step 2: Output Layer Parameter Gradients
Now calculate how V11 and c1 change the loss.
Weight V11
V11 only influences L through z^1=V11a1+V12a2+c1.
∂V11∂L=∂z^1∂L⋅∂V11∂z^1
Since ∂V11∂z^1=a1:
∂V11∂L=δ1[2]⋅a1
Bias c1
Since ∂c1∂z^1=1:
∂c1∂L=∂z^1∂L⋅∂c1∂z^1=δ1[2]⋅1=δ1[2]
Following the exact same path for all other weights in Layer 2:
∂V12∂L=δ1[2]a2,∂V21∂L=δ2[2]a1,∂V22∂L=δ2[2]a2,∂c2∂L=δ2[2]
Step 3: Propagating Back to the Hidden Activations
How sensitive is L to hidden activation a1?
Notice that a1 connects to both output neurons:
By the multivariable chain rule:
∂a1∂L=∂z^1∂L∂a1∂z^1+∂z^2∂L∂a1∂z^2=δ1[2]V11+δ2[2]V21
Notice the index orientation: δ1[2] multiplies V11 and δ2[2] multiplies V21. This matches the column of V:
[∂a1∂L∂a2∂L]=[V11V12V21V22][δ1[2]δ2[2]]=VTδ[2]
The matrix transpose is an explicit outcome of collecting multi-path downstream connections.
Step 4: Pushing Through the Non-Linearity (ReLU)
To compute δ1[1]=∂z1∂L, trace through the activation function a1=max(0,z1):
∂z1∂L=∂a1∂L⋅∂z1∂a1
The derivative of ReLU is:
∂z1∂a1={10if z1>0if z1≤0
Therefore:
δ1[1]=(δ1[2]V11+δ2[2]V21)⋅I(z1>0)
δ2[1]=(δ1[2]V12+δ2[2]V22)⋅I(z2>0)
With δ1[1] and δ2[1] established, the first layer parameters mirror Step 2:
z1=W11x1+W12x2+b1
-
Weight W11:
∂W11∂L=∂z1∂L⋅∂W11∂z1=δ1[1]⋅x1
-
Weight W12:
∂W12∂L=∂z1∂L⋅∂W12∂z1=δ1[1]⋅x2
-
Bias b1:
∂b1∂L=∂z1∂L⋅∂b1∂z1=δ1[1]⋅1=δ1[1]
Likewise for neuron 2:
∂W21∂L=δ2[1]x1,∂W22∂L=δ2[1]x2,∂b2∂L=δ2[1]
Hands-On Numerical Run (Trace with Numbers)
Let’s plug in raw numbers for one full backward step.
Given State:
-
Inputs: x1=2.0,x2=1.0
-
Ground Truth: y1=1,y2=0
-
Hidden Activations (assume positive z, so ReLU′=1): a1=0.8,a2=0.4
-
Output Weights: V=[0.5−0.1−0.20.4]
-
Model Softmax Output: p1=0.3,p2=0.7
Manual Derivative Calculation:
-
Output Errors:
δ1[2]=p1−y1=0.3−1.0=−0.7
δ2[2]=p2−y2=0.7−0.0=+0.7
-
Output Weight Gradients:
∂V11∂L=δ1[2]⋅a1=(−0.7)(0.8)=−0.56
∂V12∂L=δ1[2]⋅a2=(−0.7)(0.4)=−0.28
∂V21∂L=δ2[2]⋅a1=(0.7)(0.8)=+0.56
∂V22∂L=δ2[2]⋅a2=(0.7)(0.4)=+0.28
-
Propagating Back to Hidden Units:
δ1[1]=(δ1[2]V11+δ2[2]V21)⋅1=(−0.7)(0.5)+(0.7)(−0.1)=−0.35−0.07=−0.42
δ2[1]=(δ1[2]V12+δ2[2]V22)⋅1=(−0.7)(−0.2)+(0.7)(0.4)=0.14+0.28=+0.42
-
Input Weight Gradients:
∂W11∂L=δ1[1]⋅x1=(−0.42)(2.0)=−0.84
∂W12∂L=δ1[1]⋅x2=(−0.42)(1.0)=−0.42
∂b1∂L=δ1[1]=−0.42
Every weight and bias gradient in an N-layer neural network resolves through this identical scalar mechanic: downstream error × incoming activation.