Skip to main content

Welcome to a Grasp lesson

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

Lesson 41

Multivariable Chain Rule for Computation Graphs

Recommended resources

Introduction

In our last lesson, we established a powerful way to represent complex models like neural networks: the computation graph. We saw how the "forward pass"—propagating input values through this graph of elementary operations—calculates the final output, which for our purposes is the loss function. This gave us a clear, structured view of how a model makes a prediction.

Now, we move to the central question of optimization: if our model's prediction is wrong (i.e., the loss is high), how do we adjust its internal parameters to improve it? To do this, we need to understand how a small change in any part of the model affects the final loss. This lesson focuses on the crucial step of calculating this sensitivity for the intermediate variables in our graph. We will introduce the multivariable chain rule not as an abstract formula, but as an intuitive rule for propagating "influence" backward through the graph. By the end of this lesson, you'll be able to trace how a change at the end of the graph ripples backward to affect the values in the middle, a process that forms the core of learning in neural networks.


The Chain Rule: Propagating Sensitivity

Before tackling multiple variables, let's revisit the single-variable chain rule from basic calculus. If we have a sequence of operations, say and , the chain rule tells us how to find the derivative of with respect to :

This isn't just a formula; it's a statement about propagating sensitivity. The sensitivity of the output to the input is the product of the sensitivity of to the intermediate variable and the sensitivity of to .

The following video provides a wonderful analogy for this, envisioning functions as interconnected cogwheels.

The Most Important Algorithm in Machine Learning

This video by Artem Kirsanov explains the chain rule with a beautiful mechanical analogy.

Watch the section on the chain rule, which uses an analogy of interconnected cog wheels. This helps build intuition that the overall rate of change is the product of the individual rates of change at each stage.

In the context of our computation graph, if we have a simple chain , the gradient is found by multiplying the local gradients along the path from back to .


The Multivariable Chain Rule: Summing Over Paths

This concept becomes more interesting when a single variable can influence the output through multiple paths. This is the norm in neural networks. Consider the function from our previous lesson, . Let's represent it with intermediate variables and , so .

This computation graph illustrates the function \(f = (a+b)(b+1)\). Notice how the input `b` influences the final output `f` through two distinct paths: one via the top addition node (`x`) and one via the bottom addition node (`y`).

To find the derivative of the final output with respect to the input , , we can't just multiply local derivatives along a single path. We have to account for both ways that influences .

The multivariable chain rule gives us an elegant way to handle this: sum the contributions from all paths.

  1. Path 1: . The contribution from this path is .
  2. Path 2: . The contribution from this path is .

The total derivative is the sum of these contributions:

This "sum over paths" rule is the most important intuition for understanding how gradients propagate in complex systems. Christopher Olah's blog post, which we touched on previously, explains this concept perfectly.

Calculus on Computational Graphs: Backpropagation - Colah's Blog

This section of Colah's blog post introduces the "sum over paths" rule for computing derivatives on a graph.

Please read the section "Derivatives on Computational Graphs." Focus on the example of calculating \frac{\partial e}{\partial b}. The key idea is explained in the paragraph starting The general rule.

This idea of summing paths is fundamental. It's conceptually similar to how you might analyze influence in a biological network, like a gene regulatory network. If a transcription factor influences a final phenotype through multiple downstream genes, its total effect is a combination of its influence through each of those pathways.

Let's work through a more detailed example to solidify this.

Computational graphs and gradient flows

This tutorial walks through a more complex graph, explicitly demonstrating the multivariable chain rule when a node has multiple dependents.

Start at the heading "Example: a comprehensive Computational Graph" and read down to the calculation for \frac{\partial g}{\partial b}. Pay close attention to the section where the text explains that to calculate the gradient with respect to b, you must use the multivariable chain rule because b feeds into both c and d. Follow the derivation that shows how the total gradient is the sum of products of gradients along the two distinct paths from g back to b.


The Backward Pass: A Step-by-Step Propagation

We now have the core mechanism to calculate the gradient of the final output with respect to any intermediate node. The process of starting from the end of the graph and working our way backward, computing the gradient at each node, is called the backward pass. This is the computational heart of backpropagation.

Here’s the general algorithm for a single node in the graph:

  1. We start with the "upstream gradient"—the gradient of the final loss with respect to this node's output. For the very last node in the graph, this is simply .
  2. The node has already computed its "local gradient" during the forward pass—the derivative of its output with respect to its input(s).
  3. The "downstream gradient"—the gradient of the final loss with respect to this node's input(s)—is calculated by the chain rule:
  4. This downstream gradient then becomes the upstream gradient for the next node in the backward pass.

If a node's output is used in multiple places (a fork in the forward pass), then during the backward pass, it will receive an upstream gradient from each of those places. In this case, we sum these incoming gradients before multiplying by the local gradient.

Andrej Karpathy's classic lecture from Stanford's CS231n course provides an exceptionally clear walkthrough of this process.

CS231n Winter 2016: Lecture 4: Backpropagation, Neural Networks 1

In this excerpt from his lecture, Andrej Karpathy steps through the backward pass on a simple graph, cementing the pattern of multiplying the local gradient by the "gradient from above".

First, watch the walkthrough of a simple graph with inputs x, y, and z. He begins the backward pass at the base case, where the gradient of the output with respect to itself is 1. Then, follow his application of the chain rule as he steps back through the multiplication gate (gradient on Z and Q). The key insight is summarized when he describes what a "gate" does during backpropagation. He explains how to calculate the downstream gradients by combining the upstream gradient (which he calls DL/DZ or the "gradient from above") with the local gradients (DZ/DX, DZ/DY). Watch this explanation from "you are a gate".

Let's see this in action on a slightly more complex graph, representing a single sigmoid neuron. This will look much more like what happens inside a real neural network.

CS231n Winter 2016: Lecture 4: Backpropagation, Neural Networks 1

Continuing with the same lecture, Karpathy now applies the same logic to a more realistic computation graph.

Watch the detailed, step-by-step backpropagation through the graph of a sigmoid function, from the end of the circuit. For each gate (1/x, +1, exp, etc.), notice the pattern: He identifies the local gradient (e.g., for 1/x, it's -1/x^2). He takes the upstream gradient (the gradient flowing back from the next node). He multiplies them to get the new downstream gradient. Observe what happens at the + gate around the 'plus' operation. Since the local gradient of addition is 1, the + gate acts as a "gradient distributor," passing the upstream gradient unchanged to all its inputs.

A conceptual visualization of the forward (blue) and backward (red) passes. During the backward pass, the gradients (denoted by δF/δX, etc.) are computed by applying the chain rule, propagating sensitivity from the final output F back to the inputs.

Conclusion

In this lesson, we've established the core engine of backpropagation: the multivariable chain rule. You now understand that calculating how an intermediate variable affects the final loss is a matter of tracing all paths from that variable to the output, multiplying the local derivatives along each path, and summing the results. We've formalized this into the "backward pass," an algorithm that systematically computes the gradient of the loss with respect to every intermediate node in our computation graph.

Key Takeaways:

  • The multivariable chain rule can be intuitively understood as a "sum over paths" rule on a computation graph.
  • The effect of a variable on the output is the sum of its effects through all the pathways that connect it to the output.
  • The backward pass is the recursive application of the chain rule, starting from the output and moving backward.
  • The fundamental step in the backward pass is: downstream_gradient = upstream_gradient * local_gradient.
  • Nodes where paths merge (e.g., addition) sum gradients during the backward pass.

We are now just one step away from our goal. We know how to calculate the gradient of the loss with respect to any intermediate node. In our next lesson, we will see that the parameters of our model—the weights and biases—are simply input nodes to this graph. By continuing the backward pass all the way to the beginning, we can efficiently compute the gradient of the loss with respect to every single parameter. This complete algorithm is backpropagation, the cornerstone of training deep neural networks.