M2-006M2: Classical Machine LearningLogistic Regression & GLMEasy
⚡Flashcard Drill➔Mastery:
Formulate Logistic Regression, Sigmoid Activation, and Binary Cross-Entropy Loss
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Logistic regression models class probability via the Sigmoid function: , and is optimized using negative log-likelihood (Binary Cross-Entropy Loss).
📌 Key Takeaways
- •Hypothesis: .
- •Sigmoid derivative: , enabling elegant gradient cancellation.
- •Loss function: .
- •Gradient: , mathematically identical in form to linear regression.
📐 Mathematical Derivations
Likelihood derivation under Bernoulli assumption: Let and . The likelihood for independent observations is . Taking the negative natural log and dividing by : . To compute the gradient with respect to : By the chain rule, . Note that , and , and . Multiplying together: . The non-linear terms cancel out completely, yielding the clean gradient .
🏭 Production Trade-offs
No closed-form solution exists because is non-linear in . Optimized using: (1) Newton-Raphson / IRLS (Iteratively Reweighted Least Squares): with weight matrix , achieving quadratic convergence for small-to-medium . (2) L-BFGS or SGD: Used in industrial production (e.g. Ad click prediction in FTRL-Proximal) for high-dimensional sparse features.
⚠️ Common Interview Pitfalls
- ✕Using Mean Squared Error (MSE) loss with Sigmoid activation (produces a non-convex loss surface with flat plateaus where gradients vanish when predictions are confidently wrong).
- ✕Confusing probabilities with binary classifications (the 0.5 decision threshold can be tuned for precision/recall tradeoffs).
🎯 Interviewer Follow-up Inquiries
- ?Why does combining Sigmoid with Mean Squared Error (MSE) cause vanishing gradients, whereas Cross-Entropy does not?
- ?How does Iteratively Reweighted Least Squares (IRLS) derive from the Newton-Raphson update in Logistic Regression?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
写出逻辑回归的模型形式与损失函数。
sigmoid 输出概率,用交叉熵(对数似然)训练;是 GLM 中 Bernoulli + logit 链接。
核心要点
- 模型与损失:逻辑回归假设 log(p/(1−p))=wᵀx(logit 是线性的),反解得 p=σ(wᵀx)=1/(1+e^−wᵀx)。损失来自 极大似然 :对 N 个样本,负对数似然为 −Σ[yᵢlog pᵢ+(1−yᵢ)log(1−pᵢ)],即二元交叉熵。关键性质是 梯度极其简洁 :∇_w L=Σᵢ(pᵢ−yᵢ)xᵢ=Xᵀ(p−y)——这与线性回归的梯度 Xᵀ(Xw−y) 形式一致,只是把 Xw 换成 σ(Xw)。这个简洁性来自 sigmoid 导数 σ'=σ(1−σ) 与交叉熵的巧妙配合(链式法则中的分母被约掉),这也是为什么 不用 MSE + sigmoid :后者的梯度含 σ' 因子,在饱和区趋近 0,导致梯度消失。
- 两个实践要点:① 凸性 ——逻辑回归的负对数似然是 凸函数 (海森 Xᵀdiag(p(1−p))X 半正定),故任何局部最优即全局最优,可用牛顿法/IRLS 快速收敛(通常 5–10 次迭代);② 完全分离问题 ——若数据线性可分,系数会趋向无穷(似然单调上升但永不达到最大值),表现为迭代不收敛、系数爆炸、标准误巨大;解法是加 L2 正则(等价于高斯先验的 MAP)或使用 Firth 惩罚似然。③ 系数解释 ——e^βⱼ 是优势比(odds ratio),表示特征每增 1 单位优势的倍数,这是业务方偏好逻辑回归的主要原因;但要注意这依赖'其他特征不变'的假设,共线时解释不可靠。
- 线性决策边界(特征空间)
- 没有闭式解,用梯度/牛顿/IRLS
常见陷阱
- 用 MSE 训练逻辑回归(梯度在饱和区消失)
- 对线性可分数据不做正则(系数发散)