Backpropagation is the algorithm used to train a deep neural network by computing the gradient of the loss function with respect to the weights of the network. Here's a high-level mathematical description of how backpropagation is implemented:
- Forward pass: First, the input x is passed through the network to obtain the prediction y. This involves computing the output of each layer using the weight matrix w and a non-linear activation function, such as ReLU or sigmoid. The forward pass can be written mathematically as:
a(l)=σ(w(l)a(l−1)+b(l)), where a(l) is the output of the l-th layer, w(l) is the weight matrix for the l-th layer, b(l) is the bias vector for the l-th layer, and σ is the activation function.
- Compute the loss: Once the prediction y is obtained, the loss function L is computed, which measures the difference between the prediction and the expected target t. A common choice for the loss function is the mean squared error (MSE) given by:
L=21∣∣y−t∣∣2
- Backward pass: The backward pass involves computing the gradient of the loss function with respect to each weight matrix in the network. This is done using the chain rule of calculus to propagate the errors backwards through the network. The gradient of the loss with respect to the weight matrix w(l) for the l-th layer can be written as:
∂w(, where
The first term ∂a(l)∂L is the error term, which measures how much the output of the l-th layer contributes to the overall error. It can be computed recursively as:
∂a(l)∂, where .
The second term ∂z(l)∂a(l) is the derivative of the activation function at the input .
The third term ∂w(l)∂z(l) is the input to the -th layer.
- Update the weights: Once the gradients are computed, the weights are updated using a gradient descent algorithm, such as stochastic gradient descent (SGD) or Adam. The weights are updated as follows:
w(l):=w(l)−α, where is the learning rate.
This process is repeated for multiple iterations until the loss function converges to a minimum.