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:
- 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$:
- 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:
- 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:
- Link Consecutive Layers: We express the error at layer $l$ in terms of the error at layer $l+1$ using the multivariate chain rule:
- 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}$:
- Transpose and Simplify: Substitute back and use the property $(AB)^T = B^T A^T$:
- 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:
- Chain Rule via Intermediate $z$:
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}$.
Conclusion:
BP4: Gradient for Weights
Goal: $\frac{\partial C}{\partial \mathbf{W}^l} = \boldsymbol{\delta}^l (\mathbf{a}^{l-1})^T$
Derivation Steps:
- Element-wise Approach: Consider a single weight $w^l_{jk}$ connecting neuron $k$ in layer $l-1$ to neuron $j$ in layer $l$:
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$.
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}$: