Deep compared with what?
In deep learning, “deep” doesn’t mean the model understands more or is any wiser. It means there are many layers of processing stacked on top of each other. Depth is a measure of the architecture, not of intelligence.
The reference definition comes from LeCun, Bengio and Hinton in Nature (28 May 2015): deep learning allows computational models made up of multiple processing layers to learn representations of data with multiple levels of abstraction (PubMed).
The intuition, without formulas: each layer takes what the previous one produced and turns it into another representation. Raw data goes in, for example the numbers that make up the pixels. Each step re-expresses them in a slightly more abstract form, and out of the other end comes the answer you are after.
Here I only explain what depth contributes. For everything else, there are other guides: how neural networks work, the types of networks and how deep learning and machine learning differ.
What each layer does
Let’s open the box. A layer such as PyTorch’s nn.Linear applies a linear transformation to its input using the weights and biases it stores. A non-linear activation is then applied, because activations are what create the complex mappings between inputs and outputs (PyTorch Tutorials). The scikit-learn documentation describes it the same way: a weighted linear sum followed by a non-linear activation function, such as the hyperbolic tangent (scikit-learn).
Using the notation from the Berkeley course by Mu Li and Alex Smola, a network with three hidden layers is written like this (STAT 157):
h1 = σ(W1·x + b1)
h2 = σ(W2·h1 + b2)
h3 = σ(W3·h2 + b3)
o = W4·h3 + b4
Each layer takes the output of the previous one as its input. A popular choice for σ is the ReLU, σ(x) = max(0, x), which operates element by element (Dive into Deep Learning).
And how is each layer tuned? With backpropagation. According to LeCun et al., this algorithm indicates how a machine should change the internal parameters that compute the representation in each layer from the representation in the previous one. I go through it step by step in gradient descent and backpropagation.
From raw data to concepts: what has been observed
The hierarchy of abstraction is not just a metaphor, because people have looked inside trained networks. Zeiler and Fergus (2013) presented a visualisation technique that gives insight into the function of the intermediate layers of a convolutional network. They also ran an ablation study to see how much each layer contributes to performance (arXiv:1311.2901).
Yosinski et al. visualised the activations of each layer while the network processed an image or a video. According to the authors, in the later convolutional layers the channels tend to correspond to specific parts, such as wheels or faces. In addition, the variation of the patterns increases with the layer number, which points to representations that become more and more invariant (arXiv:1506.06579).
Be careful not to read this as a tidy order. The same authors point out that not all features correspond to natural parts. And observing is one thing, explaining is another. The NeurIPS 2020 paper on hierarchical learning notes that a widespread belief is that deep networks perform efficient hierarchical learning, with layers that learn increasingly useful representations. Its authors state that, despite abundant empirical evidence, they lack a practical theory to understand this ability (NeurIPS 2020).
Why adding layers changes what the network can learn
First, what doesn’t work
Stacking layers without a non-linear activation is pointless. With a hidden layer h = W1x + b1 and an output o = w2ᵀh + b2, you get o = w2ᵀW1x + b′, which is still linear (STAT 157). Two linear layers collapse into one. Dive into Deep Learning leaves it as an exercise: prove that adding layers to a deep linear network never increases its expressive power (d2l.ai).
The XOR case
The classic problem is the XOR function (the output is 1 only when the two inputs are different). According to the Berkeley slides, Minsky and Papert showed in 1969 that the perceptron cannot learn it, because its neurons can only produce linear separators. With hidden layers and a non-linear activation, it can be done.
What the extra layers gain
The classic “depth separation” results show that deep networks can approximate certain functions with far fewer parameters than shallow ones (NeurIPS 2020). In practice, the same paper notes that on ImageNet classification a 152-layer residual network can reach 8%–10% higher top-1 accuracy than an 18-layer ResNet. That is a specific result, not a guarantee that more layers is always better.
A minimal example: XOR with and without a hidden layer
Code makes it clearer. This script trains two models with gradient descent written by hand: one with no hidden layer (a single linear separator) and one with a hidden layer of eight tanh neurons. It checks itself with assert.
import numpy as np
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
y = np.array([[0], [1], [1], [0]], dtype=float) # XOR
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def entrenar(n_ocultas, pasos=5000, lr=0.5, semilla=0):
rng = np.random.default_rng(semilla) # fixed seed
n = len(X)
if n_ocultas == 0: # no hidden layer: a single linear separator
W = rng.normal(0, 1, (2, 1)); b = np.zeros((1, 1))
for _ in range(pasos):
dz = (sigmoid(X @ W + b) - y) / n
W -= lr * X.T @ dz
b -= lr * dz.sum(0, keepdims=True)
return sigmoid(X @ W + b)
W1 = rng.normal(0, 1, (2, n_ocultas)); b1 = np.zeros((1, n_ocultas))
W2 = rng.normal(0, 1, (n_ocultas, 1)); b2 = np.zeros((1, 1))
for _ in range(pasos):
h = np.tanh(X @ W1 + b1) # hidden layer
p = sigmoid(h @ W2 + b2) # output
dz2 = (p - y) / n # error at the output
dz1 = (dz2 @ W2.T) * (1 - h ** 2) # propagated backwards
W2 -= lr * h.T @ dz2; b2 -= lr * dz2.sum(0, keepdims=True)
W1 -= lr * X.T @ dz1; b1 -= lr * dz1.sum(0, keepdims=True)
return sigmoid(np.tanh(X @ W1 + b1) @ W2 + b2)
p_lineal = entrenar(0)
p_oculta = entrenar(8)
acierto_lineal = ((p_lineal > 0.5) == y).mean()
print("No hidden layer:", p_lineal.ravel().round(2))
print("With hidden layer:", p_oculta.ravel().round(2))
# A linear separator never gets more than 3 of the 4 cases right
assert acierto_lineal <= 0.75
# The network with a hidden layer must output 0, 1, 1, 0
assert (p_oculta > 0.5).astype(int).ravel().tolist() == [0, 1, 1, 0]
print("OK")
What you should see: the model with no hidden layer leaves all four probabilities close to indecision, because no straight line separates the ones from the zeros. The model with a hidden layer pushes them towards 0, 1, 1 and 0, and the script finishes by printing “OK”. If an assert fails, the script will let you know loudly.
Common mistakes when thinking about “deep”
- Believing more layers is always better. The ResNet figure is a specific result on ImageNet, and the authors acknowledge that a practical theory is still missing.
- Forgetting the non-linear activation between layers. The network looks deep, but behaves like a single linear layer.
- Reading the visualisations as “each layer detects a human concept”. Yosinski et al. warn that not all features correspond to natural parts.
- Taking training accuracy at face value. With many parameters, you also have to watch for memorisation; see how to detect overfitting.
An opinion, not a fact: writing the minimal version by hand helps you see that there is no magic, just layers that transform the output of the one before.
To practise
- Replace
np.tanhwith the identity (remove the activation) in the network with a hidden layer and check that theassertfails. It is the linear collapse from above, in code. - Vary
n_ocultas(1, 2, 4, 16) and observe at which values the network stops solving XOR. - Reproduce the example in PyTorch with
nn.Sequential, an ordered container of modules through which the data passes in the same order, andnn.Linear, using only what its documentation supports.
To continue, review which problem each type of network solves.
Sources
- Deep learning (LeCun, Bengio and Hinton, Nature 521(7553):436-444, 28 May 2015) – PubMed
- Visualizing and Understanding Convolutional Networks (Zeiler and Fergus, arXiv:1311.2901, November 2013)
- Understanding Neural Networks Through Deep Visualization (Yosinski et al., arXiv:1506.06579)
- Towards Understanding Hierarchical Learning: Benefits of Neural Representations (NeurIPS 2020)
- 5.1. Multilayer Perceptrons — Dive into Deep Learning 1.0.3
- Introduction to Deep Learning, 6. Multilayer Perceptron (Mu Li and Alex Smola, STAT 157, UC Berkeley, spring 2019)
- 1.17. Neural network models (supervised) — scikit-learn 1.9.0 documentation
- Build the Neural Network — PyTorch Tutorials 2.15.0
Translated and adapted by Ollie.