Skip to main content

πŸ“Š Computing Derivatives with a Computation Graph

This notebook explains how to compute derivatives using a computation graph, following the example from Andrew Ng's neural networks course. It breaks down a simple function into smaller steps and shows how to apply the chain rule to calculate how changes in inputs affect the final output.


🧠 The Function​

We are given:

J=3(a+bc)J = 3(a + bc)

Let’s break it into smaller steps:

  • u=bΓ—cu = b \times c
  • v=a+uv = a + u
  • J=3Γ—vJ = 3 \times v

βœ… Forward Pass​

Given values:

  • a=5a = 5
  • b=3b = 3
  • c=2c = 2

We compute:

  • u=3Γ—2=6u = 3 \times 2 = 6
  • v=5+6=11v = 5 + 6 = 11
  • J=3Γ—11=33J = 3 \times 11 = 33

πŸ” Backward Pass (Derivatives)​

We use the chain rule to compute how much changing each input affects the output JJ.

Step 1: dJdv=3\frac{dJ}{dv} = 3​

Because:

J=3vβ‡’IfΒ v↑1,J↑3J = 3v \Rightarrow \text{If } v \uparrow 1, J \uparrow 3

Step 2: dvdu=1\frac{dv}{du} = 1​

Because:

v=a+uβ‡’IfΒ u↑1,v↑1v = a + u \Rightarrow \text{If } u \uparrow 1, v \uparrow 1

Step 3: dJdu=dJdvΓ—dvdu=3Γ—1=3\frac{dJ}{du} = \frac{dJ}{dv} \times \frac{dv}{du} = 3 \times 1 = 3​


πŸ“¦ Derivatives with Respect to Inputs​

1. dJda\frac{dJ}{da}​

  • v=a+uβ‡’dvda=1v = a + u \Rightarrow \frac{dv}{da} = 1
  • dJda=dJdvΓ—dvda=3Γ—1=3\frac{dJ}{da} = \frac{dJ}{dv} \times \frac{dv}{da} = 3 \times 1 = 3

2. dJdb\frac{dJ}{db}​

  • u=bΓ—cβ‡’dudb=c=2u = b \times c \Rightarrow \frac{du}{db} = c = 2
  • dJdb=dJduΓ—dudb=3Γ—2=6\frac{dJ}{db} = \frac{dJ}{du} \times \frac{du}{db} = 3 \times 2 = 6

3. dJdc\frac{dJ}{dc}​

  • u=bΓ—cβ‡’dudc=b=3u = b \times c \Rightarrow \frac{du}{dc} = b = 3
  • dJdc=dJduΓ—dudc=3Γ—3=9\frac{dJ}{dc} = \frac{dJ}{du} \times \frac{du}{dc} = 3 \times 3 = 9

βœ… Final Summary Table​

VariableDerivative
aadJda=3\frac{dJ}{da} = 3
bbdJdb=6\frac{dJ}{db} = 6
ccdJdc=9\frac{dJ}{dc} = 9

This tells us how much each input influences the final output JJ using derivatives and chain rule in a computation graph.