v9.1.1
Menu

Chapter 5: Deep Learning

Week 5-6

Neural network fundamentals, backpropagation, CNNs, and modern architectures that power computer vision.

Topics
4
Demo
1
Videos
3
Min read
33

Chapter Overview

Deep learning has revolutionized computer vision, achieving superhuman performance on tasks that once seemed impossible. But neural networks aren't magic—they're built on simple mathematical principles.

What is this chapter about? We demystify deep learning by building up from the basics: neurons, layers, and how networks learn through backpropagation. Then we explore Convolutional Neural Networks (CNNs)—the architecture specifically designed for images.

Why does this matter? Nearly every state-of-the-art computer vision system uses deep learning:

  • Image classification (ResNet, EfficientNet)
  • Object detection (YOLO, Faster R-CNN)
  • Semantic segmentation (U-Net, DeepLab)
  • Image generation (GANs, Diffusion models)

How the topics connect: We start with neural network fundamentals—understanding what neurons compute and how layers compose. Then backpropagation explains how networks learn from data. CNNs introduce convolution layers that exploit image structure. Finally, we survey modern architectures that achieve state-of-the-art results.

Chapter Roadmap

Click any topic to jump in

Explainer videoInteractive demo
1
Neural Network Fundamentals

Neurons, activation functions, and loss functions — the atomic units that compose every deep learning model.

Artificial NeuronActivation FunctionsUniversal ApproximationLoss Functions
2
Backpropagation

The chain rule applied to computational graphs — how networks learn by propagating error signals backward through layers.

Chain RuleGradient Descent UpdateVanishing/Exploding GradientsComputational Graph
Spatial weight sharing for images
3
Convolutional Neural Networks

Convolutions, pooling, and receptive fields — the architecture designed to exploit the spatial structure of images.

Convolution OperationPoolingCNN ArchitectureReceptive Field
Scaling depth and capacity
4
Modern Architectures

ResNets, transformers, batch normalization, and dropout — the innovations that enable training very deep networks at scale.

Residual Learning (ResNet)Self-Attention (Transformers)Batch NormalizationDropout

Topics

4 topics in this chapter

The previous chapter fit models by writing down the equations ourselves — a line, a homography, a total-variation energy — and solving for their parameters. But most vision problems have no equation we can write: nothing tells us the precise function that maps a grid of pixels to the label "cat." We need a model flexible enough to represent almost any mapping and then learn it from examples instead of deriving it. That is what a neural network provides, and this topic builds it from its smallest part. The building block is the artificial neuron: it takes a weighted sum of its inputs, adds a bias, and passes the result through a non-linear activation function. One neuron alone draws a single decision boundary, but stack many into layers and compose them, and the network can approximate essentially any continuous function. This topic covers the neuron, the activation functions that supply the crucial non-linearity, the universal-approximation guarantee that justifies the whole enterprise, and the loss functions that measure how wrong the network is — the quantity the next topic teaches us to minimize.

Definition

An artificial neuron computes a weighted sum of its inputs plus a bias, then applies a non-linear activation function. A neural network stacks layers of such neurons so their compositions approximate complex functions, with a loss function measuring prediction error and activations supplying the non-linearity that makes depth more expressive than a single linear map.

In this topic

  1. 1Artificial Neuron
  2. 2Activation Functions
  3. 3Universal Approximation
  4. 4Loss Functions
1 of 4
Artificial Neuron

y=σ(∑i=1nwixi+b)=σ(wTx+b)y = \sigma\left(\sum_{i=1}^{n} w_i x_i + b\right) = \sigma(\mathbf{w}^T\mathbf{x} + b)

An artificial neuron (perceptron) computes a weighted sum of inputs, adds a bias, and applies a non-linear activation function σ\sigma:

  • xix_i: Input values from previous layer
  • wiw_i: Learnable weights (connection strengths)
  • bb: Bias term (shifts the activation threshold)
  • σ\sigma: Activation function (introduces non-linearity)

Geometric Interpretation: The weighted sum wTx+b=0\mathbf{w}^T\mathbf{x} + b = 0 defines a hyperplane in input space. The neuron classifies inputs based on which side of this hyperplane they fall on.

Why Non-linearity is Essential: Without non-linear activations, stacking layers would be equivalent to a single linear transformation (W2(W1x)=(W2W1)xW_2(W_1 x) = (W_2 W_1)x). Activations enable learning complex, non-linear decision boundaries.

Mathematical Intuition

A single neuron computes the affine map z=wTx+bz = \mathbf{w}^T\mathbf{x} + b followed by a pointwise nonlinearity σ\sigma. The weight vector w∈Rn\mathbf{w} \in \mathbb{R}^n defines a hyperplane wTx+b=0\mathbf{w}^T\mathbf{x} + b = 0 that partitions the input space into two half-spaces. The bias bb shifts this hyperplane away from the origin. A single neuron with a step activation is equivalent to a linear classifier — it can only separate linearly separable classes. Stacking neurons in layers composes these half-space decisions, and the universal approximation theorem guarantees that with enough neurons in one hidden layer, any continuous function on a compact set can be approximated to within ϵ\epsilon for any ϵ>0\epsilon > 0.

Example:

A neuron with weights [0.5, -0.3], bias 0.1, and ReLU activation receives input [2, 1]. What's the output?

2 of 4
Activation Functions

ReLU(x)=max⁡(0,x)σ(x)=11+e−x\text{ReLU}(x) = \max(0, x) \quad \sigma(x) = \frac{1}{1+e^{-x}}

Activation functions introduce non-linearity, enabling neural networks to learn complex mappings:

  • ReLU (Rectified Linear Unit): Simple, fast, avoids vanishing gradients for positive values. Most popular for hidden layers. Variant: Leaky ReLU allows small gradient for negative inputs.
  • Sigmoid: Outputs in (0,1)(0,1), good for probabilities. Can suffer from vanishing gradients. Derivative: σ′(x)=σ(x)(1−σ(x))\sigma'(x) = \sigma(x)(1-\sigma(x)).
  • Tanh: Outputs in (−1,1)(-1,1), zero-centered. Derivative: tanh⁡′(x)=1−tanh⁡2(x)\tanh'(x) = 1 - \tanh^2(x).
  • GELU (Gaussian Error Linear Unit): Used in transformers. Smoother than ReLU.
  • Softmax: Converts outputs to probability distribution: pi=ezi/∑jezjp_i = e^{z_i}/\sum_j e^{z_j}.
Mathematical Intuition

ReLU is defined as σ(z)=max⁡(0,z)\sigma(z) = \max(0, z), a piecewise-linear function with derivative σ′(z)=1[z>0]\sigma'(z) = \mathbf{1}[z > 0]. Its key property is that gradients are either 0 or 1 — no exponential decay across layers. Sigmoid σ(z)=1/(1+e−z)\sigma(z) = 1/(1+e^{-z}) has maximum derivative σ′(0)=0.25\sigma'(0) = 0.25, so chaining LL layers multiplies gradients by at most 0.25L0.25^L — vanishing exponentially. Softmax maps z∈RK\mathbf{z} \in \mathbb{R}^K to the probability simplex ΔK−1\Delta^{K-1} via pi=ezi/∑jezjp_i = e^{z_i}/\sum_j e^{z_j}. Its Jacobian is ∂pi/∂zj=pi(δij−pj)\partial p_i/\partial z_j = p_i(\delta_{ij} - p_j), connecting it directly to the cross-entropy gradient y^−y\hat{\mathbf{y}} - \mathbf{y}.

Example:

For z = 2, compute ReLU(z), sigmoid(z), and tanh(z).

3 of 4
Universal Approximation

f(x)≈∑i=1Nviσ(wiTx+bi)f(x) \approx \sum_{i=1}^{N} v_i \sigma(w_i^T x + b_i)

The Universal Approximation Theorem states that a neural network with a single hidden layer can approximate any continuous function to arbitrary accuracy, given enough neurons.

Key insight: Neural networks are function approximators. Training finds the weights that make the network approximate the target function (mapping inputs to outputs).

Why Depth Matters: While a single wide layer suffices in theory, deep networks learn hierarchical features more efficiently. They can represent some functions exponentially more compactly than shallow networks.

Caveat: The theorem guarantees existence but says nothing about:

  • How many neurons are needed
  • Whether gradient descent will find the optimal weights
  • Generalization to unseen data
Mathematical Intuition

The theorem (Cybenko 1989, Hornik 1991) states: for any continuous f:[0,1]n→Rf: [0,1]^n \to \mathbb{R} and any ϵ>0\epsilon > 0, there exists NN and parameters such that ∥f(x)−∑i=1Nviσ(wiTx+bi)∥<ϵ\|f(\mathbf{x}) - \sum_{i=1}^N v_i \sigma(\mathbf{w}_i^T\mathbf{x} + b_i)\| < \epsilon. The proof uses the Stone-Weierstrass theorem for sigmoidal activations and density arguments for ReLU. The catch is that NN may grow exponentially with dimension — the curse of dimensionality. Depth helps: functions requiring O(2n)O(2^n) neurons in a shallow network can sometimes be represented with O(n)O(n) neurons in a deep network, because composition creates exponentially many linear regions. A ReLU network with LL layers and ww neurons per layer partitions input space into at most O((w/n)nL)O((w/n)^{nL}) linear regions.

Example:

How can a network with ReLU activations approximate the function f(x) = |x|?

4 of 4
Loss Functions

LMSE=1n∑i(yi−y^i)2LCE=−∑iyilog⁡(y^i)\mathcal{L}_{MSE} = \frac{1}{n}\sum_i(y_i - \hat{y}_i)^2 \quad \mathcal{L}_{CE} = -\sum_i y_i \log(\hat{y}_i)

Loss functions measure how wrong the network's predictions are:

  • MSE (Mean Squared Error): For regression. Penalizes large errors heavily. Gradient: ∂L/∂y^=2(y^−y)\partial L/\partial \hat{y} = 2(\hat{y} - y).
  • Cross-Entropy: For classification. Measures KL divergence between predicted and true distributions.
  • Binary Cross-Entropy: −[ylog⁡(p)+(1−y)log⁡(1−p)]-[y\log(p) + (1-y)\log(1-p)] for binary classification.
  • Huber Loss: Less sensitive to outliers than MSE.

Gradient Behavior: Cross-entropy has better gradient properties than MSE for classification—when predictions are very wrong, gradients are larger, enabling faster learning.

Mathematical Intuition

MSE loss L=1n∑(yi−y^i)2\mathcal{L} = \frac{1}{n}\sum(y_i - \hat{y}_i)^2 is the negative log-likelihood under a Gaussian noise model y=f(x)+ϵy = f(x) + \epsilon where ϵ∼N(0,σ2)\epsilon \sim \mathcal{N}(0, \sigma^2). Cross-entropy L=−∑yklog⁡y^k\mathcal{L} = -\sum y_k \log \hat{y}_k is the negative log-likelihood under a categorical distribution — it equals the KL divergence DKL(y∥y^)D_{KL}(y \| \hat{y}) plus the entropy H(y)H(y), which is constant during optimization. The gradient of cross-entropy with softmax simplifies to ∇zkL=y^k−yk\nabla_{z_k} \mathcal{L} = \hat{y}_k - y_k, which is bounded and well-conditioned. MSE with sigmoid outputs has gradient ∝σ′(z)\propto \sigma'(z) which vanishes when predictions are confident, making training slow.

Example:

True label is cat (class 1). Model predicts [0.7, 0.2, 0.1] for [cat, dog, bird]. Compute cross-entropy loss.

Theory Exercise

Problem:

A neuron has inputs x = [1, 2], weights w = [0.5, -0.3], and bias b = 0.1. Calculate the output using (a) linear activation, (b) ReLU, and (c) sigmoid.

Hints:
  • First compute the weighted sum: z = w·x + b
  • z = 0.5×1 + (-0.3)×2 + 0.1
  • Apply each activation function to z