cswork / libraryOpen practice workspace

Articles

Detailed RNN Backpropagation Walkthrough

This tutorial provides a complete walkthrough of backpropagation through time (BPTT) for recurrent neural networks, covering step-by-step derivations…

Backpropagation Through Time (BPTT) - Complete Walkthrough

Given Values

Video companion: This verified YouTube video gives a second pass on the same prep area.

Video: Tutorial 30- Recurrent Neural Network Forward Propogation With Time

Inputs

  • x₁ = 1.0
  • x₂ = 2.0
  • Initial hidden: h₀ = 0

Weights

  • Wₓ = 0.5
  • Wₕ = 0.1
  • b = 0.0
  • W_y = 2.0

Target Outputs

  • y₁^target = 1.0
  • y₂^target = 2.0

Loss Function

L=12(y1−1)2+12(y2−2)2L = \frac{1}{2}(y_1 - 1)^2 + \frac{1}{2}(y_2 - 2)^2


🟦 1. FORWARD PASS

Step 1: t = 1

h1=tanh⁡(Wxx1+Whh0)h_1 = \tanh(W_x x_1 + W_h h_0) =tanh⁡(0.5⋅1+0.1⋅0)=tanh⁡(0.5)≈0.4621= \tanh(0.5 \cdot 1 + 0.1 \cdot 0) = \tanh(0.5) \approx 0.4621

y1=Wyh1=2.0⋅0.4621=0.9242y_1 = W_y h_1 = 2.0 \cdot 0.4621 = 0.9242

Step 2: t = 2

h2=tanh⁡(Wxx2+Whh1)h_2 = \tanh(W_x x_2 + W_h h_1) =tanh⁡(0.5⋅2+0.1⋅0.4621)= \tanh(0.5 \cdot 2 + 0.1 \cdot 0.4621) =tanh⁡(1.0+0.04621)=tanh⁡(1.04621)≈0.7790= \tanh(1.0 + 0.04621) = \tanh(1.04621) \approx 0.7790

y2=Wyh2=2.0⋅0.7790=1.558y_2 = W_y h_2 = 2.0 \cdot 0.7790 = 1.558

Loss Calculation

L1=12(0.9242−1)2=0.00288L_1 = \frac{1}{2}(0.9242 - 1)^2 = 0.00288 L2=12(1.558−2)2=0.0978L_2 = \frac{1}{2}(1.558 - 2)^2 = 0.0978 L=0.1007L = 0.1007


🟥 2. BACKWARD PASS (BPTT)

We compute gradients from t=2 backward to t=1.

⚠️ Remember: The gradient for h₁ has TWO paths:

  1. Loss at t=1
  2. Through h₂ (because h₁ affects h₂)

🔻 Step 1: Gradients at t = 2

dL/dy₂

∂L∂y2=y2−y2target=1.558−2=−0.442\frac{\partial L}{\partial y_2} = y_2 - y_2^{target} = 1.558 - 2 = -0.442

Since ∂y₂/∂h₂ = W_y = 2:

∂L∂h2=−0.442⋅2=−0.884\frac{\partial L}{\partial h_2} = -0.442 \cdot 2 = -0.884

Backprop through tanh

tanh⁡′(a2)=1−h22=1−(0.7790)2=1−0.6069=0.3931\tanh'(a_2) = 1 - h_2^2 = 1 - (0.7790)^2 = 1 - 0.6069 = 0.3931

So: ∂L∂a2=∂L∂h2⋅tanh⁡′(a2)=−0.884⋅0.3931=−0.3475\frac{\partial L}{\partial a_2} = \frac{\partial L}{\partial h_2} \cdot \tanh'(a_2) = -0.884 \cdot 0.3931 = -0.3475

Where a₂ = Wₓ·x₂ + Wₕ·h₁.

Gradient wrt W_x

∂L∂Wx∣t=2=∂L∂a2⋅x2=−0.3475⋅2=−0.695\frac{\partial L}{\partial W_x}\bigg|_{t=2} = \frac{\partial L}{\partial a_2} \cdot x_2 = -0.3475 \cdot 2 = -0.695

Gradient wrt W_h

∂L∂Wh∣t=2=∂L∂a2⋅h1=−0.3475⋅0.4621=−0.1606\frac{\partial L}{\partial W_h}\bigg|_{t=2} = \frac{\partial L}{\partial a_2} \cdot h_1 = -0.3475 \cdot 0.4621 = -0.1606

🔻 Step 2: Gradient flows back to h₁

Because h₁ affects:

  1. Loss at t=1
  2. h₂

From t = 2:

∂L∂h1∣via t=2=∂L∂a2⋅Wh=−0.3475⋅0.1=−0.03475\frac{\partial L}{\partial h_1}\bigg|_{\text{via } t=2} = \frac{\partial L}{\partial a_2} \cdot W_h = -0.3475 \cdot 0.1 = -0.03475

🔻 Step 3: Gradients at t = 1

dL/dy₁

∂L∂y1=y1−1=0.9242−1=−0.0758\frac{\partial L}{\partial y_1} = y_1 - 1 = 0.9242 - 1 = -0.0758

∂L∂h1∣via y1=−0.0758⋅2=−0.1516\frac{\partial L}{\partial h_1}\bigg|_{\text{via } y_1} = -0.0758 \cdot 2 = -0.1516

TOTAL gradient wrt h₁

∂L∂h1=−0.1516+(−0.03475)=−0.18635\frac{\partial L}{\partial h_1} = -0.1516 + (-0.03475) = -0.18635

Backprop through tanh for t=1

tanh⁡′(a1)=1−h12=1−(0.4621)2=1−0.2135=0.7865\tanh'(a_1) = 1 - h_1^2 = 1 - (0.4621)^2 = 1 - 0.2135 = 0.7865

∂L∂a1=−0.18635⋅0.7865=−0.1466\frac{\partial L}{\partial a_1} = -0.18635 \cdot 0.7865 = -0.1466

Gradients for W_x at t=1

∂L∂Wx∣t=1=−0.1466⋅x1=−0.1466\frac{\partial L}{\partial W_x}\bigg|_{t=1} = -0.1466 \cdot x_1 = -0.1466

Gradients for W_h at t=1

∂L∂Wh∣t=1=−0.1466⋅h0=0\frac{\partial L}{\partial W_h}\bigg|_{t=1} = -0.1466 \cdot h_0 = 0

(because h₀ = 0)


🟩 3. Combine Gradients Across Timesteps

∂L∂Wx=−0.1466+(−0.695)=−0.8416\frac{\partial L}{\partial W_x} = -0.1466 + (-0.695) = -0.8416

∂L∂Wh=−0.1606+0=−0.1606\frac{\partial L}{\partial W_h} = -0.1606 + 0 = -0.1606

∂L∂Wy=h1⋅dLdy1+h2⋅dLdy2\frac{\partial L}{\partial W_y} = h_1 \cdot \frac{dL}{dy_1} + h_2 \cdot \frac{dL}{dy_2} =0.4621(−0.0758)+0.779(−0.442)= 0.4621(-0.0758) + 0.779(-0.442) =−0.0350−0.344=−0.379= -0.0350 - 0.344 = -0.379


🎯 Final Gradients

ParameterGradient
dL/dWₓ-0.8416
dL/dWₕ-0.1606
dL/dW_y-0.379

These would be used to update weights: W←W−η∂L∂WW \leftarrow W - \eta \frac{\partial L}{\partial W}

where η is the learning rate.

How to Use This Page as a Prep Plan

Do not treat this as passive reading. Convert the ideas in this page into a short weekly loop: learn one idea, practice it under interview conditions, then write down what changed. That is the fastest way to turn advice into visible interview behavior.

Prep areaWhat you need to provePractice artifact
UnderstandTurn the prompt into a concrete goal.Clarifying questions and success criteria.
PracticeUse realistic constraints and timed reps.Worked examples with edge cases.
ExplainMake reasoning visible.Tradeoffs, assumptions, and test strategy.
ImproveReview misses quickly.A short feedback log and next action.

For Detailed RNN Backpropagation Walkthrough, the strongest candidates usually do three things well: they make their assumptions explicit, they use concrete examples instead of vague claims, and they review mistakes quickly enough that the next practice rep is better than the last one.

FAQ

How should I use this guide?

Read it once for the structure, then turn each section into a practice task with a visible artifact.

What should I do if I am short on time?

Prioritize the skills most likely to be tested, then do one mock or timed drill to expose the largest gap.

How do I know I am ready?

You can explain your approach clearly, recover from hints, and name tradeoffs without relying on memorized wording.