Deep Learning Foundations Series

Backpropagation 

step by step

Backpropagation is the algorithm that makes neural networks learn. In this guide, you'll understand exactly how errors flow backward through a network, how gradients are computed using the chain rule, and how this single idea powers everything from image recognition to large language models.

| By Affordable AI, Nagpur

input hidden output
→ forward pass (prediction) ← backward pass (gradient)
Introduction

The Algorithm Behind Every Trained Neural Network

Backpropagation (short for "backward propagation of errors") is the core algorithm that allows neural networks to learn from data. Every time a model like a convolutional network, a transformer, or a simple feedforward classifier improves its predictions during training, backpropagation is the mechanism working behind the scenes.

At its heart, backpropagation answers one question: "How much did each weight in the network contribute to the error?" Once we know that, we can nudge every weight in the direction that reduces the error — and repeat this process thousands or millions of times until the network becomes accurate.

Definition: Backpropagation is an algorithm that efficiently computes the gradient of a loss function with respect to every weight in a neural network, using the chain rule of calculus, by propagating the error backward from the output layer to the input layer.

This blog breaks the concept down into digestible parts: the intuition, the math, the step-by-step algorithm, working code, common pitfalls, and how it's used in real-world AI systems.

Why It Matters

Three Reasons Backpropagation Powers Modern AI

Without an efficient way to compute gradients, training deep networks with millions of parameters would be computationally impossible. Backpropagation solved this problem in a way that scales.

Training Efficiency

Backpropagation computes gradients for all parameters in a single backward pass, making it far faster than computing each derivative independently.

Universal Applicability

It works for any differentiable architecture — CNNs, RNNs, Transformers — regardless of depth or structure.

Foundation of Modern AI

From image generation to language models, nearly every deep learning breakthrough since the 1980s relies on backpropagation to train its parameters.

The Core Idea

The Chain Rule: Backpropagation's Building Block

A neural network is essentially a long chain of functions: input data passes through layers of weighted sums and activation functions to produce an output. To know how a change in an early-layer weight affects the final loss, we need to track how that change ripples through every subsequent function — this is exactly what the chain rule from calculus does.

Chain rule for a weight in an earlier layer
∂L/∂w = (∂L/∂ŷ) · (∂ŷ/∂z) · (∂z/∂w)

Here, L is the loss, ŷ is the network's output, and z is the weighted input to a neuron. By applying this rule repeatedly, layer by layer, backpropagation calculates the gradient for every weight — starting from the output layer and moving backward toward the input layer.

Two Passes, One Algorithm

Forward Pass vs. Backward Pass

Every training iteration consists of two complementary passes through the network.

Pass 01 · Left → Right

Forward Pass

  • Input data flows through each layer
  • Weighted sums and activations are computed
  • Produces a prediction (ŷ)
  • Loss is calculated by comparing ŷ to the true label

Pass 02 · Right → Left

Backward Pass

  • Loss gradient is computed at the output
  • Gradient flows backward through each layer
  • Chain rule computes ∂L/∂w for every weight
  • Weights are updated via gradient descent
Algorithm Walkthrough

Step-by-Step: How Backpropagation Works

Because this is a true sequential process, here's exactly what happens during one training iteration.

01

Initialize Weights

Weights and biases are initialized, typically with small random values, to break symmetry between neurons.

02

Forward Propagation

Input data passes through each layer, applying weighted sums followed by an activation function (e.g., ReLU, sigmoid), producing a final prediction.

03

Compute the Loss

A loss function (e.g., cross-entropy, mean squared error) measures the difference between the prediction and the true target value.

04

Backward Propagation

Starting at the output layer, the gradient of the loss is computed with respect to each weight, moving backward layer by layer using the chain rule.

05

Update Weights

Each weight is adjusted in the direction that reduces the loss, scaled by a learning rate, using an optimizer such as SGD or Adam.

06

Repeat

Steps 2–5 are repeated across many batches and epochs until the loss converges to an acceptably low value.

Code Walkthrough

A Minimal Backpropagation Implementation

Here is a simplified two-layer neural network trained with backpropagation using only NumPy — useful for understanding the mechanics before relying on frameworks like PyTorch or TensorFlow.

backprop_demo.py
import numpy as np

# Sigmoid activation and its derivative
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
    return x * (1 - x)

# Initialize weights
np.random.seed(1)
W1 = np.random.randn(2, 4)
W2 = np.random.randn(4, 1)

X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([[0],[1],[1],[0]])

lr = 0.5

for epoch in range(10000):
    # ---- Forward pass ----
    z1 = X.dot(W1)
    a1 = sigmoid(z1)
    z2 = a1.dot(W2)
    y_hat = sigmoid(z2)

    # ---- Backward pass ----
    error = y - y_hat
    d_output = error * sigmoid_derivative(y_hat)
    d_hidden = d_output.dot(W2.T) * sigmoid_derivative(a1)

    # ---- Update weights (gradient descent) ----
    W2 += a1.T.dot(d_output) * lr
    W1 += X.T.dot(d_hidden) * lr

print("Final predictions:")
print(y_hat)
Common Challenges

What Can Go Wrong

Backpropagation is powerful, but it comes with well-known failure modes that every practitioner should recognize.

Vanishing Gradients

In deep networks, gradients can shrink exponentially as they propagate backward, making early layers learn extremely slowly.

Exploding Gradients

The opposite problem — gradients grow uncontrollably large, causing unstable updates and diverging loss values.

Overfitting Sensitivity

Backpropagation optimizes purely for training loss, so without regularization the network can memorize noise instead of generalizing.

Computational Cost

Storing intermediate activations for the backward pass requires significant memory, especially for very deep or wide networks.

Optimization Techniques

Making Backpropagation More Effective

Momentum

Accumulates past gradients to smooth out updates and accelerate convergence, especially in narrow valleys of the loss surface.

Adam Optimizer

Combines momentum with adaptive learning rates per parameter, making it the default choice for most modern architectures.

Batch Normalization

Normalizes layer inputs during training, reducing internal covariate shift and helping gradients flow more consistently.

Learning Rate Scheduling

Gradually reduces the learning rate during training, allowing large early steps and fine, stable steps near convergence.

Real-World Applications

Where Backpropagation Is Used Today

Computer Vision

Convolutional neural networks use backpropagation to learn filters that detect edges, shapes, and objects in images.

Natural Language Processing

Transformer-based language models are trained through backpropagation across billions of parameters to predict and generate text.

Speech Recognition

Recurrent and transformer-based speech models learn acoustic patterns using gradients computed via backpropagation through time.

Recommendation Systems

Deep learning-based recommenders use backpropagation to learn user and item embeddings that predict preferences.