Key Moments
Stanford CS229 Machine Learning | Spring 2026 | Lecture 8: Neural Networks 2 (Backprop)
Want to know something specific about what's covered?
We've already dissected every moment. Ask and we will deliver (with timestamps).
Key Moments
Backpropagation efficiently computes gradients for neural networks by treating computations as a differentiable circuit, enabling training in time proportional to network size.
Key Insights
Backpropagation, or automatic differentiation, allows for the efficient computation of gradients for neural network loss functions with respect to parameters, in time complexity proportional to the number of parameters.
The core idea behind backpropagation is the chain rule, applied iteratively to a computational graph, allowing gradients to be computed from the output layer backward to the input layers.
For a matrix multiplication operation G(Z) = WZ + b, the gradient with respect to the input Z is given by W^T * (dJ/dU), where dJ/dU is the gradient of the loss with respect to the output U.
The gradient of the loss with respect to the weight matrix W in a matrix multiplication G(Z) = WZ + b is given by (dJ/dU) * Z^T. For a single example, this gradient is a rank-one matrix.
For activation functions like sigmoid, the backward pass involves element-wise multiplication of the gradient dJ/dU with the derivative of the activation function applied to the input Z (i.e., sigma'(Z)).
The forward and backward passes of backpropagation have similar time complexities, typically O(N) where N is the number of operations or parameters, making training feasible.
The need for efficient gradient computation
The lecture introduces backpropagation as the fundamental algorithm for computing the gradient of a loss function with respect to the parameters of a neural network. This is crucial for optimization algorithms like Stochastic Gradient Descent (SGD), which require gradients for parameter updates. While the concept of a loss function and SGD was reviewed, the focus is on the technical challenge of efficiently calculating these gradients for complex, multi-layered neural networks. The speaker emphasizes that understanding backpropagation is essential, even with modern auto-differentiation libraries, as it informs architectural decisions and debugging. This algorithm is considered one of the more technical and objectively answerable questions in deep learning, historically probed in interviews by research labs.
Differentiable circuits and the size theorem
The concept of a 'differentiable circuit' is introduced as a general framework for understanding computation. This circuit is composed of arithmetic operations (like addition, multiplication) and elementary functions (like sine, cosine, sigmoid). The key theorem states that if a function can be computed by a differentiable circuit of size N (number of operations), its gradient can also be computed in time O(N). This implies that the forward pass (computing the function) and the backward pass (computing the gradient) have similar time complexities. For neural networks, the number of operations is often proportional to the number of parameters, meaning the gradient computation is roughly as expensive as the forward pass, which is a significant efficiency gain.
The chain rule as the engine of backpropagation
The chain rule from calculus is presented as the core mechanism for backpropagation. It's explained as a way to compute the gradient of a composite function by breaking it down into smaller, manageable steps. If we have a function J that depends on an intermediate variable U, and U depends on an input Z (J = f(U), U = g(Z)), the chain rule allows us to compute the gradient of J with respect to Z (dJ/dZ) if we know the gradient of J with respect to U (dJ/dU). This relationship can be expressed as dJ/dZ = (dJ/dU) * (dU/dZ), where dU/dZ is the Jacobian of g. The key insight is that this process is 'Markovian' in nature: to compute the gradient with respect to an earlier variable, you only need the gradient of the immediate next variable and the local function connecting them, not the entire history of computations. This local computation property enables efficient, modular implementation, with each 'module' in a neural network having its own 'backward' function.
Applying the chain rule to neural network components
The lecture details how to apply the chain rule to specific neural network operations. For matrix multiplication G(Z) = WZ + b, where Z is the input, W is the weight matrix, and b is the bias, the gradient of the loss J with respect to Z (dJ/dZ) is computed as W^T * (dJ/dU). This means the backward pass for matrix multiplication involves multiplying the transpose of the weight matrix by the incoming gradient. For parameters, the gradient of J with respect to W (dJ/dW) is found to be (dJ/dU) * Z^T. This relationship is described as the 'Hessian rule' or 'Hebbian rule' in some contexts, where the update to a weight is related to the gradient at the output and the input value at the other end of the connection. Notably, for a single training example, the gradient matrix dJ/dW is rank-one, a property exploited for efficiency.
Backpropagation for activation functions
The backpropagation process for activation functions, such as the sigmoid function, is also explained. If U = sigma(Z), where sigma is the activation function, the gradient dJ/dZ is computed by taking the gradient dJ/dU and performing an element-wise multiplication with the derivative of the activation function applied to Z (i.e., sigma'(Z)). This operation can be efficiently represented by constructing a diagonal matrix using sigma'(Z) and multiplying it with dJ/dU, or more compactly, by taking the element-wise (Hadamard) product of the vector sigma'(Z) with dJ/dU. This highlights that for element-wise operations, the backward pass is also element-wise, maintaining computational efficiency.
Handling parameters and computation order
The lecture clarifies that the ultimate goal is to compute gradients with respect to the network's parameters (weights and biases), not just intermediate activations. The computational graph can be thought of as having two types of inputs: the data (X) and the parameters (theta). When computing dJ/dtheta for a module, the data X is treated as fixed, and the module is viewed as a function mapping theta to its output. This allows the same chain rule mechanism to be applied. The process involves two phases: first, computing gradients with respect to intermediate variables (activations) by going backward through the network; second, branching off to compute gradients with respect to parameters. The order of these computations is important for memory management and potential parallelization, as gradients for earlier parameters can be computed without necessarily backpropagating through the entire network.
Efficiency considerations and memory management
The lecture concludes by reiterating the efficiency claim: the backward pass takes roughly the same amount of time as the forward pass, both being proportional to the number of operations or parameters. Techniques like dropping intermediate computed values after their gradients have been used can help manage memory during training. Understanding the computational graph and the dependencies between variables is crucial for optimizing the order of operations, memory allocation, and communication between GPUs, all of which are vital for training large-scale deep learning models.
Mentioned in This Episode
●Software & Apps
Common Questions
Backpropagation is the algorithm used to compute the gradient of the loss function with respect to the parameters of a neural network. It's crucial because it enables efficient training of complex models by guiding parameter updates, and it's automatable through auto differentiation.
Topics
Mentioned in this video
More from Stanford Online
View all 106 summaries
77 minStanford CS229 Machine Learning | Spring 2026 | Lecture 18: GMM (EM), PCA
78 minStanford CS229 Machine Learning | Spring 2026 | Lecture 14: Transformers, In-Context Learning
79 minStanford CS229 Machine Learning | Spring 2026 | Lecture 20: GMM (EM), PCA
81 minStanford CS229 Machine Learning | Spring 2026 | Lecture 10: GMM (EM), PCA
Ask anything from this episode.
Save it, chat with it, and connect it to Claude or ChatGPT. Get cited answers from the actual content — and build your own knowledge base of every podcast and video you care about.
Get Started Free