Understanding Deep Learning Backpropagation: A Step-by-Step Example
Backpropagation is a fundamental algorithm in deep learning, used to train neural networks by computing the gradient that is needed to update the network's weights. It's a workhorse that enables our models to learn from data and make accurate predictions. Let's dive into backpropagation with a simple example, using a single hidden layer neural network.
Setting the Stage: A Simple Neural Network
Consider a neural network with an input layer of 2 neurons, a hidden layer of 3 neurons, and an output layer of 1 neuron. The activation function is the sigmoid function, and we'll use mean squared error (MSE) as our loss function.
Weights and Biases Initialization
First, we initialize the weights (W1, W2) and biases (b1, b2) with small random values.

| Weights | Biases |
|---|---|
| W1 (2x3): [[0.2, 0.1, 0.3], [0.4, 0.5, 0.6]] | b1 (3x1): [0.1, 0.2, 0.3] |
| W2 (3x1): [[0.7], [0.8], [0.9]] | b2 (1x1): [0.1] |
Forward Propagation
Given an input X = [1, 2], we perform forward propagation to compute the output (Z2) and the activations (Z1, A1, Z2, A2).
1. Compute Z1: Z1 = W1 * X + b1
2. Apply activation function: A1 = sigmoid(Z1)

3. Compute Z2: Z2 = W2 * A1 + b2
4. Apply activation function: A2 = sigmoid(Z2)
activations and output
| Activations | Output |
|---|---|
| A1: [0.549, 0.574, 0.589] | A2: 0.570 |
Backpropagation: Computing Gradients
Now, let's compute the gradients of the weights and biases using backpropagation. We'll use the error term (δ) to compute the gradients.

Error Term (δ) Computation
1. Compute δ2: δ2 = (A2 - Y) * sigmoid_derivative(A2)
2. Compute δ1: δ1 = (W2^T * δ2) * sigmoid_derivative(A1)
Gradients Computation
1. Compute dW2: dW2 = δ2 * A1^T
2. Compute db2: db2 = δ2
3. Compute dW1: dW1 = δ1 * X^T
4. Compute db1: db1 = δ1
Gradients
| Gradients |
|---|
| dW1: [[-0.003, -0.004, -0.005], [-0.006, -0.008, -0.010]] |
| db1: [-0.003, -0.004, -0.005] |
| dW2: [[-0.002], [-0.004], [-0.006]] |
| db2: [-0.003] |
Updating Weights and Biases
Finally, we update the weights and biases using the gradients and a learning rate (η).
1. Update W1: W1 = W1 - η * dW1
2. Update b1: b1 = b1 - η * db1
3. Update W2: W2 = W2 - η * dW2
4. Update b2: b2 = b2 - η * db2
Repeat the forward propagation, backpropagation, and weight update steps for multiple epochs to train the neural network.
Backpropagation is a powerful algorithm that enables neural networks to learn from data. By understanding and implementing backpropagation, we can train deep learning models to perform various tasks, such as image classification, natural language processing, and more.






















