Optimization and Gradient Descent
Loss landscapes, convexity, gradient descent and its stochastic variants: how model parameters are actually found.
Intermediate · about 10 hours · difficulty 3/5
Learn first: Calculus and Gradients
Fitting a model means solving an optimization problem: find parameters that minimize a loss. A few models have closed-form solutions, but almost everything else uses iterative, gradient-based methods.
Concepts
- Objective functions: loss plus regularization terms.
- Convex versus non-convex: linear and logistic regression have one global minimum; neural networks do not, yet still train well in practice.
- Gradient descent: repeat
theta = theta - lr * grad. The learning rate is the most important knob. - Stochastic and mini-batch gradient descent: noisy but cheap gradient estimates from small batches; the default for large data.
- Momentum and adaptive methods (preview): SGD with momentum, RMSProp, Adam. Covered in depth in the deep learning section.
- Constrained optimization and Lagrange multipliers: conceptual level, for understanding SVMs.
Diagnosing
Plot the loss per iteration. Divergence or oscillation means the learning rate is too high; a flat slow curve means too low or poorly scaled features. Feature scaling dramatically speeds up gradient descent.
Good enough to move on when you can implement mini-batch gradient descent for logistic regression and explain the effect of learning rate and batch size on the loss curve.
Key concepts
- Loss functions and objectives
- Convexity
- Batch gradient descent
- Stochastic and mini-batch GD
- Learning rate effects
- Momentum and Adam preview
- Lagrange multipliers
Mini project
Implement batch, mini-batch and stochastic gradient descent for logistic regression on the Breast Cancer Wisconsin dataset and plot loss curves for three learning rates on one figure.
Check yourself
Your loss goes to NaN after a few iterations. Name two likely causes and how you would check each.