Blame
|
1 | # 7. Multilayer neural networks |
||||||
| 2 | ||||||||
| 3 | A single linear unit only draws a straight boundary. Stacking many simple units with a nonlinearity between them gives a multilayer neural network, which fits curved boundaries and learns its own features. This module is a compact tour of neural networks, from architecture to training, and the gateway to the [Deep Learning](/en/Deep%20Learning) course, which develops every topic here in depth. |
|||||||
| 4 | ||||||||
| 5 | **Objectives** |
|||||||
| 6 | - Contrast the linear and nonlinear approaches and see why hidden layers are needed. |
|||||||
| 7 | - Read a network as input, hidden, and output layers, and write its forward pass. |
|||||||
| 8 | - Choose the output layer and loss for binary and multiclass classification. |
|||||||
| 9 | - Pick an activation function and see why zero-centered outputs help. |
|||||||
| 10 | - Train by the chain rule and backpropagation, with mini-batches, good initialization, and dropout. |
|||||||
| 11 | - Guard the implementation with gradient checking and vectorization. |
|||||||
| 12 | ||||||||
| 13 | ## 7.1 Linear versus nonlinear |
|||||||
| 14 | ||||||||
| 15 | The linear classifiers of the [previous module](/en/Machine%20Learning/06%20Linear%20classification) separate classes with a single straight boundary, so a problem like XOR, which is not linearly separable, is out of reach. Composing units through a nonlinear activation $g$ bends the boundary. The nonlinearity is essential: without it, a stack of linear layers collapses back to a single linear map, |
|||||||
| 16 | ||||||||
| 17 | $$\boxed{ W^{[2]}\!\left(W^{[1]} x + b^{[1]}\right) + b^{[2]} = W' x + b' }$$ |
|||||||
| 18 | ||||||||
| 19 | so depth would add nothing. The nonlinear activation is what makes stacking worthwhile. |
|||||||
| 20 | ||||||||
| 21 | ## 7.2 Layers: input, hidden, output |
|||||||
| 22 | ||||||||
| 23 | A single neuron computes $a = g(w^T x + b)$. A layer stacks many neurons, and a network stacks layers. Layer $l$ transforms the previous activations into new ones: |
|||||||
| 24 | ||||||||
| 25 | $$\boxed{ z^{[l]} = W^{[l]} a^{[l-1]} + b^{[l]}, \quad a^{[l]} = g^{[l]}\!\left(z^{[l]}\right), \quad a^{[0]} = x, \quad \hat{y} = a^{[L]} }$$ |
|||||||
| 26 | ||||||||
| 27 | The input layer holds $x$, the hidden layers learn intermediate features, and the output layer produces the prediction $\hat{y}$. |
|||||||
| 28 | ||||||||
| 29 |  |
|||||||
| 30 | ||||||||
| 31 | *Each edge carries a weight in $W^{[l]}$ and each unit adds a bias then applies the activation.* |
|||||||
| 32 | ||||||||
| 33 | *Remark:* the bias is now written out explicitly and each layer has its own weight matrix $W^{[l]}$, unlike the earlier convention of folding the bias into $\theta^T x$ with $x_0 = 1$. This is the notation the Deep Learning course uses throughout. |
|||||||
| 34 | ||||||||
| 35 | ## 7.3 Output layer: binary and multiclass |
|||||||
| 36 | ||||||||
| 37 | The output layer matches the task, reusing the losses from the previous module. For two classes, a sigmoid output with the binary cross-entropy; for $k$ classes, a softmax output with the categorical cross-entropy: |
|||||||
| 38 | ||||||||
| 39 | $$\boxed{ \hat{y} = \frac{1}{1 + e^{-z}} \quad\text{(binary)} \qquad \hat{y}_c = \frac{e^{z_c}}{\sum_{j} e^{z_j}} \quad\text{(multiclass)} }$$ |
|||||||
| 40 | ||||||||
| 41 | ## 7.4 Activation functions and the zero-centered problem |
|||||||
| 42 | ||||||||
| 43 | The hidden activation is usually the sigmoid, the hyperbolic tangent, or the rectified linear unit: |
|||||||
| 44 | ||||||||
| 45 | $$\boxed{ \sigma(z) = \frac{1}{1 + e^{-z}}, \qquad \tanh(z), \qquad \mathrm{ReLU}(z) = \max(0, z) }$$ |
|||||||
| 46 | ||||||||
| 47 | The sigmoid saturates in its tails, and its outputs are never negative, so a unit's incoming weights all receive gradients of the same sign and the updates zig-zag. The zero-centered $\tanh$ removes that bias, and ReLU avoids positive-side saturation altogether, which is why it is the common default. |
|||||||
| 48 | ||||||||
| 49 |  |
|||||||
| 50 | ||||||||
| 51 | *The tanh is zero-centered while the sigmoid is not, and ReLU stays linear for positive inputs.* |
|||||||
| 52 | ||||||||
| 53 | ## 7.5 Chain rule and backpropagation |
|||||||
| 54 | ||||||||
| 55 | Training minimizes the loss by gradient descent, which needs its gradient with respect to every weight. Backpropagation computes all of them in one forward and one backward sweep: the forward pass caches each $z^{[l]}$ and $a^{[l]}$, then the backward pass applies the chain rule from the loss back to the first layer, reusing the cache. With the layer error $\delta^{[l]} = \partial L / \partial z^{[l]}$, |
|||||||
| 56 | ||||||||
| 57 | $$\boxed{ \delta^{[l]} = \left((W^{[l+1]})^T \delta^{[l+1]}\right) \odot g'^{[l]}\!\left(z^{[l]}\right), \qquad \frac{\partial L}{\partial W^{[l]}} = \delta^{[l]} (a^{[l-1]})^T }$$ |
|||||||
| 58 | ||||||||
| 59 |  |
|||||||
| 60 | ||||||||
| 61 | *The [Backpropagation](/en/Deep%20Learning/05%20Backpropagation) lesson of the Deep Learning course derives this step by step.* |
|||||||
| 62 | ||||||||
| 63 | ## 7.6 Training in practice |
|||||||
| 64 | ||||||||
| 65 | - **Mini-batches.** Estimate the gradient on a small batch of examples at a time, a middle ground between the full batch (accurate but slow) and one example (noisy but cheap). |
|||||||
| 66 | - **Vanishing gradient.** Through many saturating layers the backpropagated gradient is a product of small factors and shrinks toward zero, so early layers barely learn. ReLU activations and careful initialization keep it alive. |
|||||||
| 67 | - **Initialization.** Start the weights small and random to break symmetry, scaling the variance by the number of inputs (Xavier or He), so signals neither vanish nor explode through depth. |
|||||||
| 68 | - **Dropout.** Randomly zero a fraction of units during training. This prevents units from co-adapting and acts as a regularizer, one of the topics of the [next module](/en/Machine%20Learning/08%20Regularization%20and%20high-dimensional%20inference). |
|||||||
| 69 | ||||||||
| 70 | ## 7.7 Sanity checks and vectorization |
|||||||
| 71 | ||||||||
| 72 | Backpropagation is error-prone, so check the analytic gradient against a numerical finite-difference estimate: |
|||||||
| 73 | ||||||||
| 74 | $$\boxed{ \frac{\partial L}{\partial \theta} \approx \frac{L(\theta + \varepsilon) - L(\theta - \varepsilon)}{2\varepsilon} }$$ |
|||||||
| 75 | ||||||||
| 76 | and implement the passes in vectorized form, one matrix operation per layer over the whole mini-batch (columns are examples), which is both clearer and far faster: |
|||||||
| 77 | ||||||||
| 78 | $$\boxed{ Z^{[l]} = W^{[l]} A^{[l-1]} + b^{[l]} }$$ |
|||||||
| 79 | ||||||||
| 80 | *This module is the doorway to the [Deep Learning](/en/Deep%20Learning) course, which develops architectures, optimizers, initialization, normalization, and regularization in full. The next module returns to the linear setting to control model complexity.* |
|||||||
| 81 | ||||||||
| 82 | --- |
|||||||
| 83 | Next: [Regularization and high-dimensional inference](/en/Machine%20Learning/08%20Regularization%20and%20high-dimensional%20inference) · [Course overview](/en/Machine%20Learning) |
|||||||
