Hello! In our last lesson, you did an excellent job implementing dropout, a powerful regularization technique, into our network's forward pass. Our Multi-Layer Perceptron (MLP) can now make predictions and is structured to avoid overfitting. However, it's still missing the most crucial part: the ability to learn from its mistakes.
This lesson dives into the heart of how neural networks learn: backpropagation. Our goal is to manually derive the gradients for a simple two-layer network using the chain rule. This process calculates how much each weight and bias in the network contributed to the final error. By understanding this, we can later write the code to update these parameters and make the network smarter with each training example.
We will focus on the mechanics of this process, building the intuition needed to translate these mathematical concepts into code in our next lesson.
1. The Big Picture: Computational Graphs and the Chain Rule
At its core, a neural network is a large, nested mathematical function. The forward pass is simply the evaluation of this function. To figure out how to adjust the parameters (weights and biases) to reduce the error, we need to calculate the derivative of the loss function with respect to each of these parameters. Doing this for a deep network with millions of parameters seems daunting, but backpropagation is a clever algorithm that makes it efficient.
Backpropagation is essentially an organized application of the chain rule from calculus, applied backward through the network's structure, which can be visualized as a computational graph.
To build a strong intuition for this, let's watch a segment from Stanford's renowned deep learning course, CS231n.
CS231n Winter 2016: Lecture 4: Backpropagation, Neural Networks 1
In this video, Andrej Karpathy explains how to think about backpropagation using computational graphs. This conceptual view is fundamental to understanding how modern deep learning frameworks are designed.
Watch the segment from 05:13 to 14:59. It first walks through a simple example f(x, y, z) = (x + y) * z to introduce the concepts of 'local gradient' and the 'global' or 'upstream' gradient. Then, it generalizes the idea of a 'gate' in the circuit and how the chain rule (simple multiplication) connects these gradients.
As you saw, the key idea is to break a complex function into simple stages or "gates." During the backward pass, each gate is responsible for one thing: taking the gradient from the stage after it (the upstream gradient) and multiplying it by its own local gradient to pass it to the stage before it.

2. A Concrete Example: Deriving Gradients by Hand
Now let's apply this concept to a simple two-layer neural network. We'll walk through a complete example with specific numbers, which is often the best way to solidify understanding.
The following video provides a step-by-step walkthrough of both a forward and backward pass for a small network. We'll go through it in parts.
Backpropagation in Neural Network with an Example By hand - TensorFlow Tutorial
The video 'Backpropagation in Neural Network with an Example By hand' from Koolac provides an excellent, detailed calculation that we will follow closely.
First, watch from 00:49 to 05:23. This part sets up the network architecture, performs the forward pass to get a prediction (y_hat), and calculates the Mean Squared Error (MSE) loss. Pay close attention to how the output of one neuron becomes the input for the next.
The Setup
The video uses a network with:
- 2 input neurons
- 2 hidden neurons with ReLU activation
- 1 output neuron with a linear activation (i.e., no activation function)
- A single training example: input with a true target .
- The loss function is .
The forward pass calculated a prediction of , resulting in a loss of . Now, our goal is to find for every weight in the network.
Step 1: Gradients for the Output Layer ()
The weights and bias of the output layer directly influence the final loss. Let's start with . The loss is a function of , which is a function of .
Using the chain rule:
- : The derivative of with respect to is . This is the upstream gradient.
- : Since , the derivative with respect to is simply . This is the local gradient.
So, .
Now, watch how this is calculated in the video.
Backpropagation in Neural Network with an Example By hand - TensorFlow Tutorial
Continuing with the same video.
Watch from 05:23 to 09:41. This section calculates the gradients for the output layer's parameters (W5 and Bias 3). It also introduces the concept of updating the weights using a learning rate.
Step 2: Gradients for the Hidden Layer ()
This is where the "back" part of backpropagation becomes clear. The weight doesn't directly affect the loss . It affects , which affects , which in turn affects . We need to chain all these dependencies together.
Where is the pre-activation value of the first hidden neuron. Let's break this down:
- : The gradient we already know from the output layer.
- : The derivative of with respect to is . This tells us how much contributed to the final output.
- : The derivative of the ReLU activation function. It's 1 if and 0 otherwise.
- : The derivative of with respect to is .
The video walks through this multi-step chain rule application with concrete numbers, making it much easier to follow.
Backpropagation in Neural Network with an Example By hand - TensorFlow Tutorial
This is the most critical part of the video, showing how the error is propagated backward through the layers.
Watch from 09:41 to 19:36. Pay close attention to how the chain rule is used to find the gradient with respect to W1. The video explicitly breaks down the calculation into finding dL/dh1 first, and then dh1/dW1. It also shows examples for a bias (Bias 2) and a weight with zero gradient (W3).
Test your understanding!
In the video, the gradient turned out to be zero. Why did this happen? Trace the dependencies from to the final loss .
Show answer
The gradient was zero because of the specific input values used in the example. is multiplied by the input , which has a value of 0.
The pre-activation for the first hidden neuron is .
When calculating , the result is . Since , the local gradient is 0.
When the chain rule multiplies all the gradients together, this 0 causes the entire gradient to become 0. This means for this specific training example, changing has no effect on the loss.
3. Generalizing to Vectorized Equations
The manual, scalar-by-scalar calculation is excellent for understanding, but in practice, we use matrices and vectors for efficiency. Your background in NumPy makes you well-prepared for this transition. The logic remains the same, but the operations are performed on entire batches of data at once.
Let's look at the final, clean, vectorized formulas for a two-layer network. These are the equations you will implement in the next lesson.
Building a Neural Network From Scratch — Just Numpy
The article 'Building a Neural Network From Scratch — Just Numpy' summarizes the backpropagation derivations into the final vectorized formulas. This is a perfect bridge from the manual math to the code we will write.
Read the section 'Backward Propagation'. It lists the six key gradient equations (dZ[2], dW[2], dB[2], dZ[1], dW[1], dB[1]). Try to map each formula back to the steps we saw in the video. For instance, dZ[1] = W[2]ᵀ dZ[2] * g′(Z[1]) corresponds to backpropagating the error from the output layer (dZ[2]) through the weights (W[2]ᵀ) and then through the hidden layer's activation function (g′(Z[1])).
For a more formal mathematical treatment, the document "Backpropagation Derivation for a Two-Layer Neural Network" (LINK) lays out each step of the chain rule using matrix calculus notation. The "Summary of Gradients" section provides the same final equations we will use.
Conclusion
In this lesson, we demystified the core mechanism of network training. By peeling back the layers of abstraction, you've seen that backpropagation is not magic, but a systematic application of the chain rule.
Key Takeaways:
- Computational graphs help visualize a neural network as a series of simple operations ("gates").
- Backpropagation is the process of moving backward through this graph, calculating the gradient of the loss with respect to each parameter.
- The Chain Rule is the engine of backpropagation. At each gate, the gradient is calculated as:
downstream_gradient = upstream_gradient * local_gradient. - We manually derived the gradient formulas for all weights and biases in a simple two-layer network, first with scalars and then with generalized vector equations.
Preview of the Next Lesson:
With the "why" and "how" of the math covered, we are ready to code. In the next lesson, you will implement the backpropagation algorithm to compute gradients for all parameters in our deep MLP. We will add a backward method to our DenseLayer and MLP classes, bringing our from-scratch network one step closer to a complete training loop.