Registry Synced

๐Ÿ”™ Backpropagation

538 words
3 min read

Reading compass

Now ยท 1. ๐ŸŽฏ Learning Objectives

๐Ÿ”™ 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โ‹…xz_1 = w_1 \cdot x h=ฯƒ(z1)=11+eโˆ’z1h = \sigma(z_1) = \frac{1}{1+e^{-z_1}} z2=w2โ‹…hz_2 = w_2 \cdot h y^=ฯƒ(z2)\hat{y} = \sigma(z_2) L=12(yโˆ’y^)2L = \frac{1}{2}(y - \hat{y})^2

3.4 Backward Pass (Gradient Computation)

We want โˆ‚Lโˆ‚w1\frac{\partial L}{\partial w_1} and โˆ‚Lโˆ‚w2\frac{\partial L}{\partial w_2}. Chain rule for wโ‚‚:
โˆ‚Lโˆ‚w2=โˆ‚Lโˆ‚y^โ‹…โˆ‚y^โˆ‚z2โ‹…โˆ‚z2โˆ‚w2\frac{\partial L}{\partial w_2} = \frac{\partial L}{\partial \hat{y}} \cdot \frac{\partial \hat{y}}{\partial z_2} \cdot \frac{\partial z_2}{\partial w_2}
Step 1: โˆ‚Lโˆ‚y^=y^โˆ’y\frac{\partial L}{\partial \hat{y}} = \hat{y} - y (since L=12(yโˆ’y^)2L = \frac{1}{2}(y-\hat{y})^2) Step 2: โˆ‚y^โˆ‚z2=ฯƒ(z2)(1โˆ’ฯƒ(z2))=y^(1โˆ’y^)\frac{\partial \hat{y}}{\partial z_2} = \sigma(z_2)(1-\sigma(z_2)) = \hat{y}(1-\hat{y}) Step 3: โˆ‚z2โˆ‚w2=h\frac{\partial z_2}{\partial w_2} = h Product: โˆ‚Lโˆ‚w2=(y^โˆ’y)โ‹…y^(1โˆ’y^)โ‹…h\frac{\partial L}{\partial w_2} = (\hat{y} - y) \cdot \hat{y}(1-\hat{y}) \cdot h Chain rule for wโ‚:
โˆ‚Lโˆ‚w1=โˆ‚Lโˆ‚y^โ‹…โˆ‚y^โˆ‚z2โ‹…โˆ‚z2โˆ‚hโ‹…โˆ‚hโˆ‚z1โ‹…โˆ‚z1โˆ‚w1\frac{\partial L}{\partial w_1} = \frac{\partial L}{\partial \hat{y}} \cdot \frac{\partial \hat{y}}{\partial z_2} \cdot \frac{\partial z_2}{\partial h} \cdot \frac{\partial h}{\partial z_1} \cdot \frac{\partial z_1}{\partial w_1}
Step 4: โˆ‚z2โˆ‚h=w2\frac{\partial z_2}{\partial h} = w_2 Step 5: โˆ‚hโˆ‚z1=ฯƒ(z1)(1โˆ’ฯƒ(z1))=h(1โˆ’h)\frac{\partial h}{\partial z_1} = \sigma(z_1)(1-\sigma(z_1)) = h(1-h) Step 6: โˆ‚z1โˆ‚w1=x\frac{\partial z_1}{\partial w_1} = x Product: โˆ‚Lโˆ‚w1=(y^โˆ’y)โ‹…y^(1โˆ’y^)โ‹…w2โ‹…h(1โˆ’h)โ‹…x\frac{\partial L}{\partial w_1} = (\hat{y} - y) \cdot \hat{y}(1-\hat{y}) \cdot w_2 \cdot h(1-h) \cdot 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
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.