Hello! Welcome to the first lesson in our course. We're kicking off the Neural Network Foundations module, which is all about building a solid theoretical and practical understanding of how deep learning models work.
Today, our goal is to derive the backpropagation algorithm for a simple, fully-connected neural network. You've mentioned you have experience building models with frameworks like PyTorch, but lack a deep theoretical grasp of their inner workings. This lesson is designed to pull back the curtain. Understanding backpropagation is a fundamental step towards becoming the kind of engineer who can not only use tools but also diagnose, debug, and innovate—a key part of your goal to be "irreplaceable" in the field.
We'll start with the core concepts of how a network makes predictions and how we measure its error. Then, using basic calculus, we'll figure out exactly how to adjust the network's parameters to improve its performance.
1. The Goal: Learning by Minimizing Error
A neural network is essentially a mathematical function parameterized by weights () and biases (). During a forward pass, an input travels through the network's layers, and the network produces an output, or prediction, which we'll call .
The process at each layer looks like this:
- Weighted Sum (Pre-activation): A linear combination of the previous layer's activations is computed.
- Activation: A non-linear activation function (like Sigmoid or ReLU) is applied to the weighted sum.
The final activation, , is the network's prediction .
To learn, we need a way to measure how wrong our network's prediction is. We use a loss function (or cost function), , which compares the prediction to the true target . For simplicity in our derivation, we'll use the Mean Squared Error (MSE) loss, though the principles apply to others like Cross-Entropy.
(The is just a mathematical convenience that will simplify our derivatives later.)
The goal of training is to find the weights and biases that minimize this loss. We do this using an optimization algorithm called gradient descent. The idea is to calculate the gradient of the loss function with respect to all the parameters. The gradient, , is a vector of partial derivatives () that points in the direction of the steepest increase in the loss. To minimize the loss, we take small steps in the opposite direction:
Here, represents any parameter (a weight or bias) and is the learning rate.
The central question then becomes: How do we efficiently compute the gradient ? This is where backpropagation comes in.
2. The Core Mechanism: The Chain Rule
Backpropagation is a clever application of the chain rule from calculus. It allows us to calculate how much a small change in any weight or bias, even in the earliest layers, will affect the final loss.
Let's start with a single weight in the final layer, , connecting the last hidden layer's activation to the final output neuron. To find its contribution to the loss, we need to compute .
We can trace the influence of this weight forward:
- A change in affects the weighted sum .
- A change in affects the final activation (our prediction ).
- A change in affects the loss .
The chain rule lets us express this relationship as a product of derivatives:
To see a great visual intuition of this process, let's turn to a classic video from 3Blue1Brown.
Backpropagation calculus | Deep Learning Chapter 4
This segment from 3Blue1Brown's series on neural networks provides an excellent intuitive walkthrough of how the chain rule is applied to find the derivative of the cost with respect to a single weight.
Please watch from 00:43 to 05:28. Focus on how a change in a single weight in the final layer propagates through the network to affect the final cost. Pay attention to the three ratios that make up the chain rule expression and what each one represents.
As the video explained, we can calculate each of those three parts:
-
: The derivative of the loss with respect to the output activation.
- For our MSE loss , this is simply . This term represents the output error.
-
: The derivative of the activation function.
- If we're using the sigmoid function, , its derivative is . We can write this as .
-
: The derivative of the weighted sum with respect to the weight.
- For , this is just the activation from the previous layer, .
Putting it all together for a weight in the output layer:
What about the bias, ? The derivation is almost identical, but the last term changes. Since , the gradient for the bias is:
3. Propagating the Error Backward
Now for the main event: calculating the gradient for a weight in a hidden layer, say in layer . This is the "propagation" part of backpropagation.
A neuron in a hidden layer affects the loss through all the neurons it connects to in the next layer. This is a crucial point.

To calculate , we first need to find the "error" at the output of hidden layer , which is . A change in affects the loss through its influence on the next layer's weighted sum, .
Using the chain rule, the error at a hidden neuron's activation is the error from the next layer (), weighted by the connection weight ():
If neuron connects to multiple neurons in layer , we must sum up all these weighted errors.
This recursive nature is what makes backpropagation efficient. We can define an error term, , for each neuron in layer as the partial derivative of the loss with respect to its pre-activation input, .
With this notation, we can establish two key relationships:
- Error at the output layer ():
- Error at a hidden layer ():
This second equation is the heart of backpropagation. It shows how the error at layer is calculated from the weighted sum of errors at layer .
Let's walk through a formal derivation to make this concrete.
Understanding the Math Behind Backpropagation
This article provides a clear, step-by-step calculation for a simple two-layer network. It will solidify your understanding of the equations we've discussed.
Please read the sections 'Backward Pass', 'Output Layer Derivatives', and 'Hidden Layer Derivatives'. Pay close attention to how the multivariable chain rule is used and how the derivatives for the hidden layer depend on the derivatives already computed for the output layer. Focus on understanding the final equations for the gradients.
Test your understanding!
Looking at the final equation for in the article, why is the term from the second layer present in the gradient calculation for a weight in the first layer?
Show answer
The term is present because the first layer's weights influence the final loss through the second layer. The backpropagation algorithm calculates the error at the hidden layer by taking the error from the output layer and "propagating" it backward. This backward-propagated error is weighted by the strengths of the connections between the hidden and output layers, which are precisely the weights of the second layer, .
4. The Four Core Equations of Backpropagation
From the derivations, we can summarize the entire backpropagation algorithm into four fundamental equations. For a given training example:
-
Forward Pass: Compute all activations and weighted sums for .
-
Output Error (): Compute the error vector for the output layer. In vectorized form:
( denotes element-wise multiplication. is the gradient of the loss with respect to the output activations, e.g., for MSE). -
Backpropagate Error (): For each layer , compute the error:
-
Compute Gradients: The gradients of the loss with respect to the weights and biases of each layer are:
These four equations give us a complete and efficient procedure for calculating the gradient of the loss function with respect to every single parameter in the network.
Conclusion
In this lesson, we have demystified the "magic" of how neural networks learn. We started from a simple loss function and, using the chain rule, derived the backpropagation algorithm from first principles.
Key Takeaways:
- Training a neural network involves minimizing a loss function using gradient descent.
- Backpropagation is an efficient algorithm for computing the gradient of the loss function with respect to all network parameters.
- The algorithm works by first performing a forward pass and then propagating an "error signal" backward from the output layer to the input layer.
- The error at any hidden layer is a weighted sum of the errors from the layer that follows it.
This foundational knowledge is critical. It separates you from someone who can only call model.fit() and enables you to reason about why your model might not be training correctly.
Preview of the Next Lesson:
Theory is essential, but practice is where understanding truly solidifies. In our next lesson, you will take these four equations and implement forward and backward propagation from scratch in Python for a multi-layer perceptron. This will be a challenging but incredibly rewarding exercise.