Hello! Welcome back to our module on Calculus for Machine Learning.
In the last lesson, we explored partial derivatives, which allow us to find the rate of change of a multivariable function along a specific axis. We saw how to calculate them by treating all other variables as constants and how to collect them into a gradient vector, .
However, many functions in machine learning are not simple sums or products of variables. Instead, they are functions of functions. Think of a neural network: the output of the first layer becomes the input to the second, whose output becomes the input to the third, and so on. This creates a chain of nested functions. Today, we'll learn the rule for differentiating them.
Our learning outcome is to apply the chain rule for composite functions. This is the final fundamental derivative rule we need before we can tackle gradient descent, the workhorse algorithm for training machine learning models.
1. What are Composite Functions?
A composite function is simply a function that is nested inside another function. If we have two functions, and , we can create a composite function by plugging the output of one into the input of the other.
For example, let and . The composite function would be:
Here, is the inner function and is the outer function.
You can visualize this as a sequence or a "chain" of operations, where the input flows through the first function, and its output becomes the input for the second function.

To further solidify this concept, let's review a brief article that defines composite functions and provides another example.
The Chain Rule of Calculus for Univariate and Multivariate ...
This article from Machine Learning Mastery provides a clear and concise explanation of what a composite function is, using the helpful analogy of 'systems in a cascade'.
Read the section titled 'Composite Functions'. Pay attention to how the example breaks down a function into a cascade of two systems, f(x) and g(x).
2. The Intuition: How a Small Nudge Propagates
Now, how do we find the derivative of such a composite function, ? We want to find , which represents how much the final output h changes for a tiny nudge in the input x.
Since the functions are chained together, a small nudge dx first causes a change in the output of the inner function, . Let's call this intermediate change df. This change df then serves as the input nudge for the outer function g, which in turn causes the final change dh. The chain rule formalizes the idea that the total rate of change is the product of the rates of change at each step in this chain.
The following video from 3Blue1Brown provides a brilliant visual intuition for this propagation of changes.
Visualizing the chain rule and product rule | Chapter 4, Essence of calculus
This video uses a clever three-number-line visualization to show how a small change in the input x propagates through an intermediate function to affect the final output.
Watch the segment from 08:35 to 14:26. Focus on how a nudge dx on the first number line causes a nudge dh on the second, which in turn causes a nudge on the third. This illustrates why the derivatives are multiplied.
As the video explained, if we call the output of the inner function , so that , the chain rule can be stated as:
This formula beautifully captures the intuition: the overall rate of change () is the rate of change of the outer function with respect to its input (), multiplied by the rate of change of the inner function with respect to its input ().
3. Applying the Chain Rule: The "Inside-Outside" Method
While the Leibniz notation () is great for intuition, a more practical way to apply the rule is to think in terms of "inside" and "outside" functions. The rule can be stated as:
The derivative of a composite function is the derivative of the outer function (leaving the inner function untouched) multiplied by the derivative of the inner function.
Let's use this method on our example, .
-
Identify Outer and Inner functions:
- Outer function:
- Inner function:
-
Take the derivative of the outer function, keeping the inner function inside:
- The derivative of is .
- So, we get .
-
Take the derivative of the inner function:
- The derivative of is .
-
Multiply the results from steps 2 and 3:
The following video from StatQuest demonstrates this "stuff inside" method clearly.
The Chain Rule, Clearly Explained!!!
Josh Starmer provides a very practical walkthrough of how to apply the chain rule when you're given a single, combined function.
Watch the segment from 09:19 to 11:34. Focus on how he identifies the 'stuff inside' the parentheses to break the problem down into the outer and inner derivatives.
This "inside-outside" process is a powerful and repeatable technique for any composite function.

Test your understanding!
Find the derivative of the function using the chain rule.
Show answer
-
Identify Outer and Inner functions:
- Outer function:
- Inner function:
-
Derivative of the outer function:
- The derivative of is .
- Plugging the inner function back in gives us .
-
Derivative of the inner function:
- The derivative of is .
-
Multiply:
4. Why the Chain Rule is Essential for Machine Learning
You might be wondering how this abstract rule connects to your goal of mastering machine learning. The connection is profound.
Neural networks are essentially giant, deeply nested composite functions.
An input vector x is passed through the first layer (a function), its output is passed through the second layer (another function), and this continues until the final layer produces a prediction. A loss function (yet another function) then compares this prediction to the true value.
To train the network, we use an algorithm called gradient descent, which needs the gradient of the loss function with respect to every weight in the network. The chain rule is the engine that powers this calculation. It allows us to calculate how a change in a weight in an early layer propagates through all the subsequent layers to affect the final loss. This process of using the chain rule to compute gradients in a neural network is famously known as backpropagation.
Essential Math for Machine Learning: The Chain Rule
This article explains exactly why the chain rule is so fundamental to training neural networks.
Read the sections 'Why Chain Rule Matters in Machine Learning' and 'Example: Logistic Regression' (including 'The Math Behind the Code'). You don't need to memorize the logistic regression formulas, but focus on understanding how the derivative of the loss with respect to a weight (∂loss/∂w) is calculated by 'chaining' together three separate derivatives.
Conclusion
Today, we've added the final core tool to our calculus toolkit. By understanding and applying the chain rule, you've unlocked the ability to differentiate the complex, nested functions that form the bedrock of modern machine learning.
Key Takeaways:
- A composite function is a function nested within another, like .
- The chain rule provides a method for finding the derivative of a composite function by multiplying the derivatives of its constituent parts.
- In Leibniz notation: .
- In practice, we use the "inside-outside" method: (derivative of the outer function) (derivative of the inner function).
- The chain rule is the mathematical foundation for backpropagation, the algorithm used to calculate gradients and train nearly all neural networks.
Preview of the Next Lesson:
We now have all the individual pieces: we can find derivatives of multivariable functions (partial derivatives) and derivatives of composite functions (chain rule). In the next lesson, we will put these together to compute gradients of vector-valued functions. This will lead directly into our lesson on Gradient Descent, where we will finally see how these gradients are used to iteratively "descend" a loss function surface and find the optimal parameters for a model.