Part 10: Learning from Data: Minimize Loss by Gradient Descent
10.1 Learning Function : Data and Weights
A neural network is a composition of layer functions:
where:
- is the input data
- are weight matrices (learnable parameters, collectively called )
- are bias vectors
- is the output (prediction)
ReLU (Rectified Linear Unit): The nonlinear activation function:
ReLU is a piecewise linear ramp function, which makes the entire network piecewise linear in the weights and inputs. This is crucial for training: the function is piecewise linear.
The Learning Function: maps weights and input to output . The goal is to find weights that minimize a loss function over the training data.
10.2 Counting Flat Pieces in the Graph of
Because ReLU creates folds (the "crease" where ), the piecewise linear function divides the input space into many linear regions.
Counting Formula: For ReLU folds in :
Recursive Formula:
Growth: The number of regions grows roughly as , showing that even modest networks can represent highly complex piecewise linear functions.
Example: For ReLU neurons in , the number of linear regions is approximately .
10.3 Minimizing the Loss: Stochastic Gradient Descent
Gradient Descent: Iteratively update weights in the direction of steepest descent:
where is the learning rate (step size) and is the gradient of the loss at .
Loss Functions:
- Square loss:
- Cross-entropy loss: (for classification)
Stochastic Gradient Descent (SGD): Instead of computing the gradient over the entire dataset (which is expensive), use one random sample:
where is a randomly chosen index. This is much faster per iteration, and the randomness helps escape local minima.
Minibatch SGD: Average the gradient over a small batch of random samples:
Typical batch sizes: 32, 64, 128, 256. Batches balance gradient accuracy with computational efficiency.
10.4 Slow Convergence with Zigzag: Add Momentum
Standard gradient descent can zigzag when the loss landscape is elongated (high condition number), leading to slow convergence.
Momentum (Heavy Ball Method): Add a fraction of the previous update:
The momentum term (typically ) smooths the trajectory, reducing oscillation and accelerating convergence.
ADAM (Adaptive Moment Estimation): Combines momentum with adaptive learning rates per parameter:
- Maintains exponentially decaying averages of past gradients and squared gradients
- Adapts the learning rate for each parameter individually
- Handles sparse gradients effectively
- Typically the default optimizer for deep learning
10.5 Convolutional Neural Nets: CNN in 1D and 2D
Convolutional Neural Networks use shared weights applied via sliding windows, making them shift-invariant (translation equivariant).
1D Convolution: A filter (kernel) slides over the input, computing dot products at each position:
2D Convolution (images): A small kernel (e.g., ) slides across the image, computing the same weighted sum at each patch:
Key properties:
- Weight sharing: same filter weights used at every position (dramatically reduces parameters)
- Locality: each output pixel only depends on a local neighborhood
- Hierarchy: early layers detect edges, later layers detect shapes and objects
Pooling Layers: Reduce spatial dimensions:
- Max-pooling: take the maximum value in each (or other size) block
- Average-pooling: take the average
Additional components:
- Softmax: converts outputs to probabilities
- Batch Normalization: rescales and recenters activations at each layer to maintain stable gradients
- Residual Networks (ResNet): skip connections allow training very deep networks by mitigating the vanishing gradient problem
10.6 Backpropagation: Chain Rule for
Backpropagation is the algorithm for computing gradients in neural networks. It is the chain rule applied efficiently in reverse mode.
Forward Pass: Compute and the loss .
Backward Pass (Reverse Mode): Compute gradients layer by layer from output back to input:
For each layer , the gradients with respect to and are computed via local derivatives:
Why automatic differentiation is efficient: The forward pass costs one function evaluation. The backward pass computes all partial derivatives in roughly the same time as one forward pass -- a vast improvement over forward passes for parameters via finite differences.
The key insight: never form the full Jacobian matrix (which would be enormous). Instead, propagate gradient information backward through the computational graph, storing and reusing intermediate results.