B · Neural networks
Deep Networks
Layers of neurons, each turning the previous layer's output into new features: the network learns its own feature engineering.
Try it: B1 · Going Deep
Intuition
One neuron draws one straight line. Feature engineering showed that a linear model can draw curves if someone hands it the right features. A deep network removes the "someone": it puts a layer of neurons in front of the linear model, and that layer computes the features. Each hidden neuron draws its own line and reports which side the input is on, the next layer combines those reports into shapes, and the final neuron makes its decision from the shapes.
Stack more layers and each one builds on the last: in an image model, early layers respond to edges, middle ones to textures and parts, late ones to whole objects. Nobody designs these features. They are weights like any others, set by gradient descent because they help lower the loss.
The math
A layer
A layer is many neurons side by side, sharing the same input. Write each neuron's weights as a row of a matrix \(W\), their biases as a vector \(\mathbf{b}\), and apply an activation \(f\) to each entry:
\[ \mathbf{h} = f(W\mathbf{x} + \mathbf{b}) \]A network chains layers, each one's output the next one's input:
\[ \mathbf{h}^{(1)} = f(W^{(1)}\mathbf{x} + \mathbf{b}^{(1)}), \quad \mathbf{h}^{(2)} = f(W^{(2)}\mathbf{h}^{(1)} + \mathbf{b}^{(2)}), \quad \dots, \quad \hat{y} = g(W^{(L)}\mathbf{h}^{(L-1)} + \mathbf{b}^{(L)}) \]The last activation \(g\) suits the task: none for a number, a sigmoid for yes/no, a softmax for one of many classes. Computing this left to right is the forward pass.
Counting weights
A layer from \(m\) inputs to \(n\) neurons has \(mn\) weights and \(n\) biases. A network of 784 inputs (a 28×28 image), two hidden layers of 128 and an output layer of 10 has \(784\cdot128 + 128 + 128\cdot128 + 128 + 128\cdot10 + 10 = 118{,}282\) parameters.
Why the activation is essential
Without \(f\), two layers give \(W^{(2)}(W^{(1)}\mathbf{x} + \mathbf{b}^{(1)}) + \mathbf{b}^{(2)}\), which is just one matrix \(W^{(2)}W^{(1)}\) and one bias: a single linear layer. Any depth of linear layers collapses to one. The nonlinearity between them is what makes depth add anything; see Activation functions.
Worked example: XOR with two layers. Hidden neurons with a step: \(h_1 = \text{OR}(x_1, x_2)\) using weights \((1, 1)\), bias \(-0.5\); \(h_2 = \text{NAND}(x_1, x_2)\) using \((-1, -1)\), bias \(1.5\). Output: \(\text{AND}(h_1, h_2)\) using \((1, 1)\), bias \(-1.5\).
For \((1, 0)\): \(h_1 = 1\), \(h_2 = 1\), output 1. For \((1, 1)\): \(h_1 = 1\), \(h_2 = 0\), output 0. For \((0, 0)\): \(h_1 = 0\), output 0. That is XOR, which no single neuron can compute.
What depth buys
- Universal approximation. One hidden layer with enough neurons can approximate any continuous function on a bounded region as closely as you like (Cybenko 1989, Hornik 1991). So depth is not needed for what a network can represent.
- Efficiency. Depth changes how many neurons it takes. Some functions need exponentially many neurons in one layer but only a modest number spread over several, because each layer can reuse what the previous one computed. Folding a space in half twice takes two layers; doing it in one layer means drawing every fold separately.
- Good inductive bias. Real data tends to be built hierarchically (pixels into edges into parts into objects; letters into words into phrases), and layered models fit that structure.
Variants
- Fully connected (dense) networks, as here, also called multilayer perceptrons (MLPs).
- Convolutional networks share small weight patterns across an image; see Convolution.
- Recurrent networks apply the same layer step after step along a sequence.
- Transformers mix information between positions with attention, then apply a small MLP to each position.
- Residual connections add each block's input to its output, \(\mathbf{h} + F(\mathbf{h})\), so a block only has to learn a correction; they made networks hundreds of layers deep trainable.
When it fails
- Training is the hard part. Deep networks are easy to write and hard to train: gradients can vanish or explode through many layers (Initialization), and the loss is not convex.
- Data hunger. More parameters can fit more noise. A network with 100,000 weights and 1,000 examples will memorise them unless held back (Regularization).
- Opacity. The learned features are not labelled; understanding what a network computes is a research field of its own.
History
Multi-layer networks were imagined early (Rosenblatt considered them), but nobody could train the hidden layers until backpropagation was popularised by Rumelhart, Hinton and Williams in 1986. Through the 1990s and 2000s shallow models usually won. Around 2006–2012 better initialization, ReLU activations, GPUs and large datasets changed that; AlexNet's win in the 2012 ImageNet competition, by a wide margin, started the deep learning era.
Connections
Builds on
- The PerceptronA deep network is many neurons, arranged in layers that feed each other.
- Feature EngineeringEach hidden layer computes features for the next one, so the network learns the features you would otherwise engineer by hand.
Leads to
- Activation FunctionsActivations are what make depth useful: stacked linear layers without them equal a single linear layer.
- The Chain Rule & Computational GraphsA deep network is a long chain of functions, exactly the shape the chain rule differentiates.
- Weight InitializationThe deeper the network, the more any change in the signal's size compounds, layer after layer.
- ConvolutionA convolutional layer is a dense layer where each neuron sees only a small patch and all neurons share their weights.
- EmbeddingsAn embedding is the first layer of a network: a lookup table of weights learned like any other.
Further reading
- Neural Networks Part 1 (Stanford CS231n)Layers, activation functions and why ReLU is the usual choice, with the matrix form used in forward().
- Neural Networks, Manifolds, and Topology (Chris Olah)Pictures of how each layer bends the input space: what the squares in the network diagram are showing.
- But what is a neural network? (3Blue1Brown)Why stacking layers lets a network pick out more and more complex features.