Chapter 2: Deep Learning
Deep learning is representation learning. A classical model is given features and learns a decision boundary over them; a deep network learns the features too. Early layers discover simple patterns such as edges or phonemes, and later layers compose them into complex concepts such as faces or sentences. That hierarchical representation is why deep networks succeed where hand-engineered features stall, and it is why this chapter is the hinge of AI Engineering: reinforcement learning, foundation models, and LLM systems in the chapters that follow all build on it.
Concepts from other sections (28)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- foundation model: a model pretrained on broad data at scale and then adapted to many downstream tasks. Read more: 5.3 Model landscape and lifecycle
- Computation graph: a graph with one node per value and an edge from each value to the values it was computed from. Read more: 12.1 Graphs, DAGs, and an iterative topological sort in Mathematics
- Chain rule: . Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP in Mathematics
- Regularization: penalizing model complexity to trade bias for lower variance. Read more: Chapter 1: Statistical Learning
- Learning-rate schedule: a function from the step number to the learning rate. Read more: 16.4 Learning-rate schedules (cosine, WSD, Noam) and gradient clipping in Mathematics
- inductive bias: the assumptions about data structure that an architecture builds in, such as locality for CNNs or a summarizing state for RNNs. Read more: Chapter 3: Neural Architectures and Their History
- DPO: minimizing over preference pairs, equivalent to fitting the KL-regularized reward objective. Read more: 18.2 Direct preference optimization and IPO
- Jacobian: the matrix of partial derivatives of a function . Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP in Mathematics
- Maximum-likelihood estimate (MLE): the distribution that maximizes the log-likelihood; for counts, the frequency . Read more: 13.3 MLE, Laplace, absolute discounting in Mathematics
- Gated recurrent unit (GRU): a recurrent cell with one state and two gates, . Read more: 9.3 GRU (torch gate order)
- JEPA: a non-generative self-supervised architecture that predicts the embedding of a masked target rather than reconstructing it. Read more: 5.3 Model landscape and lifecycle
- Likelihood: , the probability of the observed data as a function of the parameter. Read more: 13.10 Probability problem set, part b: joint distributions, covariance, MLE in Mathematics
- KL divergence: , the excess cost of predicting with ; nonnegative and asymmetric. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- Cross-entropy: , the average surprise when data from are predicted with . Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- Continuous function / continuity: is continuous at when , so the graph has no jump or hole there; many theorems of calculus, such as the intermediate value theorem, require it. Read more: Chapter 7: Calculus 1 in Mathematics
- Automatic differentiation / autodiff: computing exact derivatives of a program by applying the chain rule to each primitive operation it executes, in forward mode (Jacobian-vector products) or reverse mode (vector-Jacobian products); it is how neural networks get their gradients, with no step size and no symbolic formula. Read more: Chapter 14: Matrix Calculus and Autodiff in Mathematics
- GELU: , the input weighted by the standard normal CDF; its tanh approximation is a different function. Read more: 7.3 Activation Functions and Their Derivatives in Mathematics
- SiLU: , the activation in SwiGLU. Read more: 22.8 Elementwise kernels in C: RMSNorm, RoPE, SiLU-mul, embedding, add, argmax
- sigmoid: , mapping the real line into , with . Read more: 7.3 Activation Functions and Their Derivatives in Mathematics
- Cosine decay: half a cosine period from to over a fixed horizon . Read more: 16.4 Learning-rate schedules (cosine, WSD, Noam) and gradient clipping in Mathematics
- cell state: the LSTM’s additively updated memory, protected by forget, input, and output gates. Read more: Chapter 3: Neural Architectures and Their History
- Masked language modeling (MLM): training an encoder to recover the original tokens at corrupted positions. Read more: 12.2 BERT: the bidirectional encoder and masked language modeling
- Exponential moving average (EMA): the recursion , a weighted sum with geometric weights. Read more: 8.2 Series convergence, the EMA as a geometric series, and bias correction in Mathematics
- PPO: a policy gradient method that clips the new-to-old probability ratio to keep updates small. Read more: 4.4 World models, multiple agents, and RLHF
- translation equivariance: the property that shifting the input shifts the feature map correspondingly, produced by sharing filters across positions. Read more: Chapter 3: Neural Architectures and Their History
- Eigenvector / eigenvalue: a non-zero and scalar with . Read more: 9.4 Eigenvalues, power iteration, and the spectral radius in Mathematics
- Entropy: , the average surprise of a distribution. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- positional encoding: a vector depending only on position, added to the token embedding so attention can see order. Read more: 11.4 Positional encodings: sinusoidal, learned
No single algorithm is responsible. The success comes from a combination of three things: differentiable computation graphs, backpropagation to compute their gradients, and massive scale. Backpropagation in particular is less mysterious than it looks; it is the chain rule applied systematically, and most of what remains is engineering.
The chapter follows the arc of Goodfellow, Bengio, and Courville’s Deep Learning and then goes beyond it. It begins with a review of machine learning basics, then builds feedforward networks, the regularization and optimization methods that make them train and generalize, and the two classic architecture families, convolutional networks for spatial data and recurrent networks for sequences. After a section on practical methodology, it turns to generative modeling (autoencoders, GANs, diffusion, flows) and representation learning (transfer, self-supervision, contrastive and joint-embedding methods). It ends with the material no textbook yet covers fully: transformers, which have replaced RNNs and LSTMs for most sequence tasks and made attention the dominant paradigm; scaling laws, which suggest performance improves predictably with data, compute, and parameters; and alignment through human feedback.
Learning objectives
- Explain why depth helps, what the universal approximation theorem does and does not promise, and how backpropagation computes gradients efficiently.
- Choose and justify regularization and optimization methods (weight decay, dropout, batch normalization, SGD with momentum, Adam, learning-rate schedules).
- Describe the inductive bias of MLPs, CNNs, RNNs and LSTMs, and transformers, and the problem each was designed to solve.
- Derive the VAE objective (the ELBO) and contrast VAEs, GANs, diffusion models, and flows.
- Explain self-attention, scaling laws, RLHF, and DPO well enough to discuss them in an interview.
Before you begin
Section titled “Before you begin”This chapter assumes Statistical Learning, the previous chapter, for the bias-variance vocabulary. From Mathematics you need Linear Algebra, multivariable calculus through Calculus 3 (gradients, Jacobians, and the chain rule), and Probability and Statistics.
The primary text is Deep Learning by Goodfellow, Bengio, and Courville, free online; read it for theory. For hands-on implementation, use fast.ai, Stanford CS231n, or Andrej Karpathy’s Neural Networks: Zero to Hero, all free. Implement a simple neural network from scratch, forward pass and backpropagation, before you rely on a framework. For each architecture, make sure you can say what inductive bias it encodes and what problem it was designed for. And read the original papers for transformers, ResNet, and batch normalization.
In this chapter
Section titled “In this chapter”- Machine learning basics revisited: learning as optimization, capacity, MLE, and the manifold hypothesis.
- Deep feedforward networks: universal approximation, backpropagation, activations, and why depth matters.
- Regularization for deep networks: weight decay, dropout, batch normalization, augmentation, and early stopping.
- Optimization: SGD, momentum, Adam, schedules, batch size, and the shape of the loss landscape.
- Convolutional networks: convolution, pooling, and the path from LeNet to ResNet.
- Sequence models: RNNs, LSTMs, GRUs, and encoder-decoders.
- Practical methodology: baselines, debugging, tuning, and when more data helps.
- Autoencoders and Generative models: VAEs and the ELBO, GANs, diffusion, and flows.
- Representation learning: transfer, self-supervision, contrastive learning, and JEPA.
- Transformers and modern architectures and RLHF and alignment: the frontier beyond the textbook.
Machine learning basics revisited
Section titled “Machine learning basics revisited”Reading: Goodfellow Chapter 5.
The textbook starts by recasting the previous chapter in the language deep learning uses. A learning algorithm is an optimization procedure; capacity, overfitting, and underfitting describe how flexibility relates to generalization. Maximum likelihood estimation turns out to be the same as minimizing the KL divergence between the data distribution and the model’s distribution, which is why cross-entropy is the default loss. Bayesian estimation offers the alternative of keeping a distribution over parameters. Two ideas explain why deep learning is needed at all: the curse of dimensionality, which makes naive generalization in high-dimensional spaces hopeless, and the manifold hypothesis, the observation that real-world data lie near lower-dimensional manifolds that a model can learn.
Deep feedforward networks
Section titled “Deep feedforward networks”Reading: Goodfellow Chapter 6.
The basic deep model is a feedforward network: layers of affine transformations followed by non-linear activations. Its expressive power is guaranteed by the universal approximation theorem: a feedforward network with a single hidden layer of sufficient width can approximate any continuous function on a compact set. Read the caveat carefully, though. The theorem says nothing about whether gradient descent can find that network, or how wide it must be.
Depth is the better answer. Depth-separation results show that deeper networks can represent some functions exponentially more efficiently than shallow ones, which is the formal version of the hierarchical-features intuition. Choosing an architecture is a tradeoff between depth and width, and skip connections are what make very deep networks trainable in practice.
To train such a network you need gradients of the loss with respect to every parameter. Backpropagation computes them with reverse-mode automatic differentiation: one forward pass to compute and store activations, then one backward pass applying the chain rule layer by layer. The total cost is a small constant multiple of a forward pass regardless of how many parameters there are, which is what makes training networks with billions of parameters feasible.
The choice of activation matters too. ReLU is the default; GELU and SiLU (Swish) are common in modern architectures; sigmoid and tanh survive mainly as gates.
Regularization for deep networks
Section titled “Regularization for deep networks”Reading: Goodfellow Chapter 7.
Deep networks have enormous capacity, so they need regularization to generalize. Parameter-norm penalties carry over from the previous chapter: (weight decay) shrinks weights and promotes sparsity. Dropout randomly zeroes activations during training, which approximately averages over an ensemble of subnetworks that share weights. Batch normalization normalizes activations per mini-batch; it stabilizes training and also acts as a regularizer. Data augmentation applies domain-specific transformations that should not change the label, such as crops, flips, and color jitter for images. Early stopping watches validation loss and stops training when it stops improving, which is simple and effective.
Optimization
Section titled “Optimization”Reading: Goodfellow Chapter 8.
Regularization decides what you want to find; optimization decides whether you find it. Stochastic gradient descent (SGD) follows the gradient of a mini-batch, which is an unbiased estimate of the full gradient at a fraction of the cost. Momentum accumulates past gradients, which helps it move through saddle points and along narrow ravines in the loss surface. Adam combines momentum with RMSprop’s adaptive per-parameter learning rates, and it is the default choice for most tasks.
The learning rate is rarely constant. Warmup followed by cosine decay is standard for transformers, and step decay is common for CNNs. Batch size interacts with generalization as well: a widely reported observation is that larger batches tend to find sharper minima that generalize worse, while the noise of smaller batches acts as an implicit regularizer.
The reassuring insight of this section is that the loss landscape of deep networks has many saddle points but few bad local minima, so optimization in practice is easier than worst-case theory suggests.
Convolutional networks
Section titled “Convolutional networks”Reading: Goodfellow Chapter 9.
Convolutional networks encode an assumption about images: a pattern means the same thing wherever it appears. A convolution applies the same small filter at every position, combining parameter sharing with local connectivity, and the result is translation equivariant: shift the input and the feature map shifts with it. Pooling summarizes neighborhoods, which reduces spatial dimensions and provides approximate translation invariance.
The architecture history is a sequence of ideas: LeNet, AlexNet, VGG, GoogLeNet/Inception, ResNet, and EfficientNet. The most important is ResNet’s. Plain networks get worse as they get much deeper, even on training data (the degradation problem). A residual block instead computes
so the layers learn the residual rather than the full mapping . Learning a residual is easier, since doing nothing is just , and skip connections let gradients flow directly, enabling networks of 100+ layers.
Sequence models
Section titled “Sequence models”Reading: Goodfellow Chapter 10.
For sequences, the classical approach processes one element at a time while carrying a hidden state. A recurrent neural network updates
and because the same weights are applied at every step, gradients over long sequences are multiplied repeatedly by the same matrix and either vanish or explode. The LSTM adds a cell state controlled by input, forget, and output gates, which solves the vanishing-gradient problem for medium-length sequences. The GRU simplifies the LSTM to two gates with comparable performance and fewer parameters. Bidirectional variants process the sequence in both directions to capture future context, and encoder-decoder models compress an input sequence and decode it into an output sequence, the basis of neural machine translation.
RNNs and LSTMs dominated sequence modeling from 2014 to 2017; since 2018, transformers have largely replaced them, for reasons covered at the end of this chapter.
Practical methodology
Section titled “Practical methodology”Reading: Goodfellow Chapter 11.
Knowing the architectures is not enough; you also need a working method. Always start with a baseline, a simple model that establishes what “good enough” looks like. When debugging, first make the model overfit a single batch, then scale up; if it cannot overfit one batch, there is a bug. For hyperparameter tuning, random search beats grid search, and Bayesian optimization is worth it for expensive models. Finally, diagnose before collecting data: if training and test error are close and both high, more data (or more capacity) helps; if the gap between them is large, regularize first.
Autoencoders
Section titled “Autoencoders”Reading: Goodfellow Chapter 14.
So far the networks have predicted labels. Autoencoders learn representations by reconstructing their input. An undercomplete autoencoder forces data through a bottleneck, so it must learn a compact representation.
The variational autoencoder (VAE) makes this probabilistic. The encoder outputs distribution parameters , a latent code is sampled as , and the decoder reconstructs from . The objective combines a reconstruction loss with a KL-divergence penalty toward the prior, and together they form the evidence lower bound:
Because the ELBO is a lower bound on , maximizing it approximately maximizes the likelihood of the data. The KL term connects directly to Information Theory in Mathematics: it measures how far the encoder’s distribution is from the prior.
Generative models
Section titled “Generative models”Reading: Goodfellow Chapter 20, plus modern extensions.
VAEs are one family of generative models; three others matter. GANs pit a generator against a discriminator in an adversarial game; their characteristic failure is mode collapse, where the generator produces only a narrow range of outputs. Diffusion models gradually add noise to data and learn to reverse the process by denoising; DDPM and score-based models have dominated image generation since 2021. Flow models (normalizing flows) build the generator from invertible transformations, which gives a tractable exact likelihood.
Representation learning
Section titled “Representation learning”Reading: Goodfellow Chapter 15.
The theme of the chapter returns explicitly here: the representations a network learns are valuable in themselves. Transfer learning, pretraining on a large dataset and fine-tuning on a small target one, is the foundation of modern deep learning. Self-supervised learning creates labels from the data itself, as in masked language modeling or contrastive learning, and it is the paradigm that powers LLM pretraining; Foundation Models takes this up in its discussion of the LLM development lifecycle. Domain adaptation handles training on a source domain and deploying on a different target domain, where distribution shift is the challenge.
Many modern methods use Siamese or joint-embedding networks: two weight-sharing encoders map two views of the same datum to embeddings, and the loss pulls matching pairs together and pushes others apart. This is the backbone of contrastive learning (SimCLR, MoCo, CLIP) and of metric learning for verification and retrieval.
The core difficulty of joint-embedding methods is representation collapse: the trivial solution of mapping everything to the same embedding makes all pairs match. Each family avoids it differently. SimCLR uses negative pairs; BYOL and MoCo use a momentum target encoder; SimSiam uses a stop-gradient with a predictor; Barlow Twins and VICReg reduce redundancy between embedding dimensions.
JEPA (Joint-Embedding Predictive Architecture) is LeCun’s non-generative approach to self-supervision. Instead of predicting pixels or tokens, it predicts the representation of a masked target from its context, in embedding space. I-JEPA applies this to images and V-JEPA and V-JEPA 2 to video, as a step toward world models; an asymmetric context and target encoder with an EMA target avoids collapse.
Transformers and modern architectures
Section titled “Transformers and modern architectures”Beyond the textbook: key papers.
The transformer is built on self-attention:
in which every position attends to every other, at a cost of in sequence length. Unlike an RNN, it has no sequential bottleneck, so it trains in parallel and connects distant positions directly. Multi-head attention runs several attention heads in parallel so that they can capture different kinds of relationships. Because attention is permutation-equivariant, word order must be supplied by positional encodings, sinusoidal or learned.
Three transformer families cover most uses: encoder-only (BERT), decoder-only (GPT), and encoder-decoder (T5). Scale then changes the picture. Scaling laws show that loss falls as a power law in compute, data, and parameters (Kaplan et al.; Hoffmann et al.), and some capabilities, such as few-shot learning and chain-of-thought reasoning, have been reported to appear suddenly at particular scales, which are called emergent abilities.
The papers to read are “Attention Is All You Need” (Vaswani et al., 2017), “BERT” (Devlin et al., 2018), “Language Models are Few-Shot Learners” (GPT-3; Brown et al., 2020), “Scaling Laws for Neural Language Models” (Kaplan et al., 2020), and “Training Compute-Optimal Large Language Models” (Chinchilla; Hoffmann et al., 2022).
RLHF and alignment
Section titled “RLHF and alignment”Beyond the textbook: frontier research.
A pretrained language model predicts text; making it helpful and safe takes another stage. RLHF trains a reward model from human preference comparisons and then optimizes the policy against it with PPO. DPO (Direct Preference Optimization) skips the reward model and optimizes the policy directly from preference pairs. Constitutional AI has the model critique and revise its own outputs according to written principles, reducing the need for human feedback. Behind all of them is the problem of scaling supervision: as models become more capable than their evaluators, how do we evaluate them? Weak-to-strong generalization is one line of research on that question.
The papers are “Training language models to follow instructions with human feedback” (InstructGPT; Ouyang et al., 2022), “Direct Preference Optimization” (Rafailov et al., 2023), and “Constitutional AI” (Bai et al., 2022).
Comparing the architectures
Section titled “Comparing the architectures”The architectures in this chapter differ most clearly in the inductive bias they build in. The catalog below summarizes them:
| Architecture | Inductive Bias | Best For | Key Innovation |
|---|---|---|---|
| MLP | None | Tabular data | Universal approximation |
| CNN | Translation equivariance | Images, spatial data | Parameter sharing via convolution |
| RNN/LSTM | Sequential processing | (Legacy) time series | Gated memory |
| Transformer | No structural bias | Text, multimodal, general | Self-attention mechanism |
| VAE | Smooth latent space | Generation, compression | Variational inference |
| GAN | Adversarial training | Image generation | Min-max game |
| Diffusion | Gradual denoising | Image/video generation | Score matching |
Where this leads
Section titled “Where this leads”The mathematics behind this chapter runs through the rest of the book. Backpropagation is the chain rule from Calculus 3; eigenvalues and SVD from Linear Algebra appear in PCA, weight initialization, and spectral normalization; and KL divergence and entropy from Information Theory define the VAE objective and the cross-entropy loss. The bias-variance tradeoff of Statistical Learning extends into the double-descent behavior of large networks. The next chapter, Reinforcement Learning, supplies the algorithms behind RLHF, and Neural Network Architectures & the History of Deep Learning derives backpropagation, CNNs, and LSTMs from the math and codes them from scratch.
These topics are also central to interviews at AI-focused companies, as the table below shows:
| Company | How This Appears | Difficulty |
|---|---|---|
| Anthropic | Transformer internals, RLHF/DPO, alignment, interpretability | PhD-level |
| OpenAI | GPT architecture, scaling laws, RLHF, inference optimization | PhD-level |
| Google/DeepMind | Large-scale training, TPU optimization, multimodal, Gemini | PhD-level |
| NVIDIA | Inference optimization, TensorRT, quantization, hardware-aware design | Expert |
| Meta | Recommendation at scale, LLaMA, self-supervised learning | Expert |
| Netflix | Recommendation, personalization, embeddings | Advanced |
Further reading
Section titled “Further reading”- Goodfellow, Bengio, and Courville, Deep Learning (free online).
- fast.ai Practical Deep Learning and Stanford CS231n.
- Andrej Karpathy, Neural Networks: Zero to Hero.
- The original papers listed in the transformer and alignment sections above.
Key terms
Section titled “Key terms”- Representation learning: learning the features themselves, not just a decision rule over given features.
- Backpropagation: reverse-mode automatic differentiation that computes all parameter gradients in one backward pass.
- Dropout: randomly zeroing activations during training to approximate an ensemble of subnetworks.
- Residual connection: computing so that layers learn a residual and gradients flow directly.
- ELBO: the evidence lower bound, reconstruction likelihood minus KL to the prior, maximized to train a VAE.
- Representation collapse: the trivial solution in joint-embedding training where all inputs map to the same embedding.
- Self-attention: a layer in which each position computes a weighted average of all positions’ values, weighted by query-key similarity.
- Scaling laws: empirical power laws relating loss to compute, data, and parameter count.
Summary
Section titled “Summary”- Deep learning is representation learning: depth lets networks compose simple features into complex ones.
- Backpropagation is the chain rule applied systematically, and it costs about as much as a forward pass regardless of parameter count.
- Regularization (weight decay, dropout, batch normalization, augmentation, early stopping) and optimization (SGD, momentum, Adam, schedules) are what make deep networks train and generalize.
- Each architecture encodes an inductive bias: convolution for translation, recurrence for sequence, attention for flexible all-to-all interaction.
- Transformers have replaced RNNs for most sequence tasks, scaling laws make performance predictable with scale, and RLHF and DPO align models with human preferences.
Review questions
Section titled “Review questions”- What does the universal approximation theorem guarantee, and what does it not?
Answer
It guarantees that a single-hidden-layer network of sufficient width can approximate any continuous function on a compact set. It does not say how wide the network must be, whether gradient descent can find the right weights, or how well the result will generalize. Depth separation results are why deeper networks are preferred in practice. - Why does a residual block make very deep networks trainable?
Answer
With $y = F(x) + x$, the layers only need to learn the residual $F(x) = H(x) - x$; the identity is the easy default ($F = 0$), so adding layers cannot easily make the network worse. The skip connection also passes gradients directly backward, avoiding the degradation problem of plain deep networks. - Your model cannot overfit a single batch. What does that tell you?
Answer
That there is a bug, such as a wrong loss, labels misaligned with inputs, a broken gradient path, or a learning rate far off. A model with any reasonable capacity should be able to memorize one batch, so fix this before scaling up. - Write the ELBO and explain each term.
Answer
$\text{ELBO} = E_{q(z\mid x)}[\log p(x \mid z)] - D_{\mathrm{KL}}(q(z \mid x) \Vert p(z))$. The first term rewards accurate reconstruction of $x$ from sampled latents; the second penalizes the encoder's distribution for straying from the prior, keeping the latent space smooth. The ELBO lower-bounds $\log p(x)$, so maximizing it approximately maximizes likelihood. - Why do transformers need positional encodings, and why is attention ?
Answer
Self-attention is permutation-equivariant: shuffling the inputs just shuffles the outputs, so without positional information the model cannot tell word order. It is $O(n^2)$ because every one of $n$ positions computes a score against every other position. - How does DPO differ from RLHF with PPO?
Answer
RLHF first trains a separate reward model on human preferences and then optimizes the policy against it with PPO. DPO skips the reward model and the RL loop, optimizing the policy directly on preference pairs with a classification-style loss. - Name three ways joint-embedding methods avoid representation collapse.
Answer
Negative pairs (SimCLR), a momentum target encoder (BYOL, MoCo), a stop-gradient with a predictor (SimSiam), or redundancy reduction across embedding dimensions (Barlow Twins, VICReg). JEPA uses an asymmetric context/target encoder with an EMA target.