AI / ML Theory
Chapter 1 · 20 min

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 ∂L/∂z=y^−y\partial L / \partial z = \hat y - y. Now we walk the last step — from ∂L/∂z\partial L / \partial z to ∂L/∂w\partial L / \partial w — 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 {(xi,yi)}i=1m\{(x_i, y_i)\}_{i=1}^m with xi∈Rnx_i \in \mathbb{R}^n and yi∈{0,1}y_i \in \{0, 1\}. Parameters w∈Rnw \in \mathbb{R}^n, bias b∈Rb \in \mathbb{R}. Per-example pre-activation, prediction, loss:

zi=w⊤xi+b,y^i=σ(zi),z_i = w^\top x_i + b, \qquad \hat y_i = \sigma(z_i), ℓi=−yilog⁡y^i−(1−yi)log⁡(1−y^i).\ell_i = -y_i \log \hat y_i - (1 - y_i) \log(1 - \hat y_i).

Average BCE loss:

L(w,b)=1m∑i=1mℓi.L(w, b) = \frac{1}{m} \sum_{i=1}^m \ell_i.

3. The gradients

From 1.2, ∂ℓi/∂zi=y^i−yi\partial \ell_i / \partial z_i = \hat y_i - y_i. The chain rule extends this through the linear layer.

Gradient w.r.t. ww. Since ∂zi/∂w=xi⊤\partial z_i / \partial w = x_i^\top (a 1×n1 \times n row, numerator convention),

∂ℓi∂w=∂ℓi∂zi⋅∂zi∂w=(y^i−yi) xi⊤.\frac{\partial \ell_i}{\partial w} = \frac{\partial \ell_i}{\partial z_i} \cdot \frac{\partial z_i}{\partial w} = (\hat y_i - y_i)\, x_i^\top.

Transposing to a column gradient and averaging over the mini-batch,

 ∇wL  =  1m∑i=1m(y^i−yi) xi. \boxed{\ \nabla_w L \;=\; \frac{1}{m} \sum_{i=1}^m (\hat y_i - y_i)\, x_i.\ }

Gradient w.r.t. bb. ∂zi/∂b=1\partial z_i / \partial b = 1, so

 ∂L∂b  =  1m∑i=1m(y^i−yi). \boxed{\ \frac{\partial L}{\partial b} \;=\; \frac{1}{m} \sum_{i=1}^m (\hat y_i - y_i).\ }

The residual (y^i−yi)(\hat y_i - y_i) appears in both gradients the way it did for linear regression in 1.1 (with y^i\hat y_i being w⊤xi+bw^\top x_i + b 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:

w←w−α ∇wL,b←b−α ∂L∂b,w \leftarrow w - \alpha\, \nabla_w L, \qquad b \leftarrow b - \alpha\, \frac{\partial L}{\partial b},

with learning rate α>0\alpha > 0. Expanding,

 w←w−αm∑i=1m(y^i−yi) xi. \boxed{\ w \leftarrow w - \frac{\alpha}{m} \sum_{i=1}^m (\hat y_i - y_i)\, x_i. \ }

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 mm examples per step. Smooth trajectory, expensive when mm is large.
  • Stochastic GD (SGD). One example iki_k per step: w←w−α(y^ik−yik)xikw \leftarrow w - \alpha (\hat y_{i_k} - y_{i_k}) x_{i_k}. Noisy but cheap.
  • Mini-batch SGD. Average over a mini-batch of size BB (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 11 by scaling ww outwards: y^=σ(αw⊤x)→{0,1}\hat y = \sigma(\alpha w^\top x) \to \{0, 1\} as α→∞\alpha \to \infty on either side of the hyperplane, and the log-likelihood approaches its supremum at 00. The MLE doesn't exist — ww diverges. Fix: L2 regularisation L→L+λ2∥w∥2L \to L + \tfrac{\lambda}{2} \lVert w \rVert^2, 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 y^≈0\hat y \approx 0 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

wk+1=wk−αm∑i=1m(y^i−yi) xiw_{k+1} = w_k - \frac{\alpha}{m} \sum_{i=1}^m (\hat y_i - y_i)\, x_i

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: ∇wL=1m∑i(y^i−yi)xi\nabla_w L = \tfrac{1}{m} \sum_i (\hat y_i - y_i) x_i and ∂L/∂b=1m∑i(y^i−yi)\partial L / \partial b = \tfrac{1}{m} \sum_i (\hat y_i - y_i).
  • Pattern: residual × input, averaged.
  • BCE on logistic regression is convex; SGD finds the global minimum (use L2 regularisation if data is separable).

Self-test

(a) Derive ∇wL\nabla_w L for L2-regularised BCE, Lλ(w)=L(w)+λ2∥w∥2L_\lambda(w) = L(w) + \tfrac{\lambda}{2} \lVert w \rVert^2. Write the modified update.

(b) Show that the Hessian ∇w2L=1m∑iy^i(1−y^i) xixi⊤\nabla^2_w L = \tfrac{1}{m} \sum_i \hat y_i (1 - \hat y_i)\, x_i x_i^\top is PSD. When is it strictly positive definite?

(c) For 1-D data xi∈Rx_i \in \mathbb{R} on a linearly separable dataset, describe the BCE loss surface as w→∞w \to \infty. Where does the minimum live? What does adding λ2w2\tfrac{\lambda}{2} w^2 do?

(d) SGD vs mini-batch. Estimate the variance of the one-example gradient as a function of batch size BB. Why does increasing BB reduce variance by 1/B1/B and let you increase the learning rate by roughly the same factor (the "linear scaling" rule)?


References

  1. [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.

    ↩