Logistic Regression
🧮 What is logistic regression? Logistic regression is a linear classifier that outputs a probability. It computes a score
z = x w + band squashes it with the sigmoid,p = 1 / (1 + exp(-z)), into the probability of the positive class. It is trained by minimizing binary cross-entropy, which has no closed form, so you fit it with gradient descent.
Waymo asked an ML intern, in a phone screen built around a classification problem, to compare logistic regression with a neural network and then to discuss optimization algorithms and backpropagation (the question).
The Prompt
The model is one line, so the interviewer grades what surrounds it: a gradient you can derive, a loss that doesn't overflow, and a clear answer on how it relates to a neural network.
"Implement binary logistic regression with gradient descent. Return the weights and a function for probabilities. Then extend it to more than two classes."
Clarifying Questions
You: I'll assume X is (n, d) floats and y is (n,) with values 0 and 1, and I return w of shape
(d,) and a scalar bias. Is that right?
Interviewer: Yes.
You: Should I add regularization? On separable data the weights grow without limit otherwise.
Interviewer: Add an optional L2 term, not on the bias.
You: Can inputs be large enough to overflow exp? For example raw, unscaled features.
Interviewer: Assume anything. I don't want to see a NaN or an overflow warning.
You: For multiclass, softmax regression with integer labels 0..C-1?
Interviewer: Yes, softmax.
What You'll Build
sigmoid(z) -> (n,): a sigmoid that never overflows.loss_and_grad(w, b, X, y, l2) -> (loss, dw, db): binary cross-entropy computed from logits, and its gradient.fit_logistic(X, y, lr, epochs, l2) -> (w, b)andpredict_proba(X, w, b) -> (n,).fit_softmax(X, y, C, lr, epochs, l2) -> (W, b): the multiclass version,Wof shape(d, C).