Blame
|
1 | # 2. Multilayer perceptron |
||||||
| 2 | ||||||||
| 3 | A perceptron is one unit, $a = g(w^T x + b)$. Stack many units that read the same input and you get a layer, stack layers and you get a multilayer perceptron (MLP). This module builds the MLP from units, writes forward propagation for one example and for a mini-batch, tracks the shapes and the parameter count, and states the universal approximation theorem. |
|||||||
| 4 | ||||||||
| 5 | **Objectives** |
|||||||
| 6 | - Build a layer as a stack of perceptron-like units reading a shared input. |
|||||||
| 7 | - Write forward propagation for one example with explicit per-layer weights and bias. |
|||||||
| 8 | - Vectorize the forward pass over a mini-batch with broadcast bias. |
|||||||
| 9 | - Track the shape of every $W^{[l]}$ and $b^{[l]}$ and count the parameters. |
|||||||
| 10 | - State the universal approximation theorem and contrast width against depth. |
|||||||
| 11 | ||||||||
| 12 | ## 2.1 From a unit to a layer |
|||||||
| 13 | ||||||||
| 14 | ### 2.1.1 A single unit |
|||||||
| 15 | ||||||||
| 16 | A unit takes an input vector $x \in \mathbb{R}^{n_0}$, forms a weighted sum with a weight vector $w$ and a bias scalar $b$, then applies a nonlinear activation $g$: |
|||||||
| 17 | ||||||||
| 18 | $$\boxed{ a = g\left(w^T x + b\right) }$$ |
|||||||
| 19 | ||||||||
| 20 | This is the perceptron of the previous course, except the hard step is now a smooth activation such as the sigmoid or ReLU. The activation is named here and defined fully in the [next lesson](/en/Deep%20Learning/03%20Activation%20functions). |
|||||||
| 21 | ||||||||
| 22 | ### 2.1.2 A layer of units |
|||||||
| 23 | ||||||||
| 24 | Now place $n_1$ units side by side, all reading the same input $x$. Unit $i$ has its own weight vector $w_i$ and bias $b_i$, producing $a_i = g(w_i^T x + b_i)$. Collect the weight vectors as the rows of a matrix $W^{[1]}$ and the biases into a vector $b^{[1]}$: |
|||||||
| 25 | ||||||||
| 26 | $$\boxed{ W^{[1]} = \begin{bmatrix} w_1^{T} \\ \vdots \\ w_{n_1}^{T} \end{bmatrix}, \quad b^{[1]} = \begin{bmatrix} b_1 \\ \vdots \\ b_{n_1} \end{bmatrix} }$$ |
|||||||
| 27 | ||||||||
| 28 | The whole layer then computes a pre-activation vector and an activation vector in one matrix expression, $z^{[1]} = W^{[1]} x + b^{[1]}$ and $a^{[1]} = g^{[1]}(z^{[1]})$, where $g^{[1]}$ is applied elementwise. |
|||||||
| 29 | ||||||||
| 30 | *Remark:* the rows of $W^{[1]}$ are exactly the individual unit weight vectors, so a layer is just many units packed into one matrix. Bias stays explicit here: unlike the Machine Learning course, which folded the intercept into $\theta$ via the augmented input $x_0 = 1$, this course keeps $b^{[l]}$ as its own vector. |
|||||||
| 31 | ||||||||
| 32 | ## 2.2 Forward propagation |
|||||||
| 33 | ||||||||
| 34 | Stacking $L$ such layers gives the MLP. Layer $l$ reads the activation of the layer below, $a^{[l-1]}$, and produces $a^{[l]}$. The input is $a^{[0]} = x$ and the prediction is the output of the last layer. |
|||||||
| 35 | ||||||||
| 36 | ### 2.2.1 One example |
|||||||
| 37 | ||||||||
| 38 | For $l = 1, \dots, L$: |
|||||||
| 39 | ||||||||
| 40 | $$\boxed{ a^{[0]} = x, \quad z^{[l]} = W^{[l]} a^{[l-1]} + b^{[l]}, \quad a^{[l]} = g^{[l]}\!\left(z^{[l]}\right), \quad \hat{y} = a^{[L]} }$$ |
|||||||
| 41 | ||||||||
| 42 | Each layer may use its own activation $g^{[l]}$: hidden layers typically use ReLU, while the output layer uses sigmoid or softmax for classification and the identity for regression. |
|||||||
| 43 | ||||||||
| 44 | *Remark:* the composition $\hat{y} = g^{[L]}(W^{[L]} g^{[L-1]}(\cdots g^{[1]}(W^{[1]} x + b^{[1]}) \cdots) + b^{[L]})$ is what makes the network expressive. Without the nonlinear $g^{[l]}$ the whole stack would collapse to a single linear map $W x + b$. |
|||||||
| 45 | ||||||||
| 46 | ### 2.2.2 Vectorized over a mini-batch |
|||||||
| 47 | ||||||||
| 48 | Training runs on batches, not single examples. Place $m$ examples as the columns of a matrix, so $A^{[0]} = X \in \mathbb{R}^{n_0 \times m}$, and the forward pass becomes a matrix product with the bias broadcast across all columns: |
|||||||
| 49 | ||||||||
| 50 | $$\boxed{ Z^{[l]} = W^{[l]} A^{[l-1]} + b^{[l]}, \quad A^{[l]} = g^{[l]}\!\left(Z^{[l]}\right) }$$ |
|||||||
| 51 | ||||||||
| 52 | Here $Z^{[l]}$ and $A^{[l]}$ have shape $n_l \times m$, one column per example. The bias $b^{[l]} \in \mathbb{R}^{n_l}$ is added to every column, an operation known as broadcasting. |
|||||||
| 53 | ||||||||
| 54 | *Remark:* the only change from the single-example form is that the vector $a^{[l-1]}$ becomes the matrix $A^{[l-1]}$. Processing a batch as one matrix multiply is what lets a GPU run the pass efficiently. |
|||||||
| 55 | ||||||||
| 56 | ## 2.3 Shapes and parameter count |
|||||||
| 57 | ||||||||
| 58 | The shapes follow from one rule: to compute $z^{[l]} = W^{[l]} a^{[l-1]} + b^{[l]}$, the matrix $W^{[l]}$ must map an $n_{l-1}$-vector to an $n_l$-vector. |
|||||||
| 59 | ||||||||
| 60 | $$\boxed{ W^{[l]} \in \mathbb{R}^{n_l \times n_{l-1}}, \quad b^{[l]} \in \mathbb{R}^{n_l} }$$ |
|||||||
| 61 | ||||||||
| 62 | Layer $l$ therefore holds $n_l \, n_{l-1}$ weights plus $n_l$ biases. Consider a small network with $n_0 = 4$ inputs, two hidden layers of $5$ and $3$ units, and a single output unit. |
|||||||
| 63 | ||||||||
| 64 | | Layer $l$ | $W^{[l]}$ shape | $b^{[l]}$ shape | Parameters | |
|||||||
| 65 | | --- | --- | --- | --- | |
|||||||
| 66 | | 1 | $5 \times 4$ | $5$ | $25$ | |
|||||||
| 67 | | 2 | $3 \times 5$ | $3$ | $18$ | |
|||||||
| 68 | | 3 | $1 \times 3$ | $1$ | $4$ | |
|||||||
| 69 | | Total | | | $47$ | |
|||||||
| 70 | ||||||||
| 71 | *Remark:* the input layer holds no parameters, it is just the data $a^{[0]} = x$. When counting layers we count the layers that carry weights, so this network has $L = 3$. |
|||||||
| 72 | ||||||||
| 73 | ## 2.4 A multi-layer network |
|||||||
| 74 | ||||||||
| 75 | The diagram below shows the same $4$-$5$-$3$-$1$ network as a flow of activations. Each arrow group is a full weight matrix, and each box applies its activation to the pre-activation. |
|||||||
| 76 | ||||||||
| 77 |  |
|||||||
| 78 | ||||||||
| 79 | *A multilayer perceptron: each layer computes z = W a + b then a = g(z), composing input a0 into the prediction aL.* |
|||||||
| 80 | ||||||||
| 81 | Information flows strictly left to right during the forward pass, which is why this is a feedforward network. Nothing loops back. The reverse direction, used to compute gradients, is the subject of a later lesson. |
|||||||
| 82 | ||||||||
| 83 | ## 2.5 Universal approximation |
|||||||
| 84 | ||||||||
| 85 | How expressive is an MLP? The universal approximation theorem gives a strong answer. Let $f$ be any continuous function on a compact set $K \subset \mathbb{R}^{n_0}$, and let $\varepsilon > 0$. Then there exists a network with a single hidden layer of finite width, using a suitable nonlinear activation, whose output $F$ satisfies: |
|||||||
| 86 | ||||||||
| 87 | $$\boxed{ \sup_{x \in K} \left| F(x) - f(x) \right| < \varepsilon }$$ |
|||||||
| 88 | ||||||||
| 89 | In words, one hidden layer with enough units can approximate any continuous function on a bounded region to any desired accuracy $\varepsilon$. This is an existence result, not a recipe: it promises that such weights exist, but says nothing about how many units are needed or how to find them. |
|||||||
| 90 | ||||||||
| 91 | *Remark:* the catch is width. Matching a target to accuracy $\varepsilon$ with one hidden layer can demand an enormous number of units, growing fast as $\varepsilon$ shrinks. Depth is usually far more parameter-efficient: stacking several narrow layers can represent functions that a single layer would need exponentially many units to match. This efficiency of depth over width is the practical reason the field is called deep learning. |
|||||||
| 92 | ||||||||
| 93 |  |
|||||||
| 94 | ||||||||
| 95 | *A network with one hidden layer approximates a target function by summing many simple activated units.* |
|||||||
| 96 | ||||||||
| 97 | *The network is only defined once the activations $g^{[l]}$ are fixed. The next lesson defines them, sigmoid, tanh, ReLU and its variants, and explains how each shapes learning.* |
|||||||
| 98 | ||||||||
| 99 | --- |
|||||||
| 100 | Next: [Activation functions](/en/Deep%20Learning/03%20Activation%20functions) · [Course overview](/en/Deep%20Learning) |
|||||||
