Linear / logistic regression 的 loss 从哪来?
MLE、MSE、BCE 一页讲完

英文问法Derive the loss of linear / logistic regression from MLE. · Why MSE for linear regression but cross-entropy for logistic regression? · Prove MSE is non-convex for logistic regression.

考过微软 AS2 推导轮(2025-11:从 MLE 推 BCE;prove why MSE is non convex for logistic)· Glassdoor AS II(MLE loss 的概率解释)· Glassdoor Senior AS(sigmoid、LR 数学)· Glassdoor AS 2026(NumPy 手写 loss 和 logistic regression,见 ml-coding)

先弄清楚:每个概念是什么

linear / logistic regression 是模型,MLE 是挑参数的方法,MSE / BCE 是 MLE 在不同假设下推出来的 loss。

Linear regression
在散点图里拉一根直线(高维是平面),让它整体上离所有点最近。输出是任意实数。 例:用面积预测房价,价格 ≈ w × 面积 + b,w 是每多 1 平米房价涨多少。
Logistic regression
名字叫 regression,做的是二分类。先给每个样本打一个分 \(z = w^\top x\),再用 sigmoid 把分数换成属于正类的概率。分数为 0、概率为 0.5 的地方是决策边界。 例:判断邮件是不是 spam,输出 0.93 表示 93% 是 spam。
Sigmoid
S 形曲线,把任意分数压进 0 到 1 之间。\(\sigma(0) = 0.5\),\(\sigma(2) \approx 0.88\),\(\sigma(-2) \approx 0.12\)。分数越极端曲线越平,平的两端就是后面 gradient 消失的地方。
Probability vs likelihood
同一个式子 \(P(\text{data} \mid \theta)\)。固定 \(\theta\) 看数据是 probability,对数据求和等于 1。固定数据看 \(\theta\) 是 likelihood,对 \(\theta\) 积分一般不等于 1,所以不是 \(\theta\) 的 distribution。 例:已经抛出 7 正 3 反,给每个可能的 \(p\) 打分。
MLE(maximum likelihood estimation)
数据已经摆在面前,反推最可能产生它的参数。 例:抛 10 次硬币 7 次正面,\(p = 0.7\) 时出现这个结果的可能性约是 \(p = 0.5\) 时的 2.3 倍,而且没有哪个 \(p\) 更高,所以 MLE 选 0.7。
NLL(negative log-likelihood)
MLE 和 loss 之间的桥。likelihood 取 log 再取负,maximize 就变成 minimize。平时说的 loss 大多是某个模型的 NLL。
MSE(mean squared error)
residual(预测和真实值的差)平方后求平均。平方让大错被重罚,差 1 罚 1,差 10 罚 100。 例:三个误差都是 1,MSE = 1。把其中一个换成 10,MSE 变成 34,所以它对 outlier 敏感。
BCE(binary cross-entropy)
重罚"自信地答错"。真实答案是 1 时 loss = \(-\log p\),预测 90% 只扣 0.11,预测 50% 扣 0.69,预测 1% 扣 4.6。
MAP 与 regularization
MLE 只看数据。MAP 再带一个 prior,在 NLL 上加一项 \(-\log\) prior,数据少时不轻易下极端结论,\(n\) 越大越接近 MLE。L2 对应 Gaussian prior(weight 多半不大),L1 对应 Laplace prior(大部分 weight 是 0)。 例:只抛 1 次得到正面,MLE 说 \(p = 1\),用 Beta(2, 2) prior 的 MAP 说 \(\frac{k+1}{n+2} = 2/3\)。
Convex vs non-convex
convex 的 loss 像一个碗,从哪里出发往下走都到同一个最低点。non-convex 有多个坑和平台,可能卡在半路。linear regression 的 MSE 和 logistic regression 的 BCE 都是 convex,sigmoid 套上 MSE 就不是。

30 秒回答

Both losses come from maximum likelihood: we pick the parameters that make the observed data most probable, which means minimizing the negative log-likelihood. For linear regression we assume y = wᵀx plus i.i.d. Gaussian noise with constant variance; the NLL is MSE over 2σ² plus a constant, so least squares is the MLE, with closed form (XᵀX)⁻¹Xᵀy. For logistic regression the label is Bernoulli with p = σ(wᵀx); the NLL is binary cross-entropy, its gradient is (p − y)x, and its Hessian is PSD, so it's convex but has no closed form. We don't use MSE there because with a sigmoid it's non-convex — for y = 0 the second derivative in the logit is 2σ²(1 − σ)(2 − 3σ), negative once σ > 2/3 — and its gradient vanishes when the model is confidently wrong.

一张表

Linear regressionLogistic regression
模型\(y = w^\top x + \varepsilon\)\(P(y{=}1 \mid x) = \sigma(w^\top x)\)
likelihood\(\varepsilon \sim \mathcal N(0, \sigma^2)\),i.i.d.\(y \sim \text{Bernoulli}(p)\)
NLL = lossMSE(乘 \(\frac{1}{2\sigma^2}\) 再加常数)BCE
gradient\(\frac2n X^\top(Xw - y)\)\(\frac1n X^\top(p - y)\)
Hessian\(\frac2n X^\top X \succeq 0\)\(\frac1n X^\top S X \succeq 0,\ S = \operatorname{diag}(p_i(1-p_i))\)
求解closed form:normal equation没有 closed form:GD / Newton(IRLS)

推导

MLE

\[ \hat\theta = \arg\max_\theta \prod_i p(y_i \mid \theta) = \arg\min_\theta\; -\frac1n\sum_i \log p(y_i \mid \theta) \]

取 log 是为了方便(乘积变求和、防 underflow),而且 log 单调递增,argmax 不变。

Linear regression → MSE → normal equation

\[ p(y_i \mid x_i) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\!\Big(-\frac{(y_i - w^\top x_i)^2}{2\sigma^2}\Big) \] \[ -\frac1n\,\ell(w) = \tfrac12\log(2\pi\sigma^2) + \frac{1}{2\sigma^2}\,\text{MSE}(w) \]

常数项和正的倍数都不改变 argmin,所以 \(\hat w_{\text{MLE}} = \arg\min \text{MSE}\),\(\sigma\) 不影响 \(\hat w\)。令 gradient 为 0:

\[ X^\top X\,w = X^\top y \;\Longrightarrow\; \hat w = (X^\top X)^{-1}X^\top y \] \[ \nabla^2 = \tfrac2n X^\top X,\qquad v^\top X^\top X\,v = \|Xv\|^2 \ge 0 \;\Longrightarrow\; \text{convex} \]

Logistic regression → BCE → gradient

\[ P(y \mid x) = p^{\,y}(1-p)^{\,1-y},\qquad p = \sigma(w^\top x) \] \[ L = -\frac1n\sum_i \big[y_i\log p_i + (1-y_i)\log(1-p_i)\big] \] \[ \sigma' = \sigma(1-\sigma) \;\Longrightarrow\; \frac{\partial L_i}{\partial z_i} = -y_i(1-p_i) + (1-y_i)\,p_i = p_i - y_i \]

为什么 logistic regression 不用 MSE

\[ y = 0:\quad L(z) = \sigma^2,\qquad L''(z) = 2\sigma^2(1-\sigma)(2-3\sigma) < 0 \ \text{ when } \sigma > \tfrac23 \] \[ z = w^\top x \;\Longrightarrow\; \nabla_w^2 L = L''(z)\,xx^\top \;\Longrightarrow\; \text{not PSD,non-convex} \]

gradient 消失:\(y = 1,\ z = -10\) 时,MSE 的 \(\partial L/\partial z = 2(\sigma - 1)\sigma(1-\sigma) \approx -9\times10^{-5}\),BCE 的 \(\sigma - 1 \approx -1\)。

Regularization = MAP

\[ \hat w_{\text{MAP}} = \arg\min_w \big[\text{NLL}(w) - \log p(w)\big] \] \[ \mathcal N(0, \tau^2 I) \Rightarrow \text{L2}\ \big(\lambda = \sigma^2/\tau^2\big);\qquad \text{Laplace} \Rightarrow \text{L1};\qquad \text{uniform} \Rightarrow \text{MLE} \]

必答点

追问

σ 会影响 ŵ 吗?σ² 的 MLE 是多少?

不会影响。\(\hat\sigma^2 = \text{SSE}/n = \text{MSE}(\hat w)\)(Murphy eq. 11.49)。

MSE 为什么对 outlier 敏感?

Gaussian 按 \(e^{-r^2}\) 衰减,尾部很薄,远处的点几乎不可能出现,最优解会把线拉过去。换成尾部更厚的 Laplace noise 对应 MAE,也可以用 Huber loss。

XᵀX 不可逆怎么办?实际会直接求逆吗?

原因是 \(d > n\) 或 multicollinearity,用 ridge \((X^\top X + \lambda I)^{-1}X^\top y\) 解决。实际不显式求逆,用 QR 或 SVD-based solver(如 scipy.linalg.lstsq)。

数据 linearly separable 时,logistic regression 会怎样?

MLE 不存在:\(\|w\| \to \infty\),sigmoid 变成 step function(Bishop §4.3.2)。加 L2 regularization 解决。

代码里 BCE 怎么写才数值稳定?

直接对 logits 计算 \(\log\sigma(z) = -\log(1 + e^{-z})\),不要先算出 \(\sigma\) 再取 log。PyTorch 里用 BCEWithLogitsLoss。

来源CS229 notes §1.2–1.3、§2.1 · Murphy, PML1 §4.2、§10.2.3、§11.2–11.3 · Cross Validated #326350(MSE non-convex)· 全部资料见 RESOURCES.md