"Mastering Deep Learning: Backpropagation Explained with Code Examples"

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.

Backpropagation, intuitively | Deep Learning Chapter 3
Backpropagation, intuitively | Deep Learning Chapter 3

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)

backpropagation algorithm
backpropagation algorithm

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.

the cover of an article with dots and arrows
the cover of an article with dots and arrows

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.

A Step by Step Backpropagation Example
A Step by Step Backpropagation Example
Backpropagation: how it works
Backpropagation: how it works
Backpropagation Tutorial | Manfred Zabarauskas' Blog
Backpropagation Tutorial | Manfred Zabarauskas' Blog
Learn Backpropagation Derivation Step by Step
Learn Backpropagation Derivation Step by Step
Backpropagation
Backpropagation
A Step by Step Backpropagation Example
A Step by Step Backpropagation Example
Backpropagation
Backpropagation
Backpropagation Neural Network - How it Works e.g. Counting
Backpropagation Neural Network - How it Works e.g. Counting
How backpropagation works, and how you can use Python to build a neural network
How backpropagation works, and how you can use Python to build a neural network
Back Propagation in Neural Network: Machine Learning Algorithm
Back Propagation in Neural Network: Machine Learning Algorithm
Back-Propagation is very simple. Who made it Complicated ?
Back-Propagation is very simple. Who made it Complicated ?
Client Challenge
Client Challenge
A Step by Step Backpropagation Example
A Step by Step Backpropagation Example
Implement Back Propagation in Neural Networks
Implement Back Propagation in Neural Networks
Deep physical neural networks trained with backpropagation - Nature
Deep physical neural networks trained with backpropagation - Nature
Convolutional Neural Networks backpropagation: from intuition to derivation
Convolutional Neural Networks backpropagation: from intuition to derivation
What is Bayesian Deep Reinforcement Learning
What is Bayesian Deep Reinforcement Learning
Test Run - Neural Network Back-Propagation for Programmers
Test Run - Neural Network Back-Propagation for Programmers
This AI Paper Reveals a New Approach to Understand Deep Learning Models: Unpacking the 'Where' and 'What' with Concept Relevance Propagation (CRP)
This AI Paper Reveals a New Approach to Understand Deep Learning Models: Unpacking the 'Where' and 'What' with Concept Relevance Propagation (CRP)
Deep Learning Fundamentals with Python
Deep Learning Fundamentals with Python
Deep Learning for Beginners Complete Guide (2026) | AI & Neural Networks Roadmap
Deep Learning for Beginners Complete Guide (2026) | AI & Neural Networks Roadmap
an image of a diagram with the words'backprogramation algorithm '
an image of a diagram with the words'backprogramation algorithm '
deep learning backpropagation example
deep learning backpropagation example