Skip to content

Backpropagation: A Vector Calculus Perspective

The primary goal of backpropagation is to calculate the partial derivatives of the cost function $C$ with respect to every weight $\mathbf{W}$ and bias $\mathbf{b}$ in the network.

1. Notation (Matrix Form)

To facilitate a clean derivation using matrix calculus, we adopt the following conventions:

  • $\mathbf{a}^l$: Activation vector of the $l$-th layer ($n_l \times 1$).
  • $\mathbf{z}^l$: Weighted input vector of the $l$-th layer ($n_l \times 1$), where $\mathbf{z}^l = \mathbf{W}^l \mathbf{a}^{l-1} + \mathbf{b}^l$.
  • $\mathbf{W}^l$: Weight matrix of the $l$-th layer ($nl \times n{l-1}$).
  • $\mathbf{b}^l$: Bias vector of the $l$-th layer ($n_l \times 1$).
  • $\boldsymbol{\delta}^l$: Error vector of the $l$-th layer, defined as $\boldsymbol{\delta}^l \equiv \frac{\partial C}{\partial \mathbf{z}^l}$.
  • $\odot$: Hadamard product (element-wise multiplication).
  • $\sigma’(\mathbf{z}^l)$: The derivative of the activation function applied element-wise to $\mathbf{z}^l$.

2. Derivation of the Equations

BP1: Error at the Output Layer

Goal: $\boldsymbol{\delta}^L = \nabla_{\mathbf{a}^L} C \odot \sigma’(\mathbf{z}^L)$

Derivation Steps:

  1. Apply the Chain Rule: The error $\boldsymbol{\delta}^L$ represents how the cost $C$ changes with respect to the weighted input $\mathbf{z}^L$. Since $C$ depends on $\mathbf{z}^L$ through the activations $\mathbf{a}^L$:
  1. Compute the Jacobian: Because $a^L_j = \sigma(z^L_j)$ (each output depends only on its own input), the Jacobian matrix $\frac{\partial \mathbf{a}^L}{\partial \mathbf{z}^L}$ is a diagonal matrix:
  1. Result: Multiplying a diagonal matrix by a vector is equivalent to the Hadamard product:

BP2: Propagating Error to Hidden Layers

Goal: $\boldsymbol{\delta}^l = ((\mathbf{W}^{l+1})^T \boldsymbol{\delta}^{l+1}) \odot \sigma’(\mathbf{z}^l)$

Derivation Steps:

  1. Link Consecutive Layers: We express the error at layer $l$ in terms of the error at layer $l+1$ using the multivariate chain rule:
  1. Differentiate the Linear Transformation: Recall $\mathbf{z}^{l+1} = \mathbf{W}^{l+1} \mathbf{a}^l + \mathbf{b}^{l+1}$ and $\mathbf{a}^l = \sigma(\mathbf{z}^l)$. Applying the chain rule to find $\frac{\partial \mathbf{z}^{l+1}}{\partial \mathbf{z}^l}$:
  1. Transpose and Simplify: Substitute back and use the property $(AB)^T = B^T A^T$:
  1. Final Form:
    Convert the diagonal matrix multiplication to a Hadamard product:

BP3: Gradient for Biases

Goal: $\frac{\partial C}{\partial \mathbf{b}^l} = \boldsymbol{\delta}^l$

Derivation Steps:

  1. Chain Rule via Intermediate $z$:
  1. Compute Local Gradient: From $\mathbf{z}^l = \mathbf{W}^l \mathbf{a}^{l-1} + \mathbf{b}^l$, we see that $\mathbf{b}^l$ is added directly to the weighted sum. Thus, $\frac{\partial \mathbf{z}^l}{\partial \mathbf{b}^l}$ is the Identity matrix $\mathbf{I}$.

  2. Conclusion:

BP4: Gradient for Weights

Goal: $\frac{\partial C}{\partial \mathbf{W}^l} = \boldsymbol{\delta}^l (\mathbf{a}^{l-1})^T$

Derivation Steps:

  1. Element-wise Approach: Consider a single weight $w^l_{jk}$ connecting neuron $k$ in layer $l-1$ to neuron $j$ in layer $l$:
  1. Solve for Partial Derivative: Since $z^lj = \sum_m w^l{jm} a^{l-1}m + b^l_j$, the derivative with respect to $w^l{jk}$ is simply $a^{l-1}k$. Therefore, $\frac{\partial C}{\partial w^l{jk}} = \delta^l_j a^{l-1}_k$.

  2. Vectorize as an Outer Product: The collection of all such derivatives $\delta^l_j a^{l-1}_k$ for all $j, k$ forms the Outer Product of the error vector $\boldsymbol{\delta}^l$ and the input activation vector $\mathbf{a}^{l-1}$:

About this Post

This post is written by Louis C Deng, licensed under CC BY-NC 4.0.

#CS231n #Math #Vector Calculus