Loss Functions & Optimizers
What you minimize and how
The loss scores every prediction and the optimizer decides how to descend it. Choosing them well is the difference between a loss curve that falls smoothly and one that oscillates, plateaus or diverges.
What you will learn
- Match loss functions to problem type and output activation
- Explain how SGD, momentum and Adam differ in practice
- Tune learning rate using convergence signals instead of guesses
What is this?
The loss quantifies how wrong the model is on a batch, and the optimizer uses its gradient to update weights. Loss choice must match the problem (classification → cross-entropy, regression → MSE/MAE) and the final activation. Optimizers build from plain SGD by adding momentum and per-parameter adaptive rates (Adam), changing step size over training. They are inseparable: the best loss trained with a bad optimizer still won't converge.
Why do you need it?
- Wrong loss/activation pair silently cripples gradients (e.g. MSE on sigmoids).
- Momentum smooths noisy gradients; Adam adapts learning rates per parameter for sparse, varied scales.
- Learning rate is the single most impactful knob — too high diverges, too low stalls.
- A optimizer/loss audit is the fastest fix for 'loss is flat' complaints.
Mental model
A hiker descending a foggy mountain
An optimizer minimizing the loss landscape
The hiker needs a direction (gradient), step size (learning rate), and rhythm (momentum) — and in fog, particular features need different step sizes (Adam's per-parameter rates). Step too big and you overshoot; too small and night falls before you reach the valley.
How it works
- A loss scores a batch: CE = -Σ y·log(p), MSE = mean((ŷ-y)²); both are differentiable so gradients exist.
- SGD: w ← w - lr·g, using the current batch's gradient (noisier, but cheap and generalizes).
- Momentum: keeps a velocity v = β·v + g and steps with v — dampens noise, accelerates saddles.
- Adam: per-parameter adaptive rates from first and second moment estimates, with bias correction — robust, fast early.
- Schedule: lower the LR over time (step/decay/cosine) so coarse descent becomes fine-tuning near the optimum.
From loss to optimizer to weights and back.
In production
- Classification heads couple CrossEntropy/BCEWithLogits with the right final layer — standard across image and NLP models.
- Regression uses MSE for L2 or Huber for robustness to outliers; quantile losses for intervals.
- Recommendation & retrieval train with sampled softmax / InfoNCE — pairwise or contrastive losses.
- LLM pretraining/fine-tuning uses cross-entropy over token logits with AdamW.
Best practices
- Pair loss with activation (logits + CE; identity + MSE) to keep gradients well-conditioned.
- Default to Adam/AdamW for new work, SGD+momentum for image nets or when generalization is finicky.
- Start with a small LR and double until divergence to find the effective-max LR (LR range test).
- Decay LR on plateau or use cosine schedule; never hold a constant high LR to the end.
Common mistakes
- Sticking with MSE on a classification problem — gradients get tiny for wrong-but-confident predictions.
- Tuning the architecture while leaving the optimizer at defaults, then blaming the model.
- Using Adam without weight decay where L2-regularization-style decay is meant for SGD.
- Changing LR too late: spikes wasted early, then tiny steps creep forever.
Common issues
Train loss bounces hard every batch and never descendsLR too high — updates overshoot the valley
Reduce LR (10x), then ramp; add gradient clipping for safety.
Loss sits at exactly ln(2) for binary classificationConfident wrong predictions with near-zero gradients, or logits↔label mismatch
Use BCE logits directly, check label range (0/1), and normalize outputs.
Adam converges fast then wobbles near the endAdaptive LR too coarse late in training
Lower final LR, add warm-up + cosine decay, or anneal to SGD/momentum at the end.
Debugging tips
- Watch training loss per-batch: healthy = noisy downward; diverging = ascending or NaN.
- A flat 0.693 for binary CE means pure chance — check the activation/loss pairing first.
- Compare SGD(with momentum) vs Adam on a small overfit run — if Adam fails a single batch, the loss gradient is wrong.
- Log lr per epoch and weight-norm along with loss to separate schedule problems from loss problems.
Interview checkpoint
Hands-on lab
- Train a small net with different loss/optimizer pairs
- Observe SGD vs Adam convergence differences
- Use the LR range test to pick a compatible learning rate
Tune Loss and Optimizer on a Toy Problem
30 min · intermediate
Quick quiz
Which loss/activation combination keeps classification gradients informative?
Adam removes the need to ever think about the learning rate.