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

Chain Rule

The chain rule tells you how to differentiate a function that's built by composing other functions โ€” exactly the situation a neural network is in, since it's a chain of layers, each one feeding into the next. The chain rule is the single mathematical fact that makes backpropagation possible.

Formula

\[ \text{if } y = f(u) \text{ and } u = g(x), \quad \text{then} \quad \frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dx} \]

To find how \(y\) changes with respect to \(x\), when \(x\) only affects \(y\) indirectly (through \(u\)), you multiply the "local" derivatives along the chain of dependency.

Numerical Example

\[ y = (3x+1)^2, \qquad u = 3x+1, \quad y = u^2 \] \[ \frac{dy}{du} = 2u, \qquad \frac{du}{dx} = 3 \] \[ \frac{dy}{dx} = 2u \cdot 3 = 6u = 6(3x+1) \]

At \(x=1\): \(u = 3(1)+1 = 4\), so \(\frac{dy}{dx} = 6(4) = 24\). Verify directly: \(y=(3x+1)^2 = 9x^2+6x+1\), so \(\frac{dy}{dx}=18x+6\), and at \(x=1\) that's \(24\) โ€” matches.

A Longer Chain โ€” Like a Neural Network

A network is a chain of many functions: input โ†’ layer 1 โ†’ activation โ†’ layer 2 โ†’ activation โ†’ ... โ†’ loss. For \(n\) composed functions, the chain rule extends naturally:

\[ \frac{dL}{dx} = \frac{dL}{dz_n}\cdot\frac{dz_n}{dz_{n-1}}\cdots\frac{dz_2}{dz_1}\cdot\frac{dz_1}{dx} \]

Each factor is a "local" derivative โ€” how one layer's output changes with respect to its own input โ€” and the full derivative of the loss with respect to any early input is the product of all these local derivatives along the path. This product structure is exactly why the vanishing gradient problem happens: if each local derivative is a fraction less than 1, the product shrinks exponentially with the number of layers.

Code โ€” Chain Rule via Autograd

import torch

x = torch.tensor(1.0, requires_grad=True)
u = 3 * x + 1        # u = g(x)
y = u ** 2             # y = f(u)

y.backward()
print(x.grad)   # tensor(24.) -- PyTorch applied the chain rule automatically

Where This Shows Up in Deep Learning

Backpropagation is the chain rule, applied systematically from the loss backward through every layer to every weight. Each layer only needs to know how to compute its own "local" derivative (its output with respect to its own input and its own weights) โ€” the chain rule handles stitching all those local derivatives together into the full gradient for every parameter in the network, no matter how deep it is. The full worked example lives in Backpropagation Worked Example.

Common Mistakes

  • Forgetting a factor in a long chain โ€” every intermediate function in the composition contributes one multiplicative term; skipping one gives a wrong gradient.
  • Assuming the chain rule only applies to single-variable chains โ€” the multivariate version (used throughout deep learning) sums contributions across every path a variable can influence the output through, which is exactly what automatic differentiation tracks via the computational graph.

Interview Relevance

Q: "How does the chain rule relate to backpropagation?" Backpropagation computes the gradient of the loss with respect to every weight by applying the chain rule layer by layer, from the output back to the input. Each layer contributes its own local derivative, and these are multiplied together along the path from that weight to the loss โ€” this is literally what "back-propagating" the gradient means.

Practice Question

Given \(y = \sin(x^2)\), identify the inner and outer functions, then use the chain rule to find \(\frac{dy}{dx}\).

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 โ€“ FAQs

Quick answers about learning Chain Rule in Deep Learning.

This free note from CodingNow 2.0 explains Chain Rule 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, is 100% free with no signup required.
With focused practice, most students grasp Chain Rule 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