Skip to content

Lecture 3: Optimization in Deep Learning ​

GD vs. SGD ​

The three variants differ only in how many samples one update uses.

SGDMini-batch GDBatch GD
Samples per iteration1N/m (a mini-batch)all N
Path to minimumnoisy, zig-zagmostly smoothsmooth
Parallel on GPUhardyesmaximum
Main drawbackoscillates near the minimum—slow per step, may not fit in memory

In practice

Mini-batch GD is the default. It is fast (parallel) and stable enough. "SGD" in libraries usually means mini-batch GD.

With SGD the loss curve is noisy, but it still converges, and the noise can even help it find a shortcut.

Learning Rate ​

  • Too small → very slow training.
  • Too large → loss jumps around or diverges.
  • Variable learning rate: start large, then shrink it as training gets close to the minimum.

Optimizers ​

OptimizerIdea in one line
SGDfixed step α along the gradient
Momentumkeep a running average of gradients, like a ball rolling downhill → faster, less zig-zag
AdaGrada separate learning rate for each weight, shrinks for weights that got big gradients
RMSPropAdaGrad with a moving average, so the learning rate does not shrink to zero
AdamMomentum + RMSProp. Standard choice in deep learning

Momentum and Adam updates (for reference):

v←βv+(1−β)∇w,w←w−αvAdam: w←w−αv^s^+ϵ,v=avg of ∇w,s=avg of (∇w)2

Optimizer vs. optimization techniques

The optimizer is just the update rule. Optimization techniques are everything else that helps training: data splitting, regularization, normalization, batch norm, initialization, hyperparameter tuning.

Train / Validation / Test ​

SetUsed forWhen
Traincomputing the loss and updating weightsduring training
Validationchecking progress, choosing hyperparametersduring training
Testfinal, honest scoreonly once, after training

Common ratios: 70/15/15, 80/10/10, 60/20/20.

Underfitting vs. Overfitting ​

UnderfittingGood fitOverfitting
Train accuracybadgoodgreat
Test accuracybadgoodbad
Problemhigh bias (model too simple)—high variance (memorized the training set)
Fix underfittingFix overfitting
more layers / neuronsmore training data, data augmentation
train longerL1 / L2 regularization
better architecturedropout
tune hyperparametersearly stopping

Regularization ​

Add a penalty on large weights to the loss:

Jreg=J+λ∑|w|(L1),Jreg=J+λ∑w2(L2)

For a multi-layer network L2 sums over all layers: λ∑l‖W[l]‖F2 (Frobenius norm).

L1L2
pushes many weights to exactly 0 → sparse modelkeeps all weights small → dense model
robust to outlierssensitive to outliers
weaker on complex patternslearns complex patterns

Dropout: in each training step, randomly switch off neurons with probability p. No neuron can rely on one specific other neuron, so the network learns more robust features. Effect is similar to L2. Turn dropout off at test time.

Data augmentation: make more training data from what you have: add noise, rotate, flip, resize, distort.

Early stopping: watch the validation loss. When it stops going down (while the training loss keeps going down), stop training.

Vanishing / Exploding Gradients ​

In a deep network the gradient is a product of many per-layer factors. If they are <1 the gradient vanishes (early layers stop learning); if >1 it explodes (training diverges).

Three remedies:

  1. Normalize the inputs: zero mean, unit variance, x←(x−μ)/σ. Features on the same scale give a rounder loss surface and more stable steps. Use the training-set μ,σ for all sets (train, val, test).
  2. Batch normalization: do the same normalization inside the network, on each layer's output, using the statistics of the current mini-batch (mean ≈ 0, std ≈ 1).
  3. Good weight initialization:
InitVariance of wUse with
all zeros—✗ every neuron learns the same thing
N(0,1)1✗ too large in deep nets
Xavier1/nintanh
He (Kaiming)2/ninReLU

Hyperparameter Tuning ​

Hyperparameters: learning rate, number of layers, neurons per layer, activation, batch size, optimizer, number of epochs.

  • Grid search: try every combination on a fixed grid.
  • Random search: try random combinations. Usually better, because it tests more distinct values of the parameters that matter most (often the learning rate).