Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 29

Chain Rule for Gradients in Computational Graphs

Recommended resources

Hello! Welcome to your next lesson in the "Mathematics for Machine Learning" module.

In our previous lesson, we established a solid foundation in derivatives and partial derivatives. You learned how to compute them and, more importantly, how to interpret them as the instantaneous rate of change of a function. We concluded by introducing the gradient vector—a collection of all partial derivatives that points in the direction of the steepest ascent.

Today, we'll answer the crucial follow-up question: How do we compute the gradient for a complex function, like a deep neural network, which is essentially a chain of nested functions?

This lesson addresses the learning outcome: Apply the chain rule to compute gradients in computational graphs. This is the mathematical engine that drives the training of nearly all modern neural networks, a process known as backpropagation. By the end of this lesson, you will be able to:

  1. Explain the chain rule for composite functions.
  2. Represent a function as a computational graph.
  3. Trace the forward and backward passes on a graph.
  4. Apply the chain rule step-by-step to calculate the gradient of an output with respect to any input in the graph.

1. The Core Idea: Composing Rates of Change

Imagine a system of interconnected gears. If you know how fast the first gear turns the second, and how fast the second turns the third, you can figure out how fast the first gear turns the third by simply multiplying those rates. This is the essence of the chain rule.

To formalize this, let's start with a brief reading.

The Chain Rule: Backbone of Backpropagation

This article from TensorTonic provides a fantastic 'Gear System' analogy to build intuition for the chain rule. It also presents the formal definition for a single-variable case.

Read the sections 'Introduction', 'The Gear System Intuition', and 'Single Variable Chain Rule'. Focus on the core principle: multiplying local rates to get the global rate.

As the article states, if you have a composite function like , you can think of it as a chain: affects , which in turn affects . To find how a change in affects , we multiply the derivatives of each link in the chain.

If we let , then . The chain rule is:

This simple multiplication is the key. But for a neural network with millions of parameters, the "chain" is incredibly complex. We need a way to organize this calculation.


2. Computational Graphs: Visualizing the Math

Since you have a background in programming, you're used to breaking down complex logic into smaller, manageable functions. We can do the same for mathematics using computational graphs. A computational graph represents a function as a directed graph where nodes are operations (e.g., +, *, sin) and edges represent the data flowing between them.

Why bother with this? For a complex model, deriving the full gradient expression on paper is not only tedious and error-prone but also not modular. If you change one part of your model (like the activation function), you'd have to re-derive everything. Computational graphs solve this.

Lecture 6: Backpropagation

This video from Michigan Online explains why deriving gradients manually is not scalable and introduces computational graphs as the data structure that enables automatic differentiation in deep learning frameworks.

Watch from 01:09 to 07:57. Pay attention to the argument for why a modular, algorithmic approach is necessary for computing gradients in complex models like the ones you'll be building.

As the video shows, even moderately complex neural networks produce computational graphs that are far too large to reason about manually. This is why frameworks like PyTorch and TensorFlow exist—they build this graph for you automatically. Your job is to understand the process they are executing.

This process involves two steps:

  1. Forward Pass: We start with the inputs and flow through the graph from left to right, computing the value at each node until we get the final output.
  2. Backward Pass: We start from the final output and flow backward from right to left, applying the chain rule at each node to compute the gradients. This backward pass is called backpropagation.

3. Backpropagation: The Chain Rule on a Graph

Let's make the backward pass more concrete. To compute the gradient at any node, we need two things:

  1. The local gradient: The derivative of that node's operation with respect to its immediate inputs. (e.g., for a node c = a + b, the local gradients are and ).
  2. The upstream gradient: The gradient of the final output with respect to the node we're at. This is passed to us from the nodes further down the chain (to the right).

The chain rule tells us that the downstream gradient (the gradient we want to pass back to the previous node) is simply:
downstream_gradient = upstream_gradient * local_gradient

Let's watch this in action.

Derivatives With Computation Graphs (C1W2L08)

In this classic video, Andrew Ng from DeepLearningAI provides a crystal-clear, step-by-step example of computing gradients on a computational graph.

Watch the entire video (around 14 minutes). Follow along as he calculates the derivative of the final output J with respect to every single variable (v, a, u, b, c) by moving backward through the graph and applying the chain rule at each step. Note how the calculation for dJ/db reuses the gradient dJ/du that was already computed.

This backward flow is incredibly efficient because it reuses computations. Once we have the gradient of the loss with respect to a layer's output, we can use it to compute the gradients for that layer's weights and pass it backward to the previous layer.

Computational Graph for Chain Rule
This simple graph shows the core idea. To find the gradient of the output Y with respect to an input b (\(\frac{\partial Y}{\partial b}\)), we must trace all paths from b to Y. The path through `d` contributes \(\frac{\partial Y}{\partial d} \cdot \frac{\partial d}{\partial b}\), and the path through `e` contributes \(\frac{\partial Y}{\partial e} \cdot \frac{\partial e}{\partial b}\).

4. The Multi-Path Case: Summing Gradients

The image above hints at an important detail: what if an input variable affects the output through multiple paths?

For example, in the function f(a, b) = (a + b) * a, the variable a is used in two places. A change in a affects f through both the addition and the multiplication.

The multivariable chain rule states that in this case, we must compute the gradient along each path independently and then sum them up.

Computational Graphs, Forward Propagation, and Backpropagation ...

This article provides a simple text-based walkthrough of backpropagation and clearly states the rule for handling multiple paths.

Read the first three sections, up to the first block of Python code. Focus on the equation for ∂F/∂x, which explicitly shows the summation of gradients from the two paths that x takes to the output F. The PyTorch code is a good preview of how you'll implement this later.

This "sum over paths" rule is the final piece of the backpropagation puzzle. It ensures we account for the total influence of a variable on the final output. The article also extends this concept to a simple neural network, showing exactly how the gradients for weights and biases are calculated. If you're curious, you can explore the rest of that article (LINK, sections 3 and 4), which shows a full numerical example and its PyTorch equivalent.

A quick note on vector and matrix operations: while our examples use scalars, real neural networks operate on tensors (vectors, matrices). The principle remains identical, but the local_gradient becomes a Jacobian matrix, and the multiplication becomes a matrix-vector product. Deep learning frameworks handle these complex operations implicitly, but they are all just executing the chain rule you've learned today.


Conclusion

You've just learned the core algorithm that enables deep learning. While it might seem complex at first, it's built from simple, repeatable steps.

Key Takeaways:

  • The Chain Rule allows us to compute the derivative of a composite function by multiplying the derivatives of its constituent parts: .
  • Computational Graphs provide a powerful way to visualize complex functions and modularize the process of differentiation.
  • Backpropagation is the algorithm for efficiently computing gradients on a computational graph. It consists of:
    1. A forward pass to compute the output value.
    2. A backward pass that applies the chain rule from right to left, calculating downstream_gradient = upstream_gradient * local_gradient at each node.
  • If a variable contributes to the output via multiple paths, its total gradient is the sum of the gradients from each path.

We have now concluded the calculus segment of this module. You understand how to measure rates of change (derivatives) and how to propagate them through complex functions (the chain rule).

In our next lesson, we will pivot to the second major pillar of mathematics for machine learning: Probability and Statistics. We will begin by learning how to calculate and interpret fundamental descriptive statistics like mean, variance, standard deviation, and correlation coefficients. These tools are essential for understanding and summarizing your data before you ever begin training a model.