13

The deep learning big bang

Why did in 2012 what did not work in 1990?

10 min

The deep learning big bang

With voice. You can pause and jump to any step.

Lesson complete

Subtitles

Backpropagation was published in 1986, and convolutional networks were reading zip codes by 1989. Yet the takeoff of deep learning, networks with many layers, came only in 2012. What changed?

Voice

No voice: the lesson advances on its own with subtitles.

The voice for this lesson is being recorded: for now it advances on its own with subtitles.

0:00 / 0:00

What you will achieve

  • Explain why deep learning took off around 2012: massive data, compute with GPUs, and a few training tricks.
  • Predict when a GPU beats a CPU at multiplying matrices and why its advantage grows with the size of the problem.
  • Explain what a residual connection does to the gradient and why it makes it possible to stack many more layers.

Key ideas

  1. The takeoff of deep learning did not come from a new idea, but from three ingredients that coincided: massive data, compute, and training tricks.
  2. ImageNet put millions of labeled photos within everyone’s reach, and the ILSVRC contest measured progress with a common yardstick: the top-5 error.
  3. A GPU multiplies matrices with thousands of cores at once, exactly the work of training; its advantage grows with the size of the problem.
  4. AlexNet (2012) cut the top-5 error from the runner-up’s 26.2% to 15.3% with ReLU, dropout, and two GPUs; GPU memory already limited the size of the network.
  5. A residual connection, y = x + F(x), gives the gradient a direct path and made networks of more than a hundred layers possible; the compute of the largest training runs doubled every few months.

What many people believe

Common belief Deep learning took off in 2012 because someone finally invented a revolutionary idea.

What is true Backpropagation and convolutions already existed in the 1980s and 1990s, and training on GPUs had even been tried before. What changed was scale: millions of labeled images, GPUs, and a few tricks (ReLU, dropout, residual shortcuts).

Check yourself

Four questions. No grade, no account: they just show whether the idea stuck.

ConceptWhy does a GPU fit neural-network training so well?
CalculationIf each layer halves the gradient as it is backpropagated, what fraction of the output gradient reaches ten layers back?
ConceptIn a residual block, the output is y = x + F(x). What advantage does that “+ x” give during backpropagation?
ApplicationA vendor boasts a 3.6% error on ImageNet and proposes using its vision model to inspect parts in your factory. What do you do first?

Under the hoodFor those who want the math and the code. You do not need it to follow the course.

The 2012 numbers

From the AlexNet paper (Krizhevsky, Sutskever and Hinton, 2012): eight layers with weights (five convolutional and three dense), about 60 million parameters, about 650,000 neurons, more than 1.2 million training images, and five to six days on two GTX 580 3 GB GPUs. The input has 150,528 values (224×224×3224\times224\times3). Two more details from the same paper: in its figure 1, a four-layer network on CIFAR-10 reached 25% training error six times faster with ReLU than with tanh (the authors note the effect varies with the architecture), and dropout, with probability 0.5 in the first two dense layers, was used because without it the network overfit substantially, at the cost of roughly doubling the iterations needed.

Model in ILSVRC 2012 Top-5 error
SIFT + Fisher vectors (runner-up) 26.2%
1 convolutional network (validation) 18.2%
5 networks averaged 16.4%
7 networks, 2 of them pre-trained on 15 M images 15.3%

So the “15.3%” figure comes from an ensemble of seven networks and extra data; with only the contest data it was 16.4%. Even so, the gap to the runner-up is huge.

Multiplying matrices: why the GPU wins

The product C=ABC=AB of two n×nn\times n matrices is

Cij=∑k=1nAikBkj.C_{ij}=\sum_{k=1}^{n}A_{ik}B_{kj}.

That is n2n^2 independent cells, each costing nn multiply-adds: n3n^3 in total, or 2n32n^3 floating-point operations (FLOP: one addition or multiplication with decimals). With PP cores doing one multiply-add per step, the ideal number of steps is

T(n,P)=⌈n2P⌉n,T(n,8)T(n,P)  ⟶  P8 when n2≫P.T(n,P)=\left\lceil \frac{n^2}{P}\right\rceil n,\qquad \frac{T(n,8)}{T(n,P)}\;\longrightarrow\;\frac{P}{8}\ \text{when }n^2\gg P.

With P=512P=512 (GTX 580) the ideal ceiling on the advantage is 64; with P=3584P=3584 (RTX 3060), 448. It is an idealized count: it ignores memory bandwidth, the cost of moving data to the GPU, and the fact that each GPU core is slower than a CPU core. The real measurements of Raina, Madhavan and Ng (2009), with a GTX 280 GPU against a dual-core CPU, were between 12.9 and 72.6 times faster, depending on the model size.

A chip’s peak speed is cores×2×clock\text{cores}\times 2\times\text{clock}: 512×2×1.544 GHz≈1.58512\times2\times1.544\ \text{GHz}\approx1.58 TFLOP/s for the GTX 580 (the figure OpenAI uses) and, by the same arithmetic, about 12.7 TFLOP/s for the RTX 3060.

The cost of training AlexNet

According to the estimate by Amodei and Hernandez (OpenAI, 2018), the forward pass costs about 0.75×1090.75\times10^{9} multiply-adds per image; training adds 2 operations per multiply-add, the backward pass (a factor of 3), and about 90 passes over the dataset:

1.2×106⋅90⋅0.75×109⋅2⋅3≈4.9×1017 FLOP.1.2\times10^{6}\cdot 90\cdot 0.75\times10^{9}\cdot 2\cdot 3\approx 4.9\times10^{17}\ \text{FLOP}.

That is consistent with two GTX 580s for 5.5 days at 33% of their peak, which is what OpenAI assumes: 2⋅1.58×1012⋅5.5⋅86,400⋅0.33≈5×10172\cdot1.58\times10^{12}\cdot5.5\cdot86{,}400\cdot0.33\approx5\times10^{17}. Since 2012 (from AlexNet to AlphaGo Zero, in late 2017), OpenAI measured the compute of the largest training runs doubling every 3.4 months; later estimates (Sevilla et al., 2022) give about 6 months for the deep learning era. Either way, growth is exponential.

The gradient through the layers

In a plain stack, hl=tanh⁡(Wlhl−1)h_l=\tanh(W_l h_{l-1}) and the gradient flows back as

∂L∂hl−1=Wl⊤(tanh⁡′(zl)⊙∂L∂hl),\frac{\partial L}{\partial h_{l-1}}=W_l^{\top}\Big(\tanh'(z_l)\odot\frac{\partial L}{\partial h_l}\Big),

so its norm is multiplied, at each layer, by a factor of roughly σ⋅tanh⁡′‾\sigma\cdot\overline{\tanh'}. With the era’s “standard” initialization, n Var[W]=1/3n\,\mathrm{Var}[W]=1/3 (Glorot and Bengio, 2010, eq. 15), the factor is about 0.5; in the lesson’s toy it comes out ≈ 0.55, and 0.5540≈4×10−110.55^{40}\approx 4\times10^{-11}.

With a shortcut, xl+1=xl+F(xl)x_{l+1}=x_l+F(x_l) and, according to He et al. (2016, eq. 5),

∂E∂xl=∂E∂xL(I+∂∂xl∑i=lL−1F(xi)).\frac{\partial E}{\partial x_l}=\frac{\partial E}{\partial x_L}\Big(I+\frac{\partial}{\partial x_l}\sum_{i=l}^{L-1}F(x_i)\Big).

The II term carries the gradient from the output to any layer without passing through any weight. In the toy, each block also adds its contribution and the norm grows a little (between 2 and 18 at 40 layers, depending on the seed).

What the toy does not say

The toy network is untrained, uses no normalization, and all its layers are alike. In real networks, initialization and batch normalization keep signals healthy. In fact, He et al. (2015, §4.1 and figure 1) argue that the difficulty of training deep plain networks does not seem to come from vanishing gradients: their plain networks had gradients of healthy norm and still the 34-layer one had higher training error than the 18-layer one, and the 56-layer one more than the 20-layer one on CIFAR-10. They called it the degradation problem. Their hypothesis is that it is easier for a block to learn F≈0F\approx0 (identity) than to fit an identity with nonlinear layers; the authors themselves leave that explanation as an open question. What was measured is clear: with shortcuts, networks of 50, 101, and 152 layers improved with depth. ResNet-152 (11.3 billion multiply-adds) costs less than VGG-19 (19.6 billion); on its own it reached 4.49% top-5 error on validation, and an ensemble of six models, 3.57% on the test set.

In code

import numpy as np

def grad_norms(depth, residual, width=16, seed=0):
    rng = np.random.default_rng(seed)
    W = rng.normal(0, np.sqrt(1/3) / np.sqrt(width), size=(depth, width, width))
    h = rng.normal(size=width)
    cache = []
    for l in range(depth):                        # forward pass
        t = np.tanh(W[l] @ h)
        cache.append(t)
        h = h + t if residual else t
    g = np.ones(width) / np.sqrt(width)           # gradient of norm 1 at the output
    norms = [np.linalg.norm(g)]
    for l in reversed(range(depth)):              # backpropagation
        branch = W[l].T @ (g * (1 - cache[l] ** 2))
        g = g + branch if residual else branch    # with a shortcut, the gradient passes untouched and the branch is added
        norms.append(np.linalg.norm(g))
    return norms                                  # norms[k]: norm after k layers backward

for res in (False, True):
    n = grad_norms(40, res)
    print("with shortcuts" if res else "plain", f"after 10 layers: {n[10]:.2e}   after 40: {n[40]:.2e}")
# plain after 10 layers: 5.30e-03   after 40: 1.16e-10
# with shortcuts after 10 layers: 2.51e+00   after 40: 1.48e+01

The exact numbers depend on the seed and the random generator (the lesson’s gradients.py file also checks backpropagation against finite differences); the order of magnitude does not. And the ideal race count:

from math import ceil

def steps(n, cores):            # n² independent cells; each costs n multiply-adds
    return ceil(n * n / cores) * n

print(steps(64, 8), steps(64, 512), steps(64, 3584))   # 8-core CPU, GTX 580, RTX 3060
# 32768 512 128

What comes next

Residual connections and normalization reappear in every block of the transformer (lesson 22). Lesson 14 moves from seeing to representing meaning with embeddings. The accounting of FLOPs, data, and cost is formalized in lesson 24, and GPU memory, which already limited AlexNet, rules in lesson 25.

Decision lens

What this idea means if your job is to decide, not to code.

  • When someone says “our model is better,” ask what changed: more data (whose, and under what license?), more compute (how many GPU hours, at what cost?), or a better method. Those are the three engines of modern progress.
  • A result on a public benchmark describes a specific task, not yours: a 3.57% error on ImageNet does not tell you how often it will fail on your photos. Ask for a test with your own data and with the real cost of each kind of error.
  • Compute is a budget and access item: GPUs bought or rented by the hour, with limited memory. Training a large model from scratch is out of reach for almost every organization; the usual route is to start from a pre-trained one.
  • Deeper or bigger is not automatically better: without the right ideas (normalization, shortcuts), a deep network does not even train well. Ask for results on unseen data, not just the number of parameters or layers.

Terms in this lesson

Full transcript
CommentsThis lesson’s public conversation: questions, facts to check, ideas.

Comments live in GitHub Discussions, in the course’s public repository; writing one requires a GitHub account. They load from giscus.app when you open this section. The course reviewers read them, and if a change is accepted, it is applied in both languages. Open the thread on GitHub

Sources