Deep Learning1 code example
⚡ +100 XP

Backpropagation

1

What is Backpropagation?

Backpropagation is the algorithm that computes gradients of the loss with respect to every weight in a neural network using the chain rule of calculus. It enables gradient descent to update all weights efficiently in a single backward pass.

2

Forward Pass

Input flows through the network layer by layer: x → z₁ = W₁x + b₁ → a₁ = relu(z₁) → z₂ = W₂a₁ + b₂ → ŷ = sigmoid(z₂) → L = loss(ŷ, y) All intermediate values (z₁, a₁, z₂) are cached — needed for the backward pass.

3

Backward Pass — Chain Rule

Starting from the loss, propagate gradients backward: ∂L/∂W₂ = ∂L/∂ŷ · ∂ŷ/∂z₂ · ∂z₂/∂W₂ ∂L/∂a₁ = ∂L/∂ŷ · ∂ŷ/∂z₂ · ∂z₂/∂a₁ ∂L/∂W₁ = ∂L/∂a₁ · ∂a₁/∂z₁ · ∂z₁/∂W₁ Each weight gets its exact gradient. Then update: W ← W − α·∂L/∂W

4

Manual Backprop (2-layer network)

💡

PyTorch's autograd does this automatically — but understanding it from scratch is essential for debugging, designing new architectures, and interviews.

Finished reading? Mark it complete to earn your XP.