Skip to content
Phuc's Blog
Go back

Backpropagation Calculus

Edit page
๐Ÿ“ŒSeries:Artificial Intelligent
Part 2 of 6
View all articles in this series

This post covers the mathematical foundation of Backpropagation Calculus, explaining step-by-step how gradients flow backwards through a neural network using the multivariable chain rule.

Backpropagation Calculus

Table of contents

Open Table of contents

What is Backpropagation Calculus?

Backpropagation Calculus Architecture

Backpropagation is the foundational algorithm that computes the gradient of the cost function โˆ‡C\nabla C. The entries of this gradient vector represent the partial derivatives of the cost function CC with respect to every weight (ww) and bias (bb) throughout the network:

โˆ‡C=[โˆ‚Cโˆ‚w(1)โˆ‚Cโˆ‚b(1)โ‹ฎโˆ‚Cโˆ‚w(L)โˆ‚Cโˆ‚b(L)]\nabla C = \begin{bmatrix} \frac{\partial C}{\partial w^{(1)}} \\[0.6em] \frac{\partial C}{\partial b^{(1)}} \\[0.4em] \vdots \\[0.4em] \frac{\partial C}{\partial w^{(L)}} \\[0.6em] \frac{\partial C}{\partial b^{(L)}} \end{bmatrix}

Calculating these partial derivatives quantifies how sensitive the networkโ€™s total error is to small perturbations in each parameter, enabling Gradient Descent to systematically update parameters in the direction of steepest descent: wโ†wโˆ’ฮฑโˆ‚Cโˆ‚ww \leftarrow w - \alpha \frac{\partial C}{\partial w} (where ฮฑ\alpha is the learning rate).


1. Simple Case: One Neuron per Layer

To understand the calculus intuitively, consider a simplified network where each layer contains a single neuron.

Simple Neural Network with 1 Neuron per Layer Figure 1: A simplified neural network architecture with one neuron per layer.

Weights and Biases in Single Neuron Network Figure 2: Parameters (ww and bb) defining connections and activation thresholds between consecutive neurons.

For a single training sample with desired target output yy:

The Chain Rule Breakdown

Dependency Tree of Variables Figure 3: Computational dependency graph illustrating how w(L),b(L),a(Lโˆ’1)w^{(L)}, b^{(L)}, a^{(L-1)} determine z(L)z^{(L)}, which computes activation a(L)a^{(L)}, ultimately yielding cost C0C_0.

To determine how a small change in weight w(L)w^{(L)} impacts the cost C0C_0, we apply the Multivariable Chain Rule:

โˆ‚C0โˆ‚w(L)=โˆ‚z(L)โˆ‚w(L)โ‹…โˆ‚a(L)โˆ‚z(L)โ‹…โˆ‚C0โˆ‚a(L)\frac{\partial C_0}{\partial w^{(L)}} = \frac{\partial z^{(L)}}{\partial w^{(L)}} \cdot \frac{\partial a^{(L)}}{\partial z^{(L)}} \cdot \frac{\partial C_0}{\partial a^{(L)}}

Chain Rule Breakdown Ratios Figure 4: Decomposing the partial derivative โˆ‚C0โˆ‚w(L)\frac{\partial C_0}{\partial w^{(L)}} into three constituent derivative ratios.

Computing the Three Factors

  1. Previous Activation Sensitivity:

    โˆ‚z(L)โˆ‚w(L)=a(Lโˆ’1)\frac{\partial z^{(L)}}{\partial w^{(L)}} = a^{(L-1)}

    (Reflects Hebbian learning: higher activation a(Lโˆ’1)a^{(L-1)} amplifies the weight changeโ€™s effect on the weighted input).

  2. Activation Function Gradient:

    โˆ‚a(L)โˆ‚z(L)=ฯƒโ€ฒ(z(L))\frac{\partial a^{(L)}}{\partial z^{(L)}} = \sigma'(z^{(L)})

    (Measures the local derivative/slope of the non-linear activation function).

  3. Output Error Sensitivity:

    โˆ‚C0โˆ‚a(L)=2(a(L)โˆ’y)\frac{\partial C_0}{\partial a^{(L)}} = 2(a^{(L)} - y)

    (Proportional to the raw prediction error; larger errors induce steeper gradient updates).

Combining the Partial Derivatives

Multiplying these three factors yields the exact sensitivity for a single weight:

โˆ‚C0โˆ‚w(L)=a(Lโˆ’1)โ‹…ฯƒโ€ฒ(z(L))โ‹…2(a(L)โˆ’y)\frac{\partial C_0}{\partial w^{(L)}} = a^{(L-1)} \cdot \sigma'(z^{(L)}) \cdot 2(a^{(L)} - y)

Similarly, for the bias b(L)b^{(L)}, since โˆ‚z(L)โˆ‚b(L)=1\frac{\partial z^{(L)}}{\partial b^{(L)}} = 1:

โˆ‚C0โˆ‚b(L)=1โ‹…ฯƒโ€ฒ(z(L))โ‹…2(a(L)โˆ’y)\frac{\partial C_0}{\partial b^{(L)}} = 1 \cdot \sigma'(z^{(L)}) \cdot 2(a^{(L)} - y)

2. Propagating Backwards to Hidden Layers

To compute gradients for parameters in earlier hidden layers (e.g., w(Lโˆ’1)w^{(L-1)}), we first evaluate the costโ€™s sensitivity to the previous activation a(Lโˆ’1)a^{(L-1)}:

โˆ‚C0โˆ‚a(Lโˆ’1)=โˆ‚z(L)โˆ‚a(Lโˆ’1)โ‹…โˆ‚a(L)โˆ‚z(L)โ‹…โˆ‚C0โˆ‚a(L)=w(L)โ‹…ฯƒโ€ฒ(z(L))โ‹…2(a(L)โˆ’y)\frac{\partial C_0}{\partial a^{(L-1)}} = \frac{\partial z^{(L)}}{\partial a^{(L-1)}} \cdot \frac{\partial a^{(L)}}{\partial z^{(L)}} \cdot \frac{\partial C_0}{\partial a^{(L)}} = w^{(L)} \cdot \sigma'(z^{(L)}) \cdot 2(a^{(L)} - y)

By computing โˆ‚C0โˆ‚a(Lโˆ’1)\frac{\partial C_0}{\partial a^{(L-1)}}, we backward propagate the error signal layer-by-layer, efficiently evaluating derivatives for all preceding weights and biases.


3. Cost Over All Training Examples

The overall cost function CC across a full dataset of nn samples is the arithmetic mean:

C=1nโˆ‘k=0nโˆ’1CkC = \frac{1}{n} \sum_{k=0}^{n-1} C_k

By the linearity of differentiation, the derivative of total cost CC with respect to any parameter is the average of individual sample derivatives:

โˆ‚Cโˆ‚w(L)=1nโˆ‘k=0nโˆ’1โˆ‚Ckโˆ‚w(L)\frac{\partial C}{\partial w^{(L)}} = \frac{1}{n} \sum_{k=0}^{n-1} \frac{\partial C_k}{\partial w^{(L)}}

4. Generalizing to Multineuron Layers

In a general deep network with multiple neurons per layer:

Multineuron Layer Indexing Figure 5: Indexing activations ak(Lโˆ’1)a_k^{(L-1)} and aj(L)a_j^{(L)} alongside weight matrix entry wjk(L)w_{jk}^{(L)} across dense layers.

The weighted sum zj(L)z_j^{(L)} for neuron jj is:

zj(L)=โˆ‘kwjk(L)ak(Lโˆ’1)+bj(L)z_j^{(L)} = \sum_{k} w_{jk}^{(L)} a_k^{(L-1)} + b_j^{(L)}

Multiple Paths of Computational Influence

In fully connected layers, a single neuron kk in layer Lโˆ’1L-1 influences every neuron jj in layer LL. Consequently, changing ak(Lโˆ’1)a_k^{(L-1)} affects the cost through multiple computation paths. By multivariate calculus, we sum the chain rule across all output neurons in layer LL:

โˆ‚C0โˆ‚ak(Lโˆ’1)=โˆ‘j=0nLโˆ’1โˆ‚zj(L)โˆ‚ak(Lโˆ’1)โ‹…โˆ‚aj(L)โˆ‚zj(L)โ‹…โˆ‚C0โˆ‚aj(L)=โˆ‘j=0nLโˆ’1wjk(L)โ‹…ฯƒโ€ฒ(zj(L))โ‹…โˆ‚C0โˆ‚aj(L)\frac{\partial C_0}{\partial a_k^{(L-1)}} = \sum_{j=0}^{n_L - 1} \frac{\partial z_j^{(L)}}{\partial a_k^{(L-1)}} \cdot \frac{\partial a_j^{(L)}}{\partial z_j^{(L)}} \cdot \frac{\partial C_0}{\partial a_j^{(L)}} = \sum_{j=0}^{n_L - 1} w_{jk}^{(L)} \cdot \sigma'(z_j^{(L)}) \cdot \frac{\partial C_0}{\partial a_j^{(L)}}

Conclusion

Backpropagation Calculus Formula Summary Figure 6: Complete mathematical summary of backpropagation calculus formulas across layers.

Backpropagation boils down to three core principles:

  1. Chain Rule Engine: Applies the multivariable chain rule to calculate how sensitive the cost function is to every weight and bias.
  2. Backward Error Flow: Computes error signals starting from the output layer and propagates them backward through hidden layers.
  3. Vectorized Optimization: Aggregates partial derivatives into a gradient vector (โˆ‡C\nabla C), enabling modern frameworks to update parameters efficiently using Gradient Descent.

Edit page
Share this post:

Previous Post
Self-Attention Mechanism
Next Post
What is a Neural Network?

Comments