Skip to content

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 f(θ) is convex if for any θ1,θ2 and λ[0,1]:f(λθ1+(1λ)θ2)λf(θ1)+(1λ)f(θ2)The line segment between any two points lies above/on the curve.
  • For convex f, any local minimum is a global minimum. With 1-D θ, solve by dfdθ=0 and check d2fdθ2>0.
  • 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:

θθαdf(θ)dθ
  • α = learning rate (step size).
  • Why it decreases the function: ΔfΔθdfdθ=α(dfdθ)20.

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:

f(θ)=1ni=1nfi(θ),f=1ni=1ndfidθ
Full-Batch (FBGD)SGD (mini-batch)
GradientTrue gradient over all n samplesApproximate, over a subset nsub
UpdateAccurate, stableEfficient, may oscillate
CostHigh memory / slowLow memory / fast
SensitivityLowSensitive to α, batch size, init

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:

h1=f1(x,w1), h2=f2(h1,w2), , hK=fK(hK1,wK)

To train, we need Lwk for every layer's parameters; then wkwkαLwk.

4.1 The chain rule

For z=g(y), y=f(x):

dzdx=dzdydydx

Worked example (k=1, L=(y^y)2, y^=w1x):

Lw1=Ly^y^w1=2(y^y)x=2x(w1xy)

4.2 General backprop (chain rule over layers)

Lwk=LhKhKhK1hk+1hkhkwk

Interpret the factors as:

  • Upstream gradient Lhk+1 (already computed, flowing backward).
  • Local gradient hk+1hk and hkwk (from the current layer).

Backprop summary:

  • Forward pass: compute activations hk and cache them.
  • Backward pass: propagate the upstream gradient, multiply by local gradients, to get Lhk1 and Lwk.
  • 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

vt=γvt1+(1γ)θf(θt1),θt=θt1αvt
  • vt = moving average of gradient history; γ = momentum factor.
  • γ=0 → plain SGD. Increasing γ → more inertia, escapes local minima, reduces oscillations along steep directions.

5.2 RMSProp

st=βst1+(1β)(θf(θt1))2,θt=θt1αst+ϵθf(θt1)
  • st = moving average of squared gradients; adapts the learning rate per parameter.
  • As we approach a minimum we want smaller steps → dividing by st does that. β=0 → no adaptation.

5.3 Adam (Momentum + RMSProp)

mt=β1mt1+(1β1)θf(θt1)m^t=mt1β1tvt=β2vt1+(1β2)(θf(θt1))2v^t=vt1β2tθt=θt1αv^t+ϵm^t
  • mt = first moment (momentum), vt = second moment (RMSProp-style).
  • Defaults: β1=0.9, β2=0.999, ϵ108, m0=v0=0.
  • Bias correction m^t,v^t 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.
OptimizerUsesAdapts
SGDcurrent gradientnothing
Momentumgradient history (direction)direction
RMSPropsquared-gradient historylearning rate
Adamboth momentsdirection + LR

6. Regularization

Minimize data loss plus a penalty:

L(w)=1ni=1n(w,xi,yi)+λR(w)
RegularizerR(w)Gradient effect
L2 (weight decay)12|w|22wwααλw (smooth decay)
L1|w|1wwααλsgn(w) (constant-magnitude decay)
  • 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: ηt=η0ekt
  • Inverse / inverse-sqrt: ηt=η0/(1+kt) or η0/t
  • Linear: ηt=η0(1t/T)
  • Cosine: ηt=12η0(1+cos(πt/T))
  • 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

SymptomCause / fix
Loss not decreasingUnderfitting — fix model/capacity first
NaNs after some itersNumerical instability
Weird cyclical patternData not shuffled
Error increasingBug, 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.