- categories: Data Science, Algorithm
Definition:
Backpropagation is an algorithm used to train neural networks by computing the gradient of the loss function with respect to the network’s weights and biases. It applies the Chain Rule of calculus to efficiently propagate errors from the output layer back to the earlier layers, enabling gradient-based optimization methods like Stochastic Gradient Descent (SGD).
Steps of Backpropagation
-
Forward Pass:
Compute the predictions of the network for given inputs and calculate the loss. -
Backward Pass (Gradient Computation):
- Compute the gradient of the loss with respect to the outputs of each layer (starting from the last layer).
- Use the chain rule to propagate gradients back through the network to compute the gradients of the loss with respect to the weights and biases of each layer.
-
Update Parameters:
Use the computed gradients to update the weights and biases, typically using gradient descent:
where is the learning rate.
Mathematical Formulation
Consider a feedforward network with:
- layers,
- Activation functions for layer ,
- Weight matrices , bias vectors , and activations .
Forward Pass Equations:
For each layer :
- Compute the pre-activation (weighted sum):
- Compute the activation:
At the output layer:
- Predicted output .
- Loss measures the discrepancy between predictions and true labels .
Backward Pass (Gradient Computation)
-
Output Layer (Last Layer ):
Compute the gradient of the loss with respect to the pre-activation :
where denotes elementwise multiplication, and is the derivative of the activation function. -
Hidden Layers ():
Propagate the error backward through the network using:
-
Gradients of Weights and Biases:
Compute the gradients for updating parameters:- Weight gradient:
- Bias gradient:
- Weight gradient:
Algorithm Outline
-
Initialization: Randomly initialize and .
-
Forward Pass:
- Compute activations and predictions for input .
- Compute the loss .
-
Backward Pass:
- Compute for the output layer.
- Propagate errors backward to compute for all hidden layers.
- Compute gradients and .
-
Parameter Update:
Update and using an optimization algorithm (e.g., gradient descent). -
Iterate: Repeat until convergence.
Key Concepts
-
Chain Rule:
Backpropagation relies on the chain rule to efficiently compute gradients layer by layer:
-
Local Gradients:
The computations involve local gradients for each layer, reducing the complexity compared to computing the full gradient in one step. -
Computational Graph:
Neural networks can be viewed as computational graphs, where backpropagation systematically computes gradients by traversing the graph in reverse order.
Advantages
- Efficiency: Exploits the chain rule to compute gradients in time, where is the number of parameters.
- Scalability: Works for deep networks with many layers.
- General Applicability: Compatible with various loss functions and activation functions.
Limitations
- Vanishing/Exploding Gradients: Gradients can become too small or too large, especially in deep networks with sigmoidal activations.
- Computational Cost: For very large networks, backpropagation can be computationally expensive.
- Initialization Sensitivity: Poor weight initialization can slow convergence or prevent effective learning.