Simple Algorithm
Deep Learning · Activation · 16 min

What Is Deep Learning?

Stacking simple functions until they learn features no one designed.

In 2012 a network that had never been told what an edge, a wheel or a face looks like beat the best hand-engineered vision systems by a wide margin. That is deep learning in one sentence: stack many simple, adjustable functions, show them examples, and the features that matter appear on their own.

What you’ll learn

  • Explain deep learning as fitting a composition of many simple, adjustable layers, whose features are learned from raw data instead of designed by hand.
  • Compute what one neuron outputs and how many parameters a layer has, and explain why depth is useless without a nonlinearity between layers.
  • Describe the recipe behind every deep learning system: data, architecture, loss, gradient-based training and evaluation on held-out data.
  • Explain what made deep learning take off around 2012, and name what it still does badly.

Features no one designed

How would you write a program that tells a handwritten 3 from an 8? You could try rules ("an 8 has two closed loops"), but real handwriting breaks every rule you write. For decades, computer vision split the job in two. Experts designed features, numbers computed from the pixels such as the strength and direction of edges in each patch, and a simple trainable classifier learned how much to trust each one.

Deep learning hands both jobs to the data. It builds the whole program as a chain of simple functions called layers, each with adjustable numbers called parameters, and tunes all of them at once so that the final output comes out right:

f(x)=fL(⋯f3(f2(f1(x))))f(\mathbf x) = f_L\Big(\cdots f_3\big(f_2(f_1(\mathbf x))\big)\Big)
Raw input x\mathbf x (pixel values, audio samples, characters) goes in at the right; each layer transforms the output of the one before; the last layer produces the answer. Deep just means many layers: LL of them, from a handful to hundreds.
ApproachWhat a person designsWhat is learned from examples
Hand-written rulesthe whole programnothing
Classical machine learningthe features (edge histograms, word counts, …)the weights of a simple classifier on top
Deep learningthe architecture: which layers, how many, how wideevery layer's parameters, and with them the features
Because the middle layers compute their own features, deep learning is also called representation learning.
Think first Hand-designed features worked for decades. Why replace them?

Because the most useful features are often impossible to put into words. Which pixel pattern makes a face a face, under every lighting, angle and expression? Learned features are also tailored to each new task for free. Given enough examples, learned features overtook hand-designed ones in vision, speech and language. With few examples, a classical model on well-chosen features can still win.

One neuron, then a layer

The building block is the artificial neuron. It takes some numbers x1,x2,…,xdx_1, x_2, \dots, x_d, multiplies each by a weight, adds the results and a bias bb, and passes the total zz through a fixed bend called an activation function ϕ\phi:

z=w1x1+w2x2+⋯+wdxd+b,h=ϕ(z)z = w_1x_1 + w_2x_2 + \dots + w_dx_d + b,\qquad h = \phi(z)
Each weight says how much its input matters, and in which direction; the bias shifts the threshold. Today's default bend is the ReLU, ϕ(z)=max⁡(0,z)\phi(z)=\max(0, z): positive totals pass through unchanged, negative ones become zero.
Predict before you look

A ReLU neuron has weights w1=2w_1 = 2, w2=−1w_2 = -1 and bias b=1b = 1. What does it output for the input (x1,x2)=(1,4)(x_1, x_2) = (1, 4)?

Show the answer

0

The total is z=2⋅1+(−1)⋅4+1=−1z = 2\cdot1 + (-1)\cdot4 + 1 = -1. ReLU replaces negatives by zero, so the output is 00: the neuron stays silent. If you answered −1-1, you computed zz but forgot the bend, and the bend is the part that makes networks interesting.

A layer is many neurons reading the same inputs, each with its own weights. A layer of 100 neurons that reads a 28 × 28-pixel image (784 numbers) holds 100 rows of 784 weights: a table of numbers, a matrix WW. The whole layer is one matrix multiplication, plus a list of biases, with the bend applied to every entry:

h=ϕ(Wx+b).\mathbf h = \phi(W\mathbf x + \mathbf b).

That is why deep learning runs on chips built to multiply matrices quickly.

Predict before you look

How many parameters (weights plus biases) does that layer have: 100 neurons, each reading all 784 pixels?

Show the answer

78500 parameters

Each neuron has 784 weights and 1 bias: 100×(784+1)=78,500100\times(784+1) = 78{,}500. One modest layer already has tens of thousands of knobs, and large networks have billions. No one could set them by hand; they have to be learned.

Which bend? The classic choices were the S-shaped sigmoid and tanh; modern networks mostly use ReLU and smooth cousins of it. The choice matters for learning. Training adjusts each weight using the slope of the bend, and where a curve is flat its slope is nearly zero, so the neuron barely learns. This is called saturation.

Depth needs a bend

Why not skip the bend and keep only weighted sums? Because a weighted sum of weighted sums is just another weighted sum. In one dimension: if layer 1 computes 3x+13x + 1 and layer 2 doubles that and adds 5, the pair computes 2(3x+1)+5=6x+72(3x+1)+5 = 6x + 7, which one layer could do alone. A hundred such layers still collapse into one, and the boundary they draw between two classes is still a straight line (a flat plane, with more inputs).

Check your understanding

A two-layer network with no activation functions computes h=2x−1h = 2x - 1 and then y=−3h+4y = -3h + 4. Which single layer computes exactly the same function?

  1. y=−x+3y = -x + 3
  2. y=−6x+3y = -6x + 3
  3. y=−6x+7y = -6x + 7
  4. y=6x+7y = 6x + 7
Show the answer

C. y=−6x+7y = -6x + 7

Substitute: y=−3(2x−1)+4=−6x+3+4=−6x+7y = -3(2x-1) + 4 = -6x + 3 + 4 = -6x + 7. Composing straight lines gives a straight line, with the slopes multiplied. That is why stacked linear layers are no more powerful than one.

With a bend between layers, composition becomes powerful. Each neuron of the first hidden layer splits the plane along one soft line. The next layer combines those half-planes into regions, and later layers combine regions into shapes as intricate as the data require. Try it on a spiral, hopeless for any straight line.

The recipe: data, architecture, loss, gradients

Every system in this course, from a digit reader to a chat assistant, is built from the same five ingredients:

  1. Data: examples (x,y)(\mathbf x, y), split into a training set the network learns from, a validation set for choices such as when to stop, and a test set used once, at the end.
  2. Architecture: the family of functions fθf_{\boldsymbol\theta}, fixed by the choice of layers. The parameters θ\boldsymbol\theta (all the weights and biases) pick one member of the family.
  3. Loss: a single number that measures how wrong the predictions are, such as the average squared error or the cross-entropy.
  4. Optimization: gradient descent, which nudges every parameter a little in the direction that lowers the loss. That direction is minus the gradient, computed by backpropagation.
  5. Evaluation: the loss or accuracy on data the network never trained on, because what counts is generalization to new examples.
text
repeat for many epochs:                        # one epoch = one pass over the training set
    shuffle the training set
    for each mini-batch (x, y) of, say, 64 examples:
        prediction ← f(x; θ)                     # forward pass
        loss ← average error of prediction vs y
        g ← gradient of loss with respect to θ    # backward pass (backpropagation)
        θ ← θ − η · g                            # step downhill, learning rate η
    measure the loss on the validation set; keep the best θ so far

The training loop in words. Training a Network in Practice turns it into a dozen lines of PyTorch.

The learning rate η\eta sets the step size. Each gradient is computed on a small random mini-batch rather than the whole training set, which makes every step cheap and a little noisy: stochastic gradient descent. Backpropagation applies the chain rule backward through the layers and delivers the gradient for every parameter at about twice the cost of the forward pass, whether the network has a thousand parameters or a hundred billion.

Check your understanding

Your network's training loss keeps falling, but its validation loss started rising ten epochs ago. What is the most likely explanation?

  1. The learning rate is too small
  2. The network is too small to fit the training data
  3. It is overfitting: fitting quirks of the training examples that do not carry over to new ones
  4. The validation set must be mislabeled
Show the answer

C. It is overfitting: fitting quirks of the training examples that do not carry over to new ones

A falling training loss with a rising validation loss is the signature of overfitting: the network is now learning the particular training examples rather than the pattern behind them. The usual responses are to stop at the best validation epoch, add data or augmentation, or regularize (Regularization & Generalization).

Features, layer by layer

What do the hidden layers of a trained network compute? In image networks we can look. The first layer's filters respond to edges at various orientations and to blobs of color: Krizhevsky et al. (2012) show all 96 that their network learned. Zeiler and Fergus (2014) traced the layers above: the second responds to corners and other edge–color combinations, the third to textures, the fourth to class-specific parts such as dog faces and bird legs, and the fifth to whole objects in varied poses.

Each layer builds on the one below: a hierarchy of features, from simple and local to complex and abstract, that nobody designed.

Why 2012?

Neural networks are old. Rosenblatt's perceptron dates from 1958, and backpropagation was popularized by Rumelhart, Hinton and Williams in 1986. By the late 1990s convolutional networks were reading several million bank checks a day (LeCun et al., 1998). Yet around 2010 most researchers preferred other methods, because deep networks were hard to train and big labeled data sets were rare. Four things changed:

  • Data. ImageNet (Deng et al., 2009) offered over a million labeled training images in a thousand categories for a yearly competition.
  • Compute. Graphics processors (GPUs), built for video games, multiply matrices massively in parallel. AlexNet trained for five to six days on two gaming GPUs.
  • Training fixes. ReLU activations, which do not saturate for positive inputs (Nair & Hinton, 2010; Glorot, Bordes & Bengio, 2011); careful initialization (Glorot & Bengio, 2010); dropout against overfitting; and soon batch normalization (2015), the Adam optimizer (2015) and residual connections (2016).
  • Software. Libraries that compute gradients automatically (Theano, then TensorFlow and PyTorch), so trying a new architecture takes an afternoon instead of weeks of hand-derived calculus.

In 2012 Krizhevsky, Sutskever and Hinton's AlexNet, a convolutional network with 60 million parameters, won the ImageNet challenge with a top-5 error of 15.3%, against 26.2% for the runner-up. (Top-5 error: the true label is not among the network's five best guesses.) The same year, deep networks cut error rates in speech recognition (Hinton et al., 2012). A decade of milestones followed:

YearMilestoneSource
2015A residual network 152 layers deep wins the ImageNet challengeHe et al. (2016)
2016AlphaGo, deep networks guiding a tree search, beats the Go champion Lee Sedol 4–1 in Marchthe system: Silver et al. (2016)
2016Google Translate begins switching to neural machine translation, with far fewer errors than its phrase-based systemWu et al. (2016)
2017The transformer, built on attention, for translationVaswani et al. (2017)
2020GPT-3, a language model with 175 billion parameters, does new tasks from a few examples in its promptBrown et al. (2020)
2020–21Diffusion models generate images by learning to undo noise; AlphaFold 2 predicts protein structuresHo, Jain & Abbeel (2020); Jumper et al. (2021)
Residual networks, translation models, transformers, language models and diffusion models each get lessons later in the course.
Think first AlexNet's core ideas (convolutions, backpropagation, gradient descent) were all at least twenty years old in 2012. So what was new?

Mostly scale, and the engineering that makes scale work: 1.2 million labeled images instead of thousands, GPU code that trained a 60-million-parameter network in days, and ReLU and dropout to make such a big network train quickly without overfitting. Much of the progress since has had the same shape: known ideas, made to work at a larger scale.

What it still gets wrong, and the road ahead

Deep learning is not a solved problem. As of the mid-2020s its best-known weaknesses are:

  • Data hunger. Supervised networks need many labeled examples. Pretraining on unlabeled data (self-supervised learning) eases this, but does not remove it.
  • Distribution shift. Accuracy can drop sharply on data unlike the training data: a new hospital's scanner, a new camera, a new dialect.
  • Adversarial examples. Changes to an image too small for a person to notice can flip a network's prediction (Szegedy et al., 2014).
  • Shortcuts. A network uses any pattern that predicts the label in its training data, including accidental ones such as a background or a watermark, which fail elsewhere.
  • Opacity. A trained network is millions of numbers, and explaining why it produced an output is an open research field (interpretability).
  • Cost. Training the largest models takes vast amounts of compute and energy, and running them is not cheap either.

In the Representation lessons of this topic you take each ingredient apart:

  • Neurons, Layers & Activations: the multilayer network as matrix algebra, parameter counts, and the full zoo of activation functions.
  • Loss Functions & Maximum Likelihood: where squared error and cross-entropy come from, and how to choose a loss for any kind of output.
  • What Networks Can Represent: universal approximation, width versus depth, and why representing a function is not the same as learning it.
  • Training a Network in Practice: tensors, mini-batches, the PyTorch training loop and how to read a loss curve.

The other nine topics follow the recipe. Backpropagation & Gradient Flow and Optimizing Deep Networks cover the gradient and what to do with it; Regularization & Generalization asks why networks that can memorize anything still predict well. Convolutional Networks & Vision, Recurrent Networks & Sequence Models and Transformers build structure into the architecture. Deep Generative Models create images and text, Learning Representations learns features without labels and looks inside networks, and Deep Learning at Scale covers scaling laws and the engineering of giant models.

Key takeaways

  • Deep learning fits a composition of many simple, parameterized layers to data; the intermediate layers compute features that nobody designed.
  • A neuron computes a weighted sum plus a bias and applies a bend such as ReLU; a layer is a matrix multiplication followed by the bend, and even modest layers have tens of thousands of parameters.
  • Without nonlinear activations any stack of layers collapses into one linear map; with them, depth composes simple pieces into intricate functions.
  • Every system follows one recipe: data, an architecture, a loss, gradient descent with backpropagation, and evaluation on held-out data.
  • Deep learning took off around 2012 through big labeled data, GPUs, training fixes and automatic differentiation; it still struggles with distribution shift, adversarial inputs, shortcuts, opacity and cost.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

A ReLU neuron has weights w1=1w_1 = 1, w2=−2w_2 = -2 and bias b=3b = 3. What does it output for the inputs (2,1)(2, 1) and (0,3)(0, 3)?

  1. 33 and −3-3
  2. 00 and 33
  3. 33 and 00
  4. 55 and 00
Show the answer

C. 33 and 00

For (2,1)(2,1): z=2−2+3=3z = 2 - 2 + 3 = 3, and ReLU passes it: 33. For (0,3)(0,3): z=0−6+3=−3z = 0 - 6 + 3 = -3, and ReLU outputs 00.

Question 2

Which change turns a deep network into something no more expressive than a single linear layer?

  1. Removing all the biases
  2. Replacing every activation function by the identity, ϕ(z)=z\phi(z) = z
  3. Halving the number of neurons in every layer
  4. Replacing every ReLU by tanh
Show the answer

B. Replacing every activation function by the identity, ϕ(z)=z\phi(z) = z

With ϕ(z)=z\phi(z) = z every layer is a weighted sum, and a composition of weighted sums is one weighted sum: the whole stack collapses to a single linear map.

Question 3

What most distinguishes deep learning from a classical pipeline of hand-designed features plus a trained linear classifier?

  1. Deep learning does not need training data
  2. Deep learning uses a loss function, and classical methods do not
  3. Deep learning only works on images
  4. Deep learning trains the feature-computing layers and the classifier together, starting from raw input
Show the answer

D. Deep learning trains the feature-computing layers and the classifier together, starting from raw input

In a deep network the layers that compute features are trained by the same gradient descent as the final classifier, so the features are learned from data instead of designed.

Question 4

A network reaches 99.8% accuracy on its training set. What is the best evidence that it will work on new data?

  1. Its accuracy on a held-out test set it never trained on
  2. Its training loss is still decreasing
  3. It has more parameters than there are training examples
  4. It reached 99.8% after only a few epochs
Show the answer

A. Its accuracy on a held-out test set it never trained on

Only data the network never trained on can show whether it learned the pattern rather than the examples. Training accuracy can be near perfect even for a network that has memorized random labels.

Question 5

AlexNet (2012) used convolutions and backpropagation, ideas from the 1980s. What made it succeed?

  1. A new learning algorithm that replaced backpropagation
  2. Far more labeled data, training on GPUs, and fixes such as ReLU and dropout
  3. It was the first neural network with more than one layer
  4. Hand-designed edge detectors in its first layer
Show the answer

B. Far more labeled data, training on GPUs, and fixes such as ReLU and dropout

AlexNet was trained by backpropagation and gradient descent like its predecessors. What was new was the scale (1.2 million labeled ImageNet images, 60 million parameters, two GPUs) and the tricks that made that scale trainable.

Go deeper

Further reading

  • Y. LeCun, Y. Bengio & G. Hinton, "Deep learning," Nature 521, 436–444 (2015).
  • I. Goodfellow, Y. Bengio & A. Courville, Deep Learning, ch. 1 (MIT Press, 2016), free at deeplearningbook.org.
  • A. Krizhevsky, I. Sutskever & G. E. Hinton, "ImageNet classification with deep convolutional neural networks," NeurIPS (2012).
  • M. D. Zeiler & R. Fergus, "Visualizing and understanding convolutional networks," ECCV (2014).