Training Deep Learning Models
Source: CS1674 Ch.11–12. Training = minimizing a loss over parameters via gradient descent (and its variants), computing gradients with the chain rule (backprop), and controlling overfitting with regularization, learning-rate decay, and early stopping. (Chapters 11 and 12 merged.)
1. Optimization: Convex vs. Non-Convex
- A function
is convex if for any and : The line segment between any two points lies above/on the curve. - For convex
, any local minimum is a global minimum. With 1-D , solve by and check . - Neural network loss is non-convex (many local minima, saddle points) → we use gradient descent, the "golden rule" of non-convex optimization.
2. Gradient Descent
Imagine a ball on the curve; it rolls downhill following the negative gradient (the negative slope of the tangent).
Update rule:
= learning rate (step size). - Why it decreases the function:
.
Learning-rate sanity
- Too large: overshoots, may diverge / oscillate.
- Too small: extremely slow convergence.
3. Full-Batch vs. Stochastic Gradient Descent
The objective averages over all samples:
| Full-Batch (FBGD) | SGD (mini-batch) | |
|---|---|---|
| Gradient | True gradient over all | Approximate, over a subset |
| Update | Accurate, stable | Efficient, may oscillate |
| Cost | High memory / slow | Low memory / fast |
| Sensitivity | Low | Sensitive to |
SGD instability sources: mini-batch may not represent the dataset; estimated gradient deviates from true → oscillation on steep/irregular landscapes. Hyper-parameters (LR, batch size, init) matter more than for FBGD.
4. Forward and Backward Propagation
A multi-layer network composes layer functions:
To train, we need
4.1 The chain rule
For
Worked example (
4.2 General backprop (chain rule over layers)
Interpret the factors as:
- Upstream gradient
(already computed, flowing backward). - Local gradient
and (from the current layer).
Backprop summary:
- Forward pass: compute activations
and cache them. - Backward pass: propagate the upstream gradient, multiply by local gradients, to get
and . - Update parameters.
See: "Yes you should understand backprop"; matrix calculus at explained.ai.
5. Optimizers
The optimization landscape for images/video is high-dimensional and complex (trenches, saddle points). History of gradients helps the current step.
5.1 Momentum
= moving average of gradient history; = momentum factor. → plain SGD. Increasing → more inertia, escapes local minima, reduces oscillations along steep directions.
5.2 RMSProp
= moving average of squared gradients; adapts the learning rate per parameter. - As we approach a minimum we want smaller steps → dividing by
does that. → no adaptation.
5.3 Adam (Momentum + RMSProp)
= first moment (momentum), = second moment (RMSProp-style). - Defaults:
, , , . - Bias correction
fixes the zero-init bias early in training (otherwise moments are underestimated). - Adam adapts LR like RMSProp and smooths gradients with momentum → robust, widely used.
| Optimizer | Uses | Adapts |
|---|---|---|
| SGD | current gradient | nothing |
| Momentum | gradient history (direction) | direction |
| RMSProp | squared-gradient history | learning rate |
| Adam | both moments | direction + LR |
6. Regularization
Minimize data loss plus a penalty:
| Regularizer | Gradient effect | |
|---|---|---|
| L2 (weight decay) | ||
| L1 |
- L2 → weight decay: shrinks weights → small data fluctuations → less variance → less overfitting, more stable.
- L1 → drives many weights exactly to 0 (sparsity); L2 is smoother/more stable.
7. Learning Rate Decay (Extreme Training)
For hard problems, first aim for tiny training loss (extreme training) to validate model capacity before worrying about test performance. Decay schedules:
- Exponential:
- Inverse / inverse-sqrt:
or - Linear:
- Cosine:
- Step decay: multiply by 0.9 every 100 epochs, etc.
- Manual / manual-with-validation: reduce when train/val error stalls.
- Warmup (Goyal et al. 2018): start small, increase, then decay.
Diagnosing learning curves
| Symptom | Cause / fix |
|---|---|
| Loss not decreasing | Underfitting — fix model/capacity first |
| NaNs after some iters | Numerical instability |
| Weird cyclical pattern | Data not shuffled |
| Error increasing | Bug, or LR too large |
Rule: solve underfitting first (get low training loss); only then address overfitting.
8. Validation Set and Early Stopping
- Underfitting: large train and test loss.
- Overfitting: low train loss, large test loss.
- Split off a validation set (small subset of training data) to approximate test loss during training.
- Early stopping: monitor validation loss; stop when it stops improving (or when train/val gap grows). Stopping points:
- epochs without suitable validation reduction,
- no accuracy/precision/recall improvement,
- validation loss rising relative to training loss (definite overfitting).
9. Summary
- Non-convex loss → gradient descent; use SGD mini-batches for efficiency.
- Gradients via the chain rule / backprop (upstream × local).
- Better optimizers: Momentum → RMSProp → Adam (adapt direction and/or LR).
- Combat overfitting with L1/L2 regularization, LR decay, and early stopping on a validation set.