Optimization for Machine Learning
mathematical methods for finding optimal solutions in learning algorithms
Read diagram labels
- Start (w₀)
- Step 1
- Step 2
- Global minimum
- Contours represent levels of equal loss f(w)
Core Concepts Covered
- Convex optimization, gradient descent, and SGD
- Advanced optimizers: Momentum, RMSProp, and Adam
- Regularization techniques (L1, L2, dropout, early stopping)
1. Objectives, Convexity, and Constraints
Training chooses parameters to minimize an objective . A per-example loss measures prediction error, a regularizer expresses a preference such as small weights, and the objective combines them: A metric reports behavior we care about, such as accuracy, and need not be differentiable or identical to the optimized loss.
A function is convex when every chord lies above its graph. For differentiable convex objectives, every local minimum is global; strict convexity gives at most one minimizer. A positive-semidefinite Hessian is a useful twice-differentiable test. Deep networks are generally non-convex and include saddles and flat directions.
Constraints restrict feasible parameters, for example . Unit 6 introduced Lagrange multipliers; this unit focuses on iterative algorithms and diagnostics.
Optimizers Follow Different Paths Across a Ravine
Read diagram labels
- SGD oscillation
- momentum
- adaptive scaling
Loss, Regularizer, Objective, and Metric
Two examples have squared losses and . Weights are and with . Find the objective; state a possible metric.
1.Mean data loss is .
2., so the penalty is .
. For regression, MAE could be reported even when MSE is optimized.
2. Loss Functions
For a regression residual , MSE uses and strongly penalizes outliers; MAE uses and is more robust but has a kink at zero. Huber loss is quadratic for and linear outside:
Binary cross-entropy is . For mutually exclusive classes, softmax gives , and cross-entropy for true class is . Unit 12 explains its information-theoretic meaning.
Hinge loss for and score is . It becomes zero once the correct-class score clears the unit margin, matching Unit 13's SVM geometry.
Regression Losses Penalize Residuals Differently
Read diagram labels
- MSE: e²
- MAE: |e|
- Huber
- residual e
Regression Losses Under an Outlier
Residuals are and Huber uses . Calculate mean MSE, MAE, and Huber loss.
1.MSE is .
2.MAE is .
Huber terms are , averaging .
MSE lets the large residual dominate; MAE and Huber grow only linearly in the tails.
Classification Losses
For binary , find BCE. For softmax probabilities and true class 2, find cross-entropy. For , find hinge loss.
1.BCE is .
2.Multiclass cross-entropy is .
Hinge loss is .
3. Batch, Stochastic, and Mini-Batch Gradient Descent
Gradient descent updates . Batch descent uses every example for a stable but costly step. SGD uses one random example for a cheap, noisy estimate. Mini-batches average a moderate group, exploiting vectorized hardware while retaining useful gradient noise.
The learning rate controls step size. Too small wastes computation; too large can oscillate or diverge. Shuffle data and report whether a logged loss is per example, per batch, or an epoch average.
Two Gradient Steps by Hand
Minimize from with .
1.. At , , so .
2.At , , so .
falls from to to .
4. Momentum, RMSprop, Adam, Schedules, and Convergence
Momentum accumulates velocity, . RMSprop tracks and scales the step by , damping coordinates with persistently large gradients.
Adam combines both: , , and Warmup, step decay, cosine schedules, or plateau reductions can improve stability.
Convergence may mean small gradients, small objective changes, exhausted compute, or best validation performance. State the criterion and inspect gradient norms, validation curves, and sensitivity to random seeds.
Optimizers Follow Different Paths Across a Ravine
Read diagram labels
- SGD oscillation
- momentum
- adaptive scaling
First Adam Step
With , , , , , and negligible , find the first update.
1. and .
2.Bias correction gives and .
The step is , so .
Bias correction compensates for zero-initialized moment estimates.
5. Regularization, Early Stopping, and Diagnostics
regularization adds , producing gradient and discouraging large weights. adds and promotes sparsity. Decoupled weight decay is not always identical to an penalty with adaptive optimizers.
Early stopping saves the checkpoint with the best validation score and halts after a patience window. Both losses staying high suggests underfitting; training loss falling while validation loss rises suggests overfitting. Exploding gradients suggest clipping or a lower rate; flat training may indicate poor scaling, saturation, or a rate that is too small.
Training and Validation Curves Reveal Overfitting
Read diagram labels
- training
- validation
- best checkpoint
Regularized Objective and Gradient
At , data loss is with gradient , and the objective uses . Find the objective and total gradient.
1.Penalty is , so .
2.Its gradient is .
Total gradient is .
Cumulative Training Diagnostic
Training losses are while validation losses are . Which checkpoint should be retained?
1.Validation loss is lowest at epoch 3, so retain that checkpoint.
At epoch 4 training improves while validation worsens, suggesting overfitting has begun.
Interactive Practice Quiz
Test your understanding with instant feedback
For a convex differentiable objective, which statement is guaranteed?
Which method updates from one randomly selected training example?
What distinguishes Adam from vanilla SGD?
What penalty does regularization add?
Training loss falls while validation loss begins rising. What is the most likely diagnosis?
For residual and Huber threshold , what is the loss?
Which quantity may be non-differentiable because it reports rather than trains?
Why does Adam bias-correct its moment estimates?
Further Readings
Explore these highly recommended external references to deepen your understanding
Dive into Deep Learning: Optimization Algorithms
https://d2l.ai/chapter_optimization/index.html
PyTorch Optimization Documentation
https://docs.pytorch.org/docs/stable/optim.html
PyTorch Loss Functions
https://docs.pytorch.org/docs/stable/nn.functional.html#loss-functions
Adam: A Method for Stochastic Optimization
https://arxiv.org/abs/1412.6980
