什么是 overfitting / underfitting?
bias-variance tradeoff 是什么?怎么判断、怎么解决?

英文问法What is overfitting and how do you prevent it? · Explain the bias-variance tradeoff. · How do you tell if a model is overfitting or underfitting?

考过★ 作者本人微软 AS 电面 · 微软 AS 实习电面问"如何避免 overfit"(1p3a 1165076)· 微软 AS 实习电面问 bias-variance(1p3a 1158834)· MAI Senior L63 第 4 轮 · 印度校招 AS · Glassdoor AS II · NVIDIA 框架岗 2023 onsite 第 2 轮(知乎)· Unity Ads MLE 2025-04 HM 轮(overfitting,1p3a 1122164)· Unity ML intern 2022(Overfitting vs Underfitting.,Glassdoor RVW64662704)

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

Overfitting
模型把训练数据里的 noise 也背下来了,像死记硬背的学生,换一道题就不会。
Underfitting
模型太简单,train、validation error 都高。
Bias
换很多批训练数据各训练一次,平均预测离真值多远。模型假设太死,平均就是偏的。
Variance
换一批训练数据,预测变多少。模型太灵活,会跟着每批数据的 noise 跑。
Learning curve
train / validation error 随训练样本数变化的曲线,两者之差叫 generalization gap。

30 秒回答

Overfitting means the model fits training noise: low training error, much higher validation error. Underfitting means it's too simple, so both errors are high. For squared loss, expected test error = bias² + variance + noise. Bias is how far the average prediction over training sets is from the truth; variance is how much the prediction moves when the training set changes. More capacity lowers bias and raises variance, so overfitting is high variance and underfitting is high bias. To fix overfitting: more data, L1/L2 or dropout, early stopping, a smaller model, or bagging. To fix underfitting: a bigger model, better features, less regularization.

推导:bias-variance decomposition

设 \(y = f(x) + \varepsilon\),\(\operatorname{Var}(\varepsilon) = \sigma^2\),\(\bar f = \mathbb E_D[\hat f(x)]\) 是换遍训练集的平均预测。把 \(y - \hat f\) 拆成 \((f - \bar f) + (\bar f - \hat f) + \varepsilon\),平方取期望,交叉项都是 0:

\[ \mathbb E\big[(y - \hat f)^2\big] = \underbrace{(f - \bar f)^2}_{\text{bias}^2} + \underbrace{\mathbb E_D\big[(\hat f - \bar f)^2\big]}_{\text{variance}} + \underbrace{\sigma^2}_{\text{noise}} \]

noise 是任何模型都消不掉的下限,test error 随 capacity 呈 U 形。

实测:多项式回归

\(\sin 2\pi x\) 加 noise(\(\sigma^2 = 0.09\)),10 个点训练,换 2000 个训练集:

模型train MSEtest MSEbias²variance诊断
1 次(直线)0.340.330.220.02underfit
3 次0.060.130.010.03刚好
9 次0.000.690.000.60overfit
9 次 + L2,\(\lambda = 10^{-4}\)0.040.130.000.04variance 压下来
9 次 + L2,\(\lambda = 0.1\)0.190.230.120.02罚太狠,bias 上升

每一行都满足 test ≈ bias² + variance + 0.09。

怎么判断是 overfit 还是 underfit

  1. train error 离 noise 下限(实际用人类水平或 baseline)很远 → underfit(high bias)。
  2. train error 低,validation error 高很多 → overfit(high variance)。
  3. 拿不准就画 learning curve。加数据后 gap 持续缩小是 variance,两条线贴在一起停在高处是 bias。

同一实验,\(N\) 从 10 加到 100:

N102050100
9 次:train / test0.00 / 0.690.05 / 0.130.07 / 0.110.08 / 0.10
1 次:train / test0.34 / 0.330.31 / 0.300.30 / 0.290.29 / 0.29

9 次的 gap 从 0.69 缩到 0.02,加数据有用。直线停在 0.29,远高于 0.09,加数据没用,要换更强的模型。

必答点

追问

Regularization 为什么能防 overfitting?

限制 weight 就是限制有效 capacity,用一点 bias 换 variance 大幅下降,上表 \(\lambda = 10^{-4}\) 把 variance 从 0.60 压到 0.04。

Early stopping 为什么也算 regularization?

validation error 开始上升就停,限制了参数离初始值能走多远。线性模型 + squared loss + gradient descent 时等价于 L2(Goodfellow §7.8)。

参数远多于样本,为什么不一定 overfit?

\(P > N\) 之后 test error 会再次下降,叫 double descent,因为模型能用更平滑的函数穿过全部训练点(Murphy §13.5.7)。

来源Murphy, PML1 §1.2.3(pp.12–13)、§4.5(pp.120–127)、§4.7.6.3(p.161)、§13.5.7(p.459)、§18.5(p.612)· Bishop, PRML §1.1(p.9)、§3.2(pp.147–151)· Goodfellow et al., Deep Learning, Ch.5 §5.2、§5.4.4(pp.108–113、127–128)· Ch.7 §7.8、§7.12(pp.241–247、255)