VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 15: The ML coding round

Lesson 15.5 of 12 in this module Unrolled Pro

Logistic regression with gradient descent

Train a binary classifier p = sigmoid(Xw + b) by minimising the average log loss with gradient descent, in NumPy. From per-example Python loops with a sigmoid that overflows, to the vectorised gradient Xᵀ(p − y)/n, a numerically stable sigmoid and a loss computed with logaddexp from the logits, a finite-difference gradient check, and standardised features so one learning rate works. Then mini-batch SGD for large n, L2 regularisation for separable data, class weights for imbalance, and early stopping on a validation set.

about 10 min 3 Python examples 2 quick checks

Problem. Given features X (n × d) and labels y ∈ {0, 1}, learn weights w and bias b so that

text
p = sigmoid(X w + b),        sigmoid(z) = 1 / (1 + e^(−z))
loss = −mean( y · log p + (1 − y) · log(1 − p) )        (log loss, binary cross-entropy)

is minimised, using gradient descent. Return w, b and the training loss curve.

The gradient has a famously clean form: ∂loss/∂w = Xᵀ (p − y) / n and ∂loss/∂b = mean(p − y). The interview is about the details around it: computing it without loops, keeping sigmoid and log from overflowing or returning log(0), verifying the gradient, and making gradient descent actually converge.

ML context. Logistic regression is the baseline classifier, the output layer of every binary neural classifier, and the model behind click-through prediction and calibration. Its loss is the binary case of softmax cross-entropy (lesson 15.6), and the stable way to compute it (from logits, with logaddexp) is exactly how deep-learning libraries do it.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Logistic regression with gradient descent"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee