Hello! Welcome to the third lesson in our module on the mathematical foundations of AI.
In our previous lesson, you saw how matrices can be interpreted as linear transformations that rotate, scale, and shear vector spaces. This gave us a geometric understanding of how data can be manipulated. Now, we shift our focus from how data is manipulated to how we can find the best manipulation. This is the essence of model training.
Today's lesson addresses the learning outcome: Explain the role of partial derivatives and the chain rule in computing gradients. We are moving from linear algebra into calculus. These tools are fundamental to optimization, the process by which an AI model learns from data by minimizing its errors. You'll learn how we can precisely measure the impact of each of a model's thousands (or billions) of parameters on its performance.
1. From Error to Improvement: The Need for Derivatives
Imagine an AI model trying to predict house prices. It takes features (square footage, number of bedrooms) and makes a prediction. We measure its performance with a cost function (or loss function), which calculates the error—how far off the predictions are from the real prices. A high cost means a bad model; a low cost means a good model.
The goal of training is to adjust the model's internal parameters (its weights and biases) to minimize this cost. But how do we know which way to adjust them? If we nudge a specific weight up, will the cost go up or down? And by how much?
A derivative gives us the answer. It measures the instantaneous rate of change of a function with respect to a variable. In simpler terms, it tells you the slope of the function at a specific point. A positive slope means the function is increasing; a negative slope means it's decreasing.
2. Functions with Many Inputs: Partial Derivatives and the Gradient
Our model's cost isn't a function of just one parameter; it's a function of many parameters. This is where partial derivatives come in. A partial derivative measures the rate of change of a function with respect to one variable, while holding all other variables constant.
Let's say our cost function depends on two weights, and .
- The partial derivative tells us how the cost changes as we tweak only .
- The partial derivative tells us how the cost changes as we tweak only .
The gradient is simply a vector that bundles all these partial derivatives together. For our two-weight example, the gradient of is written as (pronounced "del C"):
The gradient is incredibly important for two reasons:
- Each component tells us the "sensitivity" of the cost function to a specific parameter.
- The gradient vector itself points in the direction of the steepest ascent of the cost function.
This means if we want to reduce the cost as quickly as possible, we should move in the direction opposite to the gradient. This is the core idea behind gradient descent, which we'll implement in the next lesson.
The following article gives a great visual intuition for what the gradient is and why it's so significant for finding the minimum of a function.
The Gradient: A Visual Descent - The Laziest Programmer
This article, 'The Gradient: A Visual Descent,' explains how the gradient is a vector of partial derivatives and why it points in the direction of steepest ascent.
Find the sections on finding, visualizing, and proving the gradient's significance. Below the first heading, read the mathematical explanation of 'The Math: Finding the Gradient', 'The Math: Visualizing the Gradient’s Significance', and 'The Math: Proving the Gradient’s Significance'. Focus on the concept that the gradient is a vector of partials and that it points 'uphill' on the cost surface.
3. Untangling Dependencies: The Chain Rule
In a neural network, the cost function is not a direct, simple function of the weights. Instead, there's a chain of dependencies:
- The cost depends on the network's final activation (its output).
- The activation depends on the weighted sum of inputs ().
- The weighted sum depends on the weights, biases, and previous activations.
How do we find the partial derivative of the cost with respect to a weight () that's buried deep in this chain? The answer is the chain rule. It allows us to calculate the derivative of a composite function by multiplying the derivatives of the "links" in the chain.
Intuition First
Before diving into neural network notation, let's build an intuition with a clear, simple analogy.
The Chain Rule, Clearly Explained!!!
This video from the StatQuest channel provides one of the clearest explanations of the chain rule using easy-to-understand examples.
Starting just past the two-minute mark, watch the two examples covered in this section. The first example (weight -> height -> shoe size) shows how to chain derivatives of separate functions. The second example (time -> hunger -> ice cream) shows how to apply it to a nested function. Focus on the core idea: d(outer)/d(inner) * d(inner)/d(input).
As the video shows, if a variable depends on , and depends on , the chain rule states that the derivative of with respect to is:
You can think of this as "canceling out" the terms.
When dealing with multiple variables, we use the same principle with partial derivatives.

4. The Chain Rule in Action: Calculating Gradients in a Neural Network
Now, let's apply the chain rule to our neural network problem. We want to find . We can trace the dependencies backward from the cost to the weight :
Cost (C) <-- Activation (a) <-- Weighted Sum (z) <-- Weight (w)
Using the chain rule, we can express as a product of three simpler derivatives:
Let's break down each part:
- : How does the cost change with the output activation? This tells us how much a change in the final output affects the error. If the output is far from the target, this derivative will be large.
- : How does the activation change with the weighted sum? This is simply the derivative of the activation function (e.g., Sigmoid or ReLU). It tells us how sensitive the neuron's output is to its input.
- : How does the weighted sum change with a weight? This tells us how much a single weight contributes to the neuron's input.
This process of using the chain rule to work backward from the cost and compute gradients is the core of the backpropagation algorithm, which is how all deep neural networks are trained.
The following video from 3Blue1Brown is a classic and provides an excellent, formal explanation of this process.
Backpropagation calculus | Deep Learning Chapter 4
This video, 'Backpropagation calculus,' walks through the exact calculus of computing the gradient for a simple neural network. It's the perfect bridge from the abstract chain rule to its concrete application in AI.
Watch the derivative calculation. This section shows how to calculate the derivative of the cost with respect to a weight in the final layer using the chain rule, and then discusses how this generalizes to layers with more neurons. Don't worry about memorizing the formulas; focus on understanding the chain of dependencies.
For a more detailed, text-based version of this explanation that you can read at your own pace, the accompanying article is highly recommended. It covers the same concepts shown in the video.
Backpropagation calculus - 3Blue1Brown
This is the text version of the video you just watched. It's useful for reviewing the concepts and seeing the equations written out clearly.
Find the sections from 'Computing The First Derivative' through 'More Complicated Networks' and read about network derivatives. This covers how to compute the derivative for a weight, a bias, and how the logic extends backwards to previous layers and more complex networks. Pay special attention to the formula ∂C/∂w = (∂z/∂w) * (∂a/∂z) * (∂C/∂a) (the terms may be in a different order, which is fine).
Test your understanding!
In the 3Blue1Brown example, the weighted sum for the last layer is defined as .
Using the chain rule, write down the expression for the partial derivative of the cost with respect to the bias, . How is it different from the derivative with respect to the weight, ?
Show answer
The chain of dependencies for the bias is: Cost (C₀) <-- Activation (a) <-- Weighted Sum (z) <-- Bias (b).
The chain rule expression is:
The first two terms are identical to the expression for the weight. The only difference is the last term, .
Since , the derivative of with respect to is simply 1. In contrast, the derivative with respect to was . This makes the gradient calculation for the bias slightly simpler.
Conclusion
This lesson was a deep dive into the mechanics of how learning is made possible. Let's summarize the key components and their roles:
- Partial Derivatives: They measure the sensitivity of the cost function to a single model parameter. This tells us how impactful each parameter is.
- The Gradient (): This is a vector containing all the partial derivatives. It points in the direction of the steepest increase in the cost function.
- The Chain Rule: This is the essential algorithm for calculating the partial derivatives for complex, nested functions like neural networks. It works by propagating the derivatives backward from the output layer to the input layer, a process known as backpropagation.
You now understand the "what" and the "why" behind gradient computation. You know that to train a model, we need to calculate the gradient of the cost function, and the chain rule is how we do it.
In the next lesson, we will put this knowledge into practice. You will calculate a gradient and implement the gradient descent algorithm from scratch to minimize a simple function. This will be your first step in writing code that can "learn."