Skip to content

3.1 Shallow and deep networks

ready≈ 11.5 hoursNeeds: 2.3Unlocks: 3.2
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Network playground, ReLU region viewer)
  4. 4 Do
  5. 5 Check

Logistic regression from 2.3 draws one straight boundary. A neural network first computes new features, then fits a linear model on them. One hidden layer with weights W1\mathbf{W}_1 computes h=ReLU(xW1+b1)\mathbf{h} = \mathrm{ReLU}(\mathbf{x}\mathbf{W}_1 + \mathbf{b}_1), and the output is hW2+b2\mathbf{h}\mathbf{W}_2 + \mathbf{b}_2. This course stores examples as rows, so a layer is A @ W + b with W of shape (inputs, outputs). UDL writes the same layer transposed, β+Ωh\boldsymbol\beta + \boldsymbol\Omega\mathbf{h}, with examples as columns (UDL §3.1, §4.4).

A shallow net is a sum of hinges. With one input, hidden unit jj is max⁡(0,wjx+bj)\max(0, w_j x + b_j): flat on one side of x=−bj/wjx = -b_j/w_j and a ramp on the other. The output adds DD such hinges, so it is piecewise linear with at most D+1D+1 pieces (UDL §3.1.1 and §3.2). Add hidden units in the ReLU region viewer and watch each one add a kink.

Universal, but not easy. With enough hinges a shallow net can approximate any continuous function on a bounded region as closely as you like (UDL §3.2; Hornik 1991). The theorem says such weights exist. It does not say how many units you need, or that gradient descent will find them from data.

Depth composes folds. A second layer applies hinges to the first layer’s output, and each one can fold the input space onto itself. The folding weights in the region viewer give 2L2^L pieces from LL layers of just two units, while one layer with the same number of parameters is limited to 2L+12L+1 (UDL §4.1–4.5; Montúfar et al. 2014). That count is what one construction achieves, not a ceiling (UDL §4.5.2 gives the general bound). Random weights rarely come close, but the extra capacity is why deep networks fit complicated functions with few parameters.

Counting parameters. A layer from ninn_\text{in} to noutn_\text{out} units has ninnoutn_\text{in} n_\text{out} weights and noutn_\text{out} biases. The playground’s readout counts them for you. The initial weights matter too: He initialisation draws them with variance 2/nin2/n_\text{in} so that activations neither shrink nor blow up with depth, which module 3.4 explains (UDL §7.5).

★ Network playground

How depth bends decision regions

Static view on small screens. Open this page on a laptop to interact.

training loss
steps 0 loss 0.000 training accuracy 41% parameters 17

Full-batch Adam on the binary cross-entropy. Shading is the predicted class and its confidence. With 0 hidden layers this is logistic regression and every boundary is a straight line. One hidden layer of ReLUs gives a boundary made of straight segments; more layers fold the plane more times. Edge colour is the weight's sign, thickness its size.

Left: a 2-D dataset of two classes with the network’s predicted class shaded over the plane. Right: a diagram of the network with each weight drawn by sign and size, and the training loss curve. Controls choose the dataset, number of hidden layers (0 to 4), units per layer, activation and learning rate, and run full-batch Adam live (an optimizer from module 3.3; for now, think of it as a smarter gradient descent). Keyboard: Tab through the controls; arrow keys adjust sliders; Train starts and pauses.

Try circles with 0 hidden layers, then 1 layer of 2 units, then 1 layer of 4. Then try the spiral: compare 1 layer of 8 units with 3 layers of 4.

ReLU region viewer

Networks as piecewise-linear functions

Static view on small screens. Open this page on a laptop to interact.

linear pieces 5 hidden units 4 parameters 13 bound D + 1 = 5

Each hidden unit adds one hinge (dashed lines show each unit's contribution, relu(wx + b) times its output weight). A sum of D hinges has at most D + 1 linear pieces, and fewer when a hinge falls outside the window. Vertical lines mark where the slope changes.

The output of a network with one input and one output, plotted for x from −3 to 3, with vertical lines where its slope changes. One wide layer shows each hidden unit’s hinge as a dashed curve. The deep modes stack layers of two ReLU units, with random weights or with weights that fold the interval in half at every layer. The readout compares the number of linear pieces with the maximum. Keyboard: Tab through the buttons and sliders; arrow keys adjust sliders.

Write ReLU, He initialisation and the forward pass for any list of layer sizes, keeping a cache that module 3.2 will need for backprop. Then count linear pieces numerically and check both claims from the Bridge: at most D+1D+1 for one layer, and 2L2^L for the folding network.

Shallow and deep networks stubs/m3_1_mlp.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m3_1_mlp.py and run mlc check 3.1. The tests are identical; import the results on the progress page.

Hints · mlp_forward 0/3 revealed
Hints · count_linear_regions_1d 0/3 revealed

Locally: mlc check 3.1.

1. How many parameters does a network with layer sizes [784, 128, 10] have?
2. With one input, what does a single ReLU hidden unit contribute to the output?
3. A one-input network has a single hidden layer of 10 ReLUs. At most how many linear pieces can its output have?
4. Why does the output layer usually have no ReLU?
5. The universal approximation theorem guarantees that…
6. Same parameter budget: one wide layer, or several narrow layers. Which can make more linear pieces?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.