Deep Neural Networks & Backprop Cache
Week 4 generalizes neural network theory to Deep $L$-Layer Neural Networks. We formalize dimension verification rules, explore why deep hierarchical representations work, implement the standard forward-backward cache flow, derive generic multi-layer backpropagation, and categorize parameters versus hyperparameters.
Learning Objectives
By the end of this module, you should be able to:
- Generalize neural networks to arbitrary depth $L$.
- Verify matrix and vector dimensions across all forward and backward passes.
- Explain why deep representations learn hierarchical abstractions exponentially more efficiently than shallow ones.
- Implement the forward and backward propagation cache architecture.
- Differentiate between model parameters and tunable hyperparameters.
Deep $L$-Layer Neural Networks
While logistic regression is a $1$-layer network and shallow networks contain $2$ layers, deep networks possess multiple hidden layers, empowering them to approximate complex, non-linear manifolds.
text
Mathematical Notation
Let $L$ denote the total number of layers (excluding input layer $0$):
| Notation | Dimension / Type | Description |
|---|---|---|
| $L$ | Integer scalar | Total number of computational layers |
| $n^{[l]}$ | Integer scalar | Number of neurons in layer $l$ |
| $n^{[0]} = n_x$ | Integer scalar | Input feature count |
| $\mathbf{W}^{[l]}$ | $(n^{[l]}, n^{[l-1]})$ | Weight matrix connecting layer $l-1$ to layer $l$ |
| $\mathbf{b}^{[l]}$ | $(n^{[l]}, 1)$ | Bias vector for layer $l$ |
| $\mathbf{Z}^{[l]}$ | $(n^{[l]}, m)$ | Affine transformation matrix: $\mathbf{Z}^{[l]} = \mathbf{W}^{[l]} \mathbf{A}^{[l-1]} + \mathbf{b}^{[l]}$ |
| $g^{[l]}(\cdot)$ | Non-linear mapping | Activation function of layer $l$ (e.g., ReLU for $l < L$, Sigmoid for $l = L$) |
| $\mathbf{A}^{[l]}$ | $(n^{[l]}, m)$ | Activation tensor: $\mathbf{A}^{[l]} = g^{[l]}(\mathbf{Z}^{[l]})$, with $\mathbf{A}^{[0]} \equiv \mathbf{X}$ |
Dimension Sanity Checks
Matrix dimension mismatches represent the most common bug in deep learning implementations. Committing these dimensional rules to memory ensures bug-free implementations:
| Tensor | Vectorized Dimension (Batch of $m$ Examples) | Single Example Dimension ($m = 1$) |
|---|---|---|
| $\mathbf{W}^{[l]}$ | $(n^{[l]}, n^{[l-1]})$ | $(n^{[l]}, n^{[l-1]})$ |
| $\mathbf{b}^{[l]}$ | $(n^{[l]}, 1)$ | $(n^{[l]}, 1)$ |
| $\mathbf{Z}^{[l]}$ | $(n^{[l]}, m)$ | $(n^{[l]}, 1)$ |
| $\mathbf{A}^{[l]}$ | $(n^{[l]}, m)$ | $(n^{[l]}, 1)$ |
| $\mathrm{d}\mathbf{W}^{[l]}$ | $(n^{[l]}, n^{[l-1]})$ | $(n^{[l]}, n^{[l-1]})$ |
| $\mathrm{d}\mathbf{b}^{[l]}$ | $(n^{[l]}, 1)$ | $(n^{[l]}, 1)$ |
| $\mathrm{d}\mathbf{Z}^{[l]}$ | $(n^{[l]}, m)$ | $(n^{[l]}, 1)$ |
| $\mathrm{d}\mathbf{A}^{[l]}$ | $(n^{[l]}, m)$ | $(n^{[l]}, 1)$ |
Crucial Rule: The gradient of any tensor ($\mathrm{d}\mathbf{W}, \mathrm{d}\mathbf{b}, \mathrm{d}\mathbf{Z}$) must have the exact same shape as the tensor itself ($\mathbf{W}, \mathbf{b}, \mathbf{Z}$).
Why Deep Hierarchical Representations?
Why prefer a deep network with many thin layers over a wide shallow network with one massive layer?
text
1. Compositional Feature Abstraction
- Early Layers ($l=1, 2$): Detect low-level primitive features such as edge orientations, color contrasts, and corner angles.
- Intermediate Layers: Compose lines and edges into textures and geometric shapes (e.g., contours of eyes, wheels, or phonemes).
- Deep Layers: Combine intermediate features into complex holistic semantic representations (e.g., human faces, vehicle models, or complete sentences).
2. Circuit Theory & Combinatorial Efficiency
According to Boolean circuit complexity theory, computing certain parity or arithmetic functions with a shallow ($2$-layer) circuit requires an exponentially large number of hidden units ($O(2^n)$). By contrast, structuring the computation hierarchically across a deep circuit computes the exact same function using only $O(\log n)$ or $O(n)$ units.
Building Blocks of Deep Networks: The Cache Mechanism
A deep neural network consists of modular computational blocks. During forward propagation, intermediate linear terms ($\mathbf{Z}^{[l]}$) and activations ($\mathbf{A}^{[l-1]}$) are saved in a memory Cache so they can be immediately recalled during backpropagation.

Forward Propagation Loop
For layer $l = 1, \dots, L$:
- $\mathbf{Z}^{[l]} = \mathbf{W}^{[l]} \mathbf{A}^{[l-1]} + \mathbf{b}^{[l]}$
- $\mathbf{A}^{[l]} = g^{[l]}(\mathbf{Z}^{[l]})$
- Store $\text{cache}^{[l]} = (\mathbf{A}^{[l-1]}, \mathbf{W}^{[l]}, \mathbf{b}^{[l]}, \mathbf{Z}^{[l]})$

Generalized Backpropagation Equations for Layer $l$
Starting at the output layer $L$:
$$\mathrm{d}\mathbf{A}^{[L]} = -\frac{\mathbf{Y}}{\mathbf{A}^{[L]}} + \frac{1 - \mathbf{Y}}{1 - \mathbf{A}^{[L]}}$$
For binary classification with a Sigmoid output unit, $\mathrm{d}\mathbf{Z}^{[L]}$ simplifies cleanly:
$$\mathrm{d}\mathbf{Z}^{[L]} = \mathbf{A}^{[L]} - \mathbf{Y}$$
Then, flowing backwards for $l = L, L-1, \dots, 1$:
$$\mathrm{d}\mathbf{W}^{[l]} = \frac{1}{m} \mathrm{d}\mathbf{Z}^{[l]} (\mathbf{A}^{[l-1]})^T$$
$$\mathrm{d}\mathbf{b}^{[l]} = \frac{1}{m} \sum_{i=1}^m \mathrm{d}\mathbf{Z}^{l} = \frac{1}{m} \text{np.sum}(\mathrm{d}\mathbf{Z}^{[l]}, \text{axis}=1, \text{keepdims}=\text{True})$$
$$\mathrm{d}\mathbf{A}^{[l-1]} = (\mathbf{W}^{[l]})^T \mathrm{d}\mathbf{Z}^{[l]}$$
$$\mathrm{d}\mathbf{Z}^{[l-1]} = \mathrm{d}\mathbf{A}^{[l-1]} * g'^{[l-1]}(\mathbf{Z}^{[l-1]})$$
Complete Training Workflow
The end-to-end implementation of an $L$-layer neural network follows these sequential steps:
python
Parameters vs. Hyperparameters
A core distinction in deep learning engineering lies between internal learned parameters and external architectural hyperparameters:
text
Hyperparameters dictate how the underlying model parameters are shaped and optimized. Systematic experimentation (e.g., grid search, random search, and cross-validation) is essential for finding optimal hyperparameter configurations.
What Does This Have to Do with the Biological Brain?
While the terminology of neural networks was originally inspired by neurobiology (dendrites as inputs, cell bodies as affine combinations, axons as activation thresholds), modern deep learning is fundamentally applied multivariable calculus and numerical linear algebra.
As Andrew Ng summarized:
"I do think that computer vision has taken a bit more inspiration from the human brain than other disciplines that also apply deep learning, but I personally use the analogy to the human brain less than I used to. Today, deep learning is best understood as computational graphs and function approximation."
Conclusion & Next Steps
You have covered the mathematical, architectural, and vectorized foundations of deep learning:
- Week 1: The scale laws and drivers powering deep learning.
- Week 2: Logistic regression, computation graphs, and vectorization mechanics.
- Week 3: Shallow networks, activation non-linearities, and symmetry breaking.
- Week 4: Deep $L$-layer architectures, forward/backward cache flows, and hyperparameter management.
From here, proceed to Improving Deep Neural Networks (Hyperparameter Tuning, Regularization, and Optimization) and convolutional/sequential architectures.