๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #111

Chain Rule in Backpropagation

This note applies the chain rule from Chain Rule specifically to the structure of a multi-layer neural network โ€” showing exactly how a gradient signal travels backward through layer after layer to reach a weight buried deep inside the network.

A Network as a Chain of Functions

An \(L\)-layer network is literally a composition of functions: \(\hat y = f^{(L)}(f^{(L-1)}(\cdots f^{(1)}(\mathbf{x})\cdots))\). To find how the loss \(L\) depends on a weight \(w\) buried in layer \(l\), the chain rule must pass through every layer between \(l\) and the output:

\[ \frac{\partial \text{Loss}}{\partial w^{(l)}} = \frac{\partial \text{Loss}}{\partial \mathbf{a}^{(L)}} \cdot \frac{\partial \mathbf{a}^{(L)}}{\partial \mathbf{a}^{(L-1)}} \cdots \frac{\partial \mathbf{a}^{(l+1)}}{\partial \mathbf{a}^{(l)}} \cdot \frac{\partial \mathbf{a}^{(l)}}{\partial w^{(l)}} \]

Each factor is a "local" derivative โ€” how one layer's output changes with respect to its own input or its own weights, which is exactly the Jacobian introduced in Jacobian.

The Key Insight: Local Gradients, Computed Once, Reused

Notice that the product for layer \(l-1\)'s weights shares every term except the very last factor with layer \(l\)'s product. Backpropagation exploits this directly: it computes the "gradient flowing into" each layer's output once, moving backward from the loss, and every layer only ever needs to know two things โ€” its own local derivative with respect to its inputs, and its own local derivative with respect to its weights. Neither layer needs any awareness of what's happening in any other layer.

Diagram โ€” The Chain, Layer by Layer

Layer 1 Layer 2 Layer 3 Loss ∂Loss/∂a3 × ∂a3/∂a2 × ∂a2/∂a1

Each layer only ever computes its own local derivative; the chain rule stitches these local pieces into the full gradient for any weight, however deep in the network.

Code

import torch

x = torch.tensor(1.0, requires_grad=True)
a1 = x * 2          # layer 1 (toy: multiply by 2)
a2 = a1 ** 2         # layer 2 (toy: square)
loss = (a2 - 3) ** 2  # loss

loss.backward()
print(x.grad)   # PyTorch applied the exact chain-rule product across all three "layers" automatically

Common Mistakes

  • Trying to derive a deep network's full gradient by writing out the entire chain-rule product by hand for every weight โ€” this is exactly the tedious, error-prone, and unnecessary work backpropagation's local-gradient reuse eliminates; frameworks handle it automatically via the computational graph.
  • Forgetting that a variable feeding into multiple downstream paths (common in architectures with skip connections) must have its gradient contributions from every path summed, not just taken from one โ€” this was flagged already in Computational Graphs.

Interview Relevance

Q: "Why does each layer in backpropagation only need to know its own local derivative, not the whole network's structure?" Because the chain rule's product structure means the gradient for any weight decomposes into a chain of per-layer local derivatives. Backpropagation computes the "gradient flowing backward" once per layer and passes it to the previous layer, so each layer only ever needs to combine that incoming signal with its own local derivative โ€” no layer needs global knowledge of the network.

Practice Question

For a 5-layer network, how many "local derivative" factors would the chain rule need to multiply together to compute the gradient for a weight in layer 1?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Chain Rule in Backpropagation โ€“ FAQs

Quick answers about learning Chain Rule in Backpropagation in Deep Learning.

This free note from CodingNow 2.0 explains Chain Rule in Backpropagation in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Chain Rule in Backpropagation, is 100% free with no signup required.
With focused practice, most students grasp Chain Rule in Backpropagation in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now