Skip to main content
Articles/Python/calculus-of-backpropagation-matrix-derivatives
Scenario Article

Calculus of Backpropagation: Mathematical Derivation from Scratch with Matrix Derivatives

Concept: The Mathematics of Neural Network Training

Abstract Algorithms•Jun 18, 2026
💡 Executive Summary

Backpropagation is the engine behind deep learning.

🎯 System Scenario & Requirements

System Goal

Backpropagation is the engine behind deep learning.

Actors & Components

  • Edge Traffic Ingress
  • Stateless Microservices
  • Datastore Infrastructure
  • API Gateway
  • Domain Service
  • Redis Cache
  • Transactional DB

Functional Requirements

  • TLDR: Backpropagation is the engine behind deep learning.
  • It uses the chain rule of calculus to calculate the gradient of a loss function with respect to the network's weights.
  • This guide derives the backpropagation equations using matrix calculus, analyzes vanishing gradients, and provides a runnable Python implementation using only raw NumPy.

Constraints & Assumptions

  • Exploding Gradients: If the weight values are initialized too large, the matrix multiplication $((W^{[l+1]})^T \delta^{[l+1]})$ can cause gradients to grow exponentially, leading to numerical overflow ( NaN values).
  • Mitigation: Use Xavier (Glorot) or He initialization to scale initial weights based on layer sizes.

⚡ Execution Stages & Decision Trace

1

Stage 1 — Mechanics: Forward and Backward Propagation Passes

Action / Decision:

A neural network alternates between two passes: 1.

Component Behavior:

Forward Pass: Input data propagates forward through the layers.

Observable Output: For each layer $l$, we calculate the weighted input $z^{[l]}$ and the activation $a^{[l]}$: $$ z^{[l]} = W^{[l]} a^{[l-1]} + b^{[l]} $$ $$ a^{[l]} = \sigma(z^{[l]}) $$ where $\sigma$ is an activation function (like Sigm…
Read Full Stage in Deep Dive ↓
📖

Deep Dive

Detailed Architectural Analysis

Scenario context

Backpropagation is the engine behind deep learning. It uses the chain rule of calculus to calculate the gradient of a loss function with respect to the network's weights. This guide derives the backpropagation equations using matrix calculus, analyzes vanishing gradients, and provides a runnable Python implementation using only raw NumPy.


Supervised machine learning relies on optimization. A neural network is…

This walkthrough focuses on Concept: The Mathematics of Neural Network Training.

Before making a design choice, clarify:

  • Supervised machine learning relies on optimization.
  • A neural network is a mathematical function $f(x; W)$ that maps an input vector $x$ to a predicted output vector $\hat{y}$ using a set of weights and biases, collectively denoted as $W$.
  • Training the network means finding the parameters $W$ that minimize a loss function $L(y, \hat{y})$, which measures the error between our predictions and the ground-truth targets $y$.

Concepts in play: deep-learning-foundations, caching, api-gateway, api-security-oauth2

Stage 1 — Mechanics: Forward and Backward Propagation Passes

  • A neural network alternates between two passes: 1.
  • Forward Pass: Input data propagates forward through the layers.
  • For each layer $l$, we calculate the weighted input $z^{[l]}$ and the activation $a^{[l]}$: $$ z^{[l]} = W^{[l]} a^{[l-1]} + b^{[l]} $$ $$ a^{[l]} = \sigma(z^{[l]}) $$ where $\sigma$ is an activation function (like Sigm…
  • Backward Pass: We compute the gradient of the loss with respect to the activations, weighted inputs, and parameters of each layer.

Observable outcome: Backward Pass: We compute the gradient of the loss with respect to the activations, weighted inputs, and parameters of each layer.

Stage 2 — Flow: High-Level Gradient Propagation Sequence

  • The flowchart below visualizes the transition between forward and backward propagation through a two-layer neural network: The table below traces the variables calculated at each step of the forward and backward passes:
flowchart LR
    X[Input x] -->|Forward Pass: W1, b1| Z1[Z1 = W1*x + b1]
    Z1 -->|Activation: Sigmoid| A1[A1 = sig Z1]
    A1 -->|Forward Pass: W2, b2| Z2[Z2 = W2*A1 + b2]
    Z2 -->|Activation: Sigmoid| A2[Output y_hat]
    A2 -->|Compute Loss| Loss[Loss L]

    Loss -->|Backward Pass: dLoss/dZ2| Delta2[Delta 2]
    Delta2 -->|dLoss/dW2| GradW2[dLoss/dW2]
    Delta2 -->|dLoss/dA1| Delta1[Delta 1]
    Delta1 -->|dLoss/dW1| GradW1[dLoss/dW1]

Observable outcome: The flowchart below visualizes the transition between forward and backward propagation through a two-layer neural network: The table below traces the variables calculated at each step of the forward and backward passes:

Stage 3 — Deep Dive: Structural Evolution & Performance Profiling

  • Let us examine the mathematical foundations and performance characteristics of matrix backpropagation.
  • Neural Network Layer Internals A neural network layer consists of a weight matrix $W$ of size $n^{[l]} \times n^{[l-1]}$, where $n^{[l]}$ is the number of neurons in the current layer and $n^{[l-1]}$ is the number of ne…
  • The bias is a vector $b$ of size $n^{[l]} \times 1$.
  • Storing these parameters in contiguous memory allocations using NumPy arrays allows the CPU to perform vector matrix multiplications in parallel using BLAS (Basic Linear Algebra Subprograms) libraries.

Observable outcome: Storing these parameters in contiguous memory allocations using NumPy arrays allows the CPU to perform vector matrix multiplications in parallel using BLAS (Basic Linear Algebra Subprograms) libraries.

Stage 4 — Advanced Concepts: Vanishing Gradients and Activation Functions

  • A classic problem when training deep networks using backpropagation is the Vanishing Gradient Problem .
  • Consider the derivative of the Sigmoid activation function: $$ \sigma'(z) = \sigma(z)(1 - \sigma(z)) $$ The maximum value of the Sigmoid derivative is $0.25$.
  • As gradients are backpropagated through many layers, the term $\delta^{[l]} = \left( (W^{[l+1]})^T \delta^{[l+1]} \right) \odot \sigma'(z^{[l]})$ is multiplied by these small derivatives repeatedly.
  • For a 5-layer network, the gradient of the first layer is scaled by at least $(0.25)^5 \approx 0.00097$.

Observable outcome: For a 5-layer network, the gradient of the first layer is scaled by at least $(0.25)^5 \approx 0.00097$.

Stage 5 — Applications: From Perceptrons to Transformers

  • Transformer Training: Large Language Models (LLMs) use backpropagation to learn attention weight projections during training.
  • Convolutional Networks (CNNs): Image recognition networks use backpropagation to update spatial filter matrices.
  • Reinforcement Learning: Policy networks use backpropagation scaled by environment rewards to adjust agent actions.

Observable outcome: Reinforcement Learning: Policy networks use backpropagation scaled by environment rewards to adjust agent actions.

Stage 6 — Practical Implementation: NumPy MLP from Scratch

  • Here is a complete, runnable Python implementation of a 2-layer Neural Network with backpropagation from scratch.
import numpy as np

class NumPyNeuralNetwork:
    def __init__(self, input_dim, hidden_dim, output_dim):
        # Seed for reproducibility
        np.random.seed(42)

        # He initialization for hidden layer, Xavier for output layer
        self.W1 = np.random.randn(hidden_dim, input_dim) * np.sqrt(2.0 / input_dim)
        self.b1 = np.zeros((hidden_dim, 1))

        self.W2 = np.random.randn(output_dim, hidden_dim) * np.sqrt(1.0 / hidden_dim)
        self.b2 = np.zeros((output_dim, 1))

    def _sigmoid(self, z):
        return 1.0 / (1.0 + np.exp(-np.clip(z, -500, 500)))

    def _sigmoid_derivative(self, z):
        s = self._sigmoid(z)
        return s * (1.0 - s)

    def forward(self, X):
        # X shape: (input_dim, batch_size)
        self.A0 = X
        self.Z1 = np.dot(self.W1, self.A0) + self.b1
        self.A1 = self._sigmoid(self.Z1)

        self.Z2 = np.dot(self.W2, self.A1) + self.b2
        self.A2 = self._sigmoid(self.Z2)
        return self.A2

    def backward(self, Y):
        # Y shape: (output_dim, batch_size)
        m = Y.shape[1]

        # Output layer error
        self.dZ2 = (self.A2 - Y) * self._sigmoid_derivative(self.Z2)
        self.dW2 = (1.0 / m) * np.dot(self.dZ2, self.A1.T)
        self.db2 = (1.0 / m) * np.sum(self.dZ2, axis=1, keepdims=True)

        # Hidden layer error (propagated back)
        self.dZ1 = np.dot(self.W2.T, self.dZ2) * self._sigmoid_derivative(self.Z1)
        self.dW1 = (1.0 / m) * np.dot(self.dZ1, self.A0.T)
        self.db1 = (1.0 / m) * np.sum(self.dZ1, axis=1, keepdims=True)

    def update_parameters(self, learning_rate):
        self.W1 -= learning_rate * self.dW1
        self.b1 -= learning_rate * self.db1
        self.W2 -= learning_rate * self.dW2
        self.b2 -= learning_rate * self.db2

    def train(self, X, Y, epochs, learning_rate):
        for epoch in range(epochs):
            predictions = self.forward(X)
            self.backward(Y)
            self.update_parameters(learning_rate)

            if epoch % 100 == 0:
                loss = np.mean(0.5 * (predictions - Y) ** 2)
                print(f"Epoch {epoch:04d} - Loss: {loss:.6f}")

# Example worked dataset: Binary XOR Logic Gate
if __name__ == "__main__":
    # 4 training examples, 2 input features
    X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]).T
    # XOR outputs
    Y = np.array([[0], [1], [1], [0]]).T

    # Network configuration: 2 inputs, 4 hidden units, 1 output
    nn = NumPyNeuralNetwork(input_dim=2, hidden_dim=4, output_dim=1)

    print("Training NumPy MLP on XOR gate dataset:")
    nn.train(X, Y, epochs=1000, learning_rate=0.5)

    # Final predictions
    final_output = nn.forward(X)
    print("\nFinal XOR Predictions:")
    for i in range(X.shape[1]):
        print(f"Input: {X[:, i]} -> Target: {Y[0, i]} -> Predicted: {final_output[0, i]:.4f}")

Observable outcome: Here is a complete, runnable Python implementation of a 2-layer Neural Network with backpropagation from scratch.

End-to-end outcome

After working through the stages, you should be able to explain:

  • Incorrect Matrix Transpositions: When multiplying matrix derivatives, always verify the shape dimensions.
  • If you get a ValueError: shapes not aligned , double-check your transpositions (e.g.
  • Forward Pass: Compute linear combinations and activations from input to output.
  • Backward Pass: Propagate error gradients backward using the chain rule.

Alternatives considered

Decision Guide: Custom Autograd vs. Frameworks

Decision Guide: Custom Autograd vs. Frameworks

Failure and degraded modes

Trade-offs and Failure Modes

  • Detection: Exploding Gradients: If the weight values are initialized too large, the matrix multiplication $((W^{[l+1]})^T \delta^{[l+1]})$ can cause gradients to grow exponentially, leading to numerical overflo…
  • User or operator impact: Exploding Gradients: If the weight values are initialized too large, the matrix multiplication $((W^{[l+1]})^T \delta^{[l+1]})$ can cause gradients to grow exponentially, leading to numerical overflo…

Share this case study

Discuss architecture trade-offs and lessons with your team.

Share:XLinkedInReddit