Modern neural networks often consist of multiple layers and a large number of parameters, sometimes reaching millions. The central challenge in training such networks is determining how to efficiently update these parameters so that the modelβs predictions improve over time. This learning process is achieved through an algorithm known as backpropagation, which systematically computes gradients of the loss function with respect to each parameter in the network.
Why Backpropagation Is Needed
Before understanding backpropagation, let us recall how learning happens in machine learning.
The goal of training is to minimize a loss function by adjusting the model's parameters, such as its weights and biases.
A basic gradient-descent algorithm follows an iterative process:
Perform a forward pass to generate predictions.
Compute the loss by comparing the predictions with the actual targets.
Calculate how the loss changes with respect to each parameter.
Update the parameters in the direction that reduces the loss.
Repeat the process until the loss becomes sufficiently small.
For a weight w, the update rule is:
wβwβΞ±βwβEβ
For the complete weight vector w:
wβwβΞ±βwβE(w)
where:
Ξ± is the learning rate.
E(w) is the loss function.
βwβE(w) is the gradient of the loss with respect to the weights.
For simple machine-learning models such as linear regression, computing these gradients is relatively straightforward because the relationship between the weights and the output is direct.
Deep learning, however, introduces a more difficult challenge.
A deep neural network may contain:
Multiple hidden layers
Thousands, millions, or billions of parameters
Complex chains of mathematical operations
During forward propagation, input features travel through one layer after another until the network produces a prediction.
The prediction is then compared with the true target to calculate a single loss value.
At this point, an important question arises:
Which specific parameters in the network were responsible for the error?
The final loss depends on every parameter in the network, often through many intermediate calculations.
A weight in an early layer may affect the output only indirectly through several hidden layers. Calculating the contribution of every weight manually would be extremely difficult and inefficient.
This is where backpropagation becomes essential.
Backpropagation efficiently calculates gradients for all trainable parameters by moving backward from the output layer toward the input layer.
It relies on three main ideas:
The Chain Rule of Calculus
The chain rule decomposes complex derivatives into a sequence of simpler derivatives.
Gradient Computation
Gradients measure how sensitive the loss is to changes in each parameter.
Error Propagation
The error signal is passed backward through the network so that responsibility for the prediction error can be distributed across all layers.
Rather than calculating every gradient independently, backpropagation reuses intermediate computations from the network.
This makes training computationally feasible even for very large neural networks.
Backpropagation computes the gradients, while the optimizer uses those gradients to update the parameters.
Without backpropagation, training modern deep-learning models containing millions or billions of parameters would be prohibitively slow and computationally impractical.
Build Intuition of Chain Rule of Calculus
Consider we have a function Β g(f(x))
Chain Rule of Calculus (scaler case):
Consider,
z=f(x)=x2
y=g(z)=ez=ex2
We can calculate dzdyβ and dxdzβ easily.
dzdyβ=ez,dxdzβ=2x
But how would you calculate dxdyβ here? We have to apply chain rule here.
dxdyβ=dzdyβ.dxdzβ=ez.2x=ex2.2x
Chain Rule of Calculus (vector case):
Here each ziβ is a function of
x=βx1βx2β..xmβββ
and y is a function of z=βz1βz2β..znβββ. So how do we calculate βxiββyβ for any xiβ?
Now to understand it simply, earlier for scaler case we just had only one path from x to z to y (xβzβy). Now for vector case here we have three paths from xiβ to y (considering n=3). So we have to apply chain rule in all the three paths and take the summation.
We want to train the model, that means we came up with the neural network model structure but we want to find the optimized weight parameters here. Weight parameters are named as wji(l)β and it means the weight from node i from previous layer to node j in the current layer and current layer is l (starting from the first hidden layer).
How do we find the optimizer proper weights? We run Gradient Descent algorithm and weight parameters are updated in each iteration.
wt+1=wtβΞ±βwtβEβ
wt contains all the weight parameters wji(l)β.
So at each iteration, our target is to find βwji(l)ββEβ for all the weight parameters.
What is E here?
E is the Error or Loss we found comparing the actual output (y) and predicted output (y^β).
Loss/Error Functions
There are various types of Loss/Error functions depending on the task we want to perform. Here are few examples..
Mean Square Error(MSE)
Used mostly in linear regression problems.
E=n1βi=1βnβ(yiββyiβ^β)2
Root Mean Square Error(RMSE)
Just taking square root of Mean Square Error
E=n1βi=1βnβ(yiββyiβ^β)2β
Mean Absolute Error (MAE)
Sum of absolute differences between actual and predicted outputs
E=n1βi=1βnββ£yiββyiβ^ββ£
Binary Cross-Entropy
Used in binary classification
E=βylog(y^β)β(1βy)log(1βy^β)
Categorical Cross-Entropy
Used in multi-class classification when outputs are represented as one-hot encoding format and there are K classes
E=βk=1βKβykβlog(ykβ^β)
Sparse Categorical Cross-Entropy
Used in multi-class classification when outputs are represented with class indices (1,2,..K), y is the correct class index and y^βyβ is the predicted probability of correct class y
E=βlog(y^βyβ)
Backward Propagation of Errors: Step-by-Step Derivation
Backpropagation calculates gradients by starting at the output layer and moving backward through the network one layer at a time.
Let us derive the equations for a network that uses:
A Sigmoid activation function in the output layer
Binary Cross-Entropy as the loss function
Step 1: Output-Layer Gradient
For binary classification, the Binary Cross-Entropy loss is:
E=βylog(y^β)β(1βy)log(1βy^β)
Let the output activation be:
a1(2)β=y^β
We first calculate the derivative of the loss with respect to the output activation: