๐ Backpropagation
538 words
3 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# ๐ Backpropagation ## 1. ๐ฏ Learning Objectives - Apply the chain rule to compute gradients in a neural network - Trace backpropagation through a 3-node network by hand - Explain the vanishing gradient problem - Implement backpropagation for a 2-layer MLP ## 2.

๐ Backpropagation
1. ๐ฏ Learning Objectives
- Apply the chain rule to compute gradients in a neural network
- Trace backpropagation through a 3-node network by hand
- Explain the vanishing gradient problem
- Implement backpropagation for a 2-layer MLP
2. ๐ Core Content
3.1 Intuition: Reverse-Mode Differentiation
Forward propagation computes the output. Backpropagation computes the gradient of the loss with respect to each parameter by applying the chain rule from the output back to the input.
Each parameter's gradient tells us: "if we tweak this weight, how much does the loss change?" This is what we need for gradient descent.
3.2 Computational Graph for a 3-Node Network
Consider a minimal network: input x, one hidden neuron with sigmoid, output neuron with sigmoid.
(Diagram)
3.3 Forward Pass
z1โ=w1โโ x h=ฯ(z1โ)=1+eโz1โ1โ z2โ=w2โโ h y^โ=ฯ(z2โ) L=21โ(yโy^โ)23.4 Backward Pass (Gradient Computation)
We want โw1โโLโ and โw2โโLโ.
Chain rule for wโ:
Step 1: โy^โโLโ=y^โโy (since L=21โ(yโy^โ)2)
Step 2: โz2โโy^โโ=ฯ(z2โ)(1โฯ(z2โ))=y^โ(1โy^โ)
Step 3: โw2โโz2โโ=h
Product: โw2โโLโ=(y^โโy)โ
y^โ(1โy^โ)โ
h
Chain rule for wโ:
Step 4: โhโz2โโ=w2โ
Step 5: โz1โโhโ=ฯ(z1โ)(1โฯ(z1โ))=h(1โh)
Step 6: โw1โโz1โโ=x
Product: โw1โโLโ=(y^โโy)โ
y^โ(1โy^โ)โ
w2โโ
h(1โh)โ
x
3.5 Numerical Example
Let x=1, y=0 (target), wโ=0.5, wโ=0.8.
Forward: zโ = 0.5 ร 1 = 0.5 h = ฯ(0.5) = 0.622 zโ = 0.8 ร 0.622 = 0.498 ลท = ฯ(0.498) = 0.622 L = 0.5 ร (0 - 0.622)ยฒ = 0.193
Backward: โL/โลท = 0.622 - 0 = 0.622 โลท/โzโ = 0.622 ร 0.378 = 0.235 โzโ/โwโ = h = 0.622 โL/โwโ = 0.622 ร 0.235 ร 0.622 = 0.0909
โzโ/โh = wโ = 0.8 โh/โzโ = 0.622 ร 0.378 = 0.235 โzโ/โwโ = x = 1 โL/โwโ = 0.622 ร 0.235 ร 0.8 ร 0.235 ร 1 = 0.0275
Update (ฮท=0.5): wโ_new = 0.8 - 0.5 ร 0.0909 = 0.755 wโ_new = 0.5 - 0.5 ร 0.0275 = 0.486
3.6 Vanishing Gradient
Notice that โL/โwโ = (something) ร wโ ร h(1-h) ร x. The sigmoid derivative h(1-h) is at most 0.25 and near 0 for large |z|. In deep networks, multiplying many such small gradients causes the gradient to "vanish" in early layers โ they barely learn.
This is why ReLU (derivative = 1 for positive inputs) is preferred in deep networks.
4. ๐ Practice Questions
Q1: For the 3-node network above with x=0.5, y=1, wโ=1, wโ=2, compute โL/โwโ.Answer: Forward: zโ=1ร0.5=0.5, h=ฯ(0.5)=0.622, zโ=2ร0.622=1.244, ลท=ฯ(1.244)=0.776, L=0.5ร(1-0.776)ยฒ=0.025. Backward: โL/โลท=ลท-y=0.776-1=-0.224, โลท/โzโ=0.776ร0.224=0.174, โzโ/โwโ=h=0.622. โL/โwโ=(-0.224)ร0.174ร0.622=-0.0242. Join Discord PreviousForward Prop & LossNextOptimization Methods