Detailed Derivation of The Backpropagation Algorithm

Faris AlasmaryarXiv 2026 · arXiv

TL;DR

This paper provides a rigorous, step-by-step "white-box" derivation of the backpropagation algorithm, moving from simple scalar calculus to vectorized matrix equations. By annotating every term with its dimensions and providing a direct mapping to a NumPy implementation, it bridges the gap between theoretical multivariable calculus and practical deep learning code.

What Problem Does This Paper Address?

While backpropagation is the engine behind nearly all modern AI, it is frequently taught as a "black box" or presented through compact matrix identities that skip crucial intermediate steps. For many students and engineers, the jump from the basic chain rule (differentiating a single weight) to the vectorized equations used in frameworks like PyTorch or TensorFlow is a significant hurdle.

The author tackles this lack of transparency by meticulously deriving the gradients for a fully connected feedforward network. The goal is to ensure the reader understands not just that backpropagation works, but exactly how the error signals flow backward through the network's architecture.

Methods

The paper follows a pedagogical path from the specific to the general:

  1. Scalar Derivation: It starts by isolating a single weight on a specific path and applying the chain rule to find its partial derivative relative to the error.
  2. Path Summation: It explains how a single weight in an early layer affects multiple nodes in the subsequent layer, requiring a summation of all possible "error paths."
  3. Vectorization: These summations are then transformed into elegant matrix operations. The author makes heavy use of the Hadamard product ($\odot$) for element-wise activation derivatives and the outer product for weight gradients.
  4. Dimensional Bookkeeping: A standout feature of this derivation is that every single equation is annotated with matrix dimensions (e.g., $n_l \times n_{l-1}$). This allows the reader to verify the consistency of the linear algebra at every step.
The running example network used in the paper, highlighting the bold path from the first node of every layer to the output error.

Datasets

To demonstrate the derivation in practice, the author implements the resulting equations in NumPy and trains the model on the AHDBase dataset.

  • AHDBase: A dataset of handwritten Arabic digits.
  • Scale: 60,000 training images and 10,000 test images.
  • Format: Each image is $8 \times 8$ pixels (64 features), with labels provided as one-hot encoded vectors for the 10 possible digits.

Key Insights and Ideas

  • The "Error" Term ($\delta$): The derivation centers on defining a layer-specific error term, $\delta^{[l]} = \partial E / \partial Z^{[l]}$. This abstraction is the "aha moment" that allows the backpropagation equations to become recursive, where the error of one layer is computed directly from the error of the next.
  • Why No Closed-Form? The paper explicitly explains why we can't just solve for the best weights using a single equation (like in Linear Regression). Because weights are nested inside non-linear sigmoid functions, the gradient becomes a system of non-linear equations with no algebraic solution, necessitating iterative gradient descent.
  • Transposed Weights: The derivation provides a clear geometric intuition for why the transpose of the weight matrix ($W^T$) appears in the backward pass: it effectively carries the error signals back through the same connections used during forward propagation.
  • Broadcasting Logic: When moving from a single example to a batch of $m$ examples, the author explains the mathematical necessity of "broadcasting" the bias vector—a concept often taken for granted in coding but rarely derived in formal math.
Figure 1: the running example network. The bold path connects the 1st node of every layer to the error E1 of the 1st output node.

Key Results

The paper concludes with the "Four General Backpropagation Equations" that form the heart of the algorithm:

  1. Output Error: $\delta^{[L]} = -(y - \hat{y}) \odot \sigma'(Z^{[L]})$
  2. Hidden Layer Error: $\delta^{[l]} = (W^{[l+1]T} \cdot \delta^{[l+1]}) \odot \sigma'(Z^{[l]})$
  3. Weight Gradient: $\partial E / \partial W^{[l]} = \delta^{[l]} \cdot A^{[l-1]T}$
  4. Bias Gradient: $\partial E / \partial b^{[l]} = \delta^{[l]}$

The author successfully maps these to a 40-line NumPy implementation. While the paper focuses on the derivation rather than benchmark chasing, it demonstrates that a network with two hidden layers (32 and 16 nodes) can effectively learn to classify handwritten digits using only these derived first principles.

Limitations and Future Work

  • Activation Functions: The derivation is specific to the Sigmoid activation function. While the logic holds for others, modern standards like ReLU or Leaky ReLU would require slightly different derivative terms.
  • Loss Functions: The paper uses Squared Error Loss. In modern classification tasks, Cross-Entropy loss paired with a Softmax output layer is more common and would involve a different initial $\delta^{[L]}$ calculation.
  • Architecture: The focus is strictly on fully connected feedforward networks. Extending this level of dimensional rigor to Convolutional Neural Networks (CNNs) or Recurrent Neural Networks (RNNs) is suggested as a natural next step for interested readers.

Summary updated

Abstract

Backpropagation is the standard method for computing the gradients that train neural networks, but it is usually presented in a compact vector form that skips the intermediate steps. This tutorial derives backpropagation step by step for a fully connected feedforward network with sigmoid activations and a squared-error loss. Using a running example with three layers, we first differentiate the error with respect to a single weight by following its path through the network with the chain rule. We then show how summing over all paths leads to the error terms $δ$ and to the four general backpropagation equations in matrix form. Every term is annotated with its dimensions, so each equation can be checked for consistency. The derivation is then extended to $m$ training examples, where the weight gradients become a single matrix product and the bias term corresponds to broadcasting. It is also extended to mini-batch gradient descent, including why the gradient of a mini-batch estimates the full gradient. Batch and mini-batch training are both given as algorithms. Finally, each equation is linked to the corresponding line of a NumPy implementation trained on the AHDBase dataset of handwritten Arabic digits, and the complete code is included.
BibTeX
@misc{alasmary2026detailed,
  author = {Faris Alasmary},
  title = {{Detailed Derivation of The Backpropagation Algorithm}},
  year = {2026},
  eprint = {2611.02152},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.02152},
}
Figure