Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our last lesson, we successfully derived the gradient of the loss function with respect to the final layer's logits, . For the common case of softmax activation and cross-entropy loss, this gave us the elegant result . This gradient is the initial "error signal" that tells our network how wrong its final output was.
Now, we will tackle the central algorithm of neural network training: backpropagation. Our goal is to derive a method to take this initial error signal and propagate it backward through every layer of the network, layer by layer, to compute the gradient of the loss function with respect to every weight and bias. This is the ultimate application of the multivariable chain rule you've mastered. Backpropagation isn't an optimization algorithm itself; rather, it's a highly efficient computational procedure for calculating the gradients that optimizers like SGD need to update the model's parameters.
Backpropagation as Flow on a Computational Graph
Before diving into the matrix-vector calculus, it's incredibly insightful to visualize a neural network as a computational graph. Your background in computer science makes this a natural starting point. In this view, every mathematical operation—a matrix multiply, a vector addition, an activation function—is a "node" or "gate" in a directed acyclic graph.
The forward pass involves feeding inputs (, weights , biases ) into the graph and flowing them through the gates to compute the final loss, . The backward pass, or backpropagation, is the process of starting from the final loss and flowing gradients backward through the graph.
At each gate, we perform a simple, local calculation. The gate receives a gradient from "upstream" (closer to the loss function) and uses its own local gradient to compute the gradients for its inputs, which it then passes "downstream" (closer to the network inputs). The chain rule is the mechanism that governs this flow, telling us that to find the gradient of a downstream variable, we multiply the upstream gradient by the local gradient.
To build a solid intuition for this process, let's watch a segment from Andrej Karpathy's renowned lecture on this topic. He masterfully illustrates this concept with a simple scalar-valued graph.
CS231n Winter 2016: Lecture 4: Backpropagation, Neural Networks 1
This video, "CS231n Winter 2016: Lecture 4," introduces backpropagation from the perspective of computational graphs.
Watch from the simple graph example. Focus on how he performs the forward pass (green numbers) and then the backward pass (red numbers). Pay close attention to the concepts of "local gradient" and "upstream gradient" and how the chain rule (simple multiplication in this scalar case) combines them at each gate.
The Four Fundamental Equations of Backpropagation
With the computational graph intuition in place, let's formalize this for a multi-layer neural network. We will derive four fundamental equations that constitute the backpropagation algorithm.
First, let's establish our notation for an arbitrary layer :
- and : The weight matrix and bias vector for layer .
- : The activation vector from the previous layer, which is the input to layer .
- : The pre-activation (or "logit") vector for layer , computed as .
- : The element-wise activation function (e.g., ReLU, sigmoid).
- : The activation vector for layer , computed as .
- : The scalar loss function at the end of the network.
Our core quantity of interest is the error at each layer , which we define as the gradient of the loss with respect to the pre-activation vector :
This vector tells us how the final loss is influenced by the logits at layer . If we can compute for every layer, we can easily find the gradients for the weights and biases.
We will now derive the four equations that allow us to do this. The following resource provides an exceptionally detailed, first-principles derivation, which we will use as our primary guide.
The Math behind Neural Networks - Backpropagation
This article by Jason Osajima is a meticulous, step-by-step derivation of the backpropagation equations for a 3-layer network. We will generalize its logic.
As we derive each equation below, I will ask you to refer to specific parts of this article to see the full mathematical machinery, including the Jacobian matrices, laid out explicitly.
1. Error in the Output Layer ()
This is the starting point of the backward pass. The error at the final layer is given by:
where denotes element-wise (Hadamard) product.
As we discovered in the previous lesson, for the most common classification setup (Softmax activation and Cross-Entropy loss), this equation has a beautifully simple form:
Where is the vector of predicted probabilities () and is the one-hot encoded true label vector. This is our initial "prediction - label" error.
2. Error Propagation ( in terms of )
This is the recursive heart of backpropagation. It tells us how to compute the error at layer once we know the error at layer .
Question: Using the chain rule, how can we express in terms of ? What intermediate variable connects to ?
Answer
The variable connecting them is the activation of layer $l$, $\mathbf{a}^{(l)}$. The full chain is $\mathcal{L} \rightarrow \mathbf{z}^{(l+1)} \rightarrow \mathbf{a}^{(l)} \rightarrow \mathbf{z}^{(l)}$. Therefore, the chain rule gives: $$ \frac{\partial \mathcal{L}}{\partial \mathbf{z}^{(l)}} = \left(\frac{\partial \mathbf{z}^{(l+1)}}{\partial \mathbf{a}^{(l)}}\right)^T \frac{\partial \mathcal{L}}{\partial \mathbf{z}^{(l+1)}} \odot \frac{\partial \mathbf{a}^{(l)}}{\partial \mathbf{z}^{(l)}} $$ Note the transpose on the Jacobian. In element-wise form, this is: $$ \delta_j^{(l)} = \frac{\partial \mathcal{L}}{\partial z_j^{(l)}} = \sum_k \frac{\partial \mathcal{L}}{\partial z_k^{(l+1)}} \frac{\partial z_k^{(l+1)}}{\partial a_j^{(l)}} \frac{\partial a_j^{(l)}}{\partial z_j^{(l)}} $$Let's evaluate the pieces.
- (by definition).
- (derivative of the activation function).
- Since , the partial derivative is simply the weight .
Putting this together in matrix form yields the second fundamental equation:
This equation shows how the error signal is transformed by the transpose of the weights of the next layer, and then modulated by the local gradient of the activation function.
To see this derivation with all the intermediate Jacobians, please review the article.
The Math behind Neural Networks - Backpropagation
Focus on the section that derives the gradients for the second layer.
Study the derivation from the text from start to finish. Pay special attention to how the author computes \boldsymbol{\delta}^{[2]} from \delta^{[3]} and \textbf{W}^{[3]T}. This directly corresponds to our Equation 2.
This recursive relationship is visualized below. The error from a later layer is passed backward through the transposed weights to compute the error for the current layer.

3. Gradient with respect to Biases ()
Once we have the error for a layer, finding the gradient for its bias vector is straightforward.
Question: Given that and , use the chain rule to find an expression for . What is the Jacobian ?
Answer
The chain rule gives $\frac{\partial \mathcal{L}}{\partial \mathbf{b}^{(l)}} = \left(\frac{\partial \mathbf{z}^{(l)}}{\partial \mathbf{b}^{(l)}}\right)^T \frac{\partial \mathcal{L}}{\partial \mathbf{z}^{(l)}}$. Since a change in $\mathbf{b}^{(l)}$ causes an identical change in $\mathbf{z}^{(l)}$ (it's just an addition), the Jacobian $\frac{\partial \mathbf{z}^{(l)}}{\partial \mathbf{b}^{(l)}}$ is the identity matrix, $\mathbf{I}$. Therefore, $\frac{\partial \mathcal{L}}{\partial \mathbf{b}^{(l)}} = \mathbf{I}^T \delta^{(l)} = \delta^{(l)}$.This gives us our third fundamental equation:
The gradient for the biases is simply the error at that layer.
4. Gradient with respect to Weights ()
Finally, we find the gradient for the weight matrix . This gradient tells us how to adjust the connection strengths. Similar to the bias, we apply the chain rule:
We know . From the definition , we can see that . The partial derivative is simply .
Thus, . When we assemble this for all and , we get the fourth fundamental equation in matrix form:
This is an outer product between the error vector and the activation vector from the previous layer . Intuitively, the change to a weight is proportional to the error at its destination neuron and the activation of its source neuron. This is a mathematical formalization of Hebbian learning ("neurons that fire together, wire together").
The LINK resource derives this result in the sections you've just reviewed.
The Backpropagation Algorithm Summarized
With these four equations, we can now state the full algorithm.
- Input: A set of training examples. For each example :
- Forward Pass: Set . For each layer , compute:
- (During this pass, you must cache the and values for use in the backward pass. This is a key point for implementation and relates to your interest in systems performance, as it implies a memory cost.)
- Backward Pass:
- Compute the output error using Equation 1.
- For , propagate the error backward by computing using Equation 2.
- Compute the desired gradients for all layers using Equations 3 and 4:
- Gradient Descent Step: Average the gradients over a mini-batch of examples and use an optimizer (like SGD or Adam) to update all and .
Conclusion
You have now derived the complete backpropagation algorithm from first principles. By viewing the network as a computational graph and recursively applying the chain rule, we arrived at four simple, powerful equations that allow us to efficiently compute the gradient of the loss with respect to every parameter in the network.
Key Takeaways:
- Backpropagation is an algorithm for computing gradients by recursively applying the chain rule.
- It can be understood as passing error signals backward through a computational graph.
- The error is the core quantity, representing the gradient of the loss with respect to a layer's logits.
- The four fundamental equations provide a complete recipe for computing all necessary gradients:
- (for Softmax/CE)
Preview of the Next Lesson:
Look closely at Equation 2. The error signal is repeatedly multiplied by weight matrices as it propagates backward (). If the values in these matrices are consistently large or small, what might happen to the magnitude of the gradient in the early layers of a deep network? This line of inquiry leads directly to the notorious problems of exploding and vanishing gradients, which we will dissect in our next lesson.