1.3 — Gradient descent on the logistic-regression loss
Goal: Derive the gradient of the BCE loss w.r.t. the parameters and write the gradient-descent update rule in full vector form.
1. Why this session exists
Session 1.2 gave us the loss and the pre-activation gradient . Now we walk the last step — from to — and write down the gradient-descent update. This is the simplest end-to-end training loop in the course: it has all the moving pieces of SGD with none of the depth.[1]
2. Setup
A training set with and . Parameters , bias . Per-example pre-activation, prediction, loss:
Average BCE loss:
3. The gradients
From 1.2, . The chain rule extends this through the linear layer.
Gradient w.r.t. . Since (a row, numerator convention),
Transposing to a column gradient and averaging over the mini-batch,
Gradient w.r.t. . , so
The residual appears in both gradients the way it did for linear regression in 1.1 (with being there). The pattern is general: gradient = average over examples of (residual × input).
4. The update rule
Vanilla (batch) gradient descent uses the full-batch gradient:
with learning rate . Expanding,
This is the canonical update written out symbol by symbol. Three variants depending on how many examples we average over per step:
- Batch GD. All examples per step. Smooth trajectory, expensive when is large.
- Stochastic GD (SGD). One example per step: . Noisy but cheap.
- Mini-batch SGD. Average over a mini-batch of size (typically 32–1024 for tabular data, larger for vision/NLP). Standard practice.
5. Two pathologies and how to fix them
Separable data. If the classes are perfectly linearly separable, the likelihood can be made arbitrarily close to by scaling outwards: as on either side of the hyperplane, and the log-likelihood approaches its supremum at . The MLE doesn't exist — diverges. Fix: L2 regularisation , which makes the problem strictly convex with a unique finite minimiser.
Imbalanced classes. If one class dominates (say 99% negatives), the unregularised loss is dominated by the negative class and the model collapses to predicting everywhere. Fix: class weights in the loss, oversampling / undersampling the data, or focal-style losses (Chapter 7).
6. Where this is going
The update rule
is the single-sigmoid-unit version of the gradient descent rule that, in Chapter 2, will be composed across many hidden layers. The only new ingredient there is the chain rule from 0.3 walked backwards through additional matrix multiplications.
7. Takeaways
- BCE gradient: and .
- Pattern: residual × input, averaged.
- BCE on logistic regression is convex; SGD finds the global minimum (use L2 regularisation if data is separable).
(a) Derive for L2-regularised BCE, . Write the modified update.
(b) Show that the Hessian is PSD. When is it strictly positive definite?
(c) For 1-D data on a linearly separable dataset, describe the BCE loss surface as . Where does the minimum live? What does adding do?
(d) SGD vs mini-batch. Estimate the variance of the one-example gradient as a function of batch size . Why does increasing reduce variance by and let you increase the learning rate by roughly the same factor (the "linear scaling" rule)?
References
- [1]
Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer. — Linear regression (1.1), logistic regression (1.2–1.3), softmax (1.7), MAP / Gaussian priors.
↩