对应 Maps to:Stanford CS229 Autumn 2018 Lectures 8、9、13(Data Splits, Models & Cross-Validation; Approx/Estimation Error & ERM; Debugging ML Models and Error Analysis),CS229 main notes(Andrew Ng & Tengyu Ma)Ch. 8(Generalization)和 Ch. 9(Regularization and model selection),CS229 learning theory notes,以及 Andrew Ng 的讲稿 “Advice for applying machine learning”。 CS229 当前学期(Summer 2026)的课表需要 Stanford 登录才能看,所以本讲以公开的讲义和 2018 年的录像为准;这部分内容多年来基本没变(double descent 和 implicit regularization 是新版 main notes 加进来的)。 配套代码 Code:
code/lecture03/(numpy;1 个工具模块 + 5 个脚本,最慢的约 16 秒) 预计阅读时间:2.5–3 小时(含运行代码)。这是一份复习讲义:重点放在”为什么”和”实验里到底看到了什么”上,包括好几处和教科书故事不完全一样的结果。
0. 本讲导航 · How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- 写出 bias–variance 分解,并说清楚 bias 和 variance 是”对训练集的随机性取期望”才有定义的量 —— 以及为什么高次多项式的 variance 是重尾的。
- Explain double descent with one picture in your head: the smallest singular value of the feature matrix, and the minimum-norm solution chosen by lstsq / GD-from-zero (implicit regularization).
- 用 Hoeffding + union bound 推出有限假设类的样本复杂度 ,说出 VC 维在替换 时扮演的角色,以及这些界”松在哪里、对在哪里”。
- 把 ridge、lasso、early stopping、data augmentation 都看成”先验”:知道它们各自的偏差往哪个方向,以及先验不对时会发生什么。
- 正确使用交叉验证:k 的选择、LOOCV 的封闭公式、one-standard-error rule、选择之后的乐观偏差与 nested CV。
- 用学习曲线、“optimizer vs objective”诊断、error analysis 和 ablative analysis 决定”下一步该做什么”。
0.2 默认你已经会的 / 本讲会省略的
| 默认你已经会 Assumed | 本讲省略或只给结论 Skipped |
|---|---|
| 第 1 讲:最小二乘、normal equation、最小范数解、高斯 MLE | Hoeffding 不等式和 VC 定理的证明(只给结论和直觉) |
| 过拟合 / 欠拟合的基本概念 | Sauer 引理、Radon 定理的证明 |
| 期望、方差、协方差 | 随机矩阵理论(Marchenko–Pastur)的推导;double descent 的精确渐近公式 |
| SVD 的定义 | LOOCV 公式(Sherman–Morrison)、高斯后验的完整代数 |
| Python + numpy 基础 | AIC / BIC 和贝叶斯模型选择的推导(§5.3 只给结论) |
0.3 本讲地图 Map
§1 Bias–variance:重新采样训练集,直接"看见"两项;approximation vs estimation error ← 代码 01
§2 Double descent:p = n 的尖峰、最小奇异值、第二次下降、implicit regularization ← 代码 02
§3 Learning theory:ERM、Hoeffding + union bound、一致收敛、VC 维 ← 代码 03
§4 Regularization:ridge = MAP、完整后验、lasso、early stopping 与数据增强 ← 代码 04
§5 Model selection:hold-out / k-fold / LOOCV、选择之后的乐观偏差、nested CV、贝叶斯视角 ← 代码 05 演示 1–2
§6 ML advice:学习曲线、optimizer vs objective、error analysis、ablative analysis ← 代码 05 演示 3–6
§7–11 小结、常见误区、练习、延伸阅读、下一讲预告
0.4 Notation(在第 1 讲的基础上新增)
| 符号 | 含义 |
|---|---|
| , | 真实函数和噪声方差:,, |
| , | 一个随机的训练集,以及在它上面训练出来的模型 |
| , | 假设类(hypothesis class)及其大小(有限时) |
| , | 泛化误差(真实风险)和训练误差(经验风险) |
| , | 里泛化误差最小的假设;ERM 选出的假设 |
| 学习理论部分沿用 CS229 learning theory notes 的记号,用 表示样本数(其余部分仍用 ) | |
| 特征 / 参数个数(§2 的随机特征数) | |
| 特征矩阵及其 SVD, |
代码的约定(和第 1 讲一致):回归的代价写成平均形式 ,所以 的含义不随样本量变化;截距不惩罚。
0.5 关于数字
讲义里所有实验数字都来自配套代码的实际输出(固定随机种子,同一台机器上重复运行逐字相同)。数据是合成的:我们知道真实函数,所以 bias、variance、泛化误差都能精确地算或无限地重新采样 —— 这正是本讲需要的。好几处结果和”预想的故事”不一样,讲义按实际结果写,并在代码 README 的”关于实验结果”里记录了原计划和改动的原因。
1. Bias–variance · 偏差与方差
1.1 The decomposition
Fix a test point . The learned model depends on the random training set ; let be the “average model”. For a fresh label :
The key step: write and square. The cross terms vanish because is independent of with mean zero, and by definition. So this is an identity, not an approximation.
直觉:bias 是”平均而言错了多少”—— 假设类太小、或者正则化把模型拉偏了;variance 是”换一个训练集,预测会抖多少”—— 模型太灵活,把噪声也学进去了。两者都是对训练集的随机性取期望才有定义的量。现实中你只有一个训练集,看不到它们;合成数据可以无限地重新采样训练集,于是两项都能直接算出来。第 1 讲 §2.5 只用了一个训练集,这里是它的续集。
1.2 Seeing it: polynomial degree
代码 01_bias_variance.py 演示 1:,,每个次数 重新采样 个训练集,在 1000 个固定的测试点上算 bias² 和 variance:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | |
|---|---|---|---|---|---|---|---|---|---|
| train MSE | 0.5702 | 0.2659 | 0.2511 | 0.0819 | 0.0773 | 0.0715 | 0.0689 | 0.0666 | 0.0603 |
| bias² | 0.490 | 0.199 | 0.202 | 0.0048 | 0.0054 | 0.0042 | |||
| variance | 0.0204 | 0.0228 | 0.0512 | 0.0173 | 0.0314 | 0.0634 | 0.138 | 1.035 | 30.82 |
| bias² + var + | 0.6006 | 0.3121 | 0.3427 | 0.1121 | 0.1268 | 0.1535 | 0.2281 | 1.125 | 30.92 |
| test MSE(直接测,平均) | 0.6005 | 0.3125 | 0.3428 | 0.1121 | 0.1267 | 0.1536 | 0.2279 | 1.126 | 30.92 |
| test MSE(中位数) | 0.5941 | 0.3060 | 0.3220 | 0.1074 | 0.1130 | 0.1140 | 0.1216 | 0.1340 | 0.1926 |
What to notice:
- The identity checks out: the formula row and the directly measured row agree to about 0.1%. Bias falls with , variance rises, and the sum is U-shaped with its minimum at .
- is worse than , and worse than . In the centred coordinate , is odd, so even powers cannot reduce the bias (0.199 → 0.202) but still cost an extra parameter’s worth of variance. “One more parameter” is never free.
- The variance is heavy-tailed. For the mean and the median part ways (: mean 30.9, median 0.193). Most training sets are fine; a few leave a gap near or , the polynomial extrapolates into the gap and explodes, and those rare sets dominate the expectation. 第 1 讲那一个训练集看到的是”典型”的情形;“期望测试误差”却被倒霉的训练集主导。( 的 bias² 不再单调下降,是因为平均预测 本身只是 2000 个样本的平均,混进了约 variance 的蒙特卡洛误差。)
最初的计划是直接沿用第 1 讲的 。试跑时 的平均值在两次独立的试跑之间就相差好几倍 —— 期望本身被重尾主导、极不稳定(记录见代码 README 的”关于实验结果”)。所以这里改用 ,并同时报告中位数。
1.3 The same trade-off with a different knob: ridge
Now fix the flexible model of Lecture 1 — degree 14, , i.e. 15 parameters for 15 points — and add (演示 2,2000 个训练集):
| 0 | 0.1 | 1 | 10 | |||||
|---|---|---|---|---|---|---|---|---|
| bias² | – | 0.0024 | 0.0020 | 0.012 | 0.041 | 0.171 | 0.374 | 0.486 |
| variance | 23.5 | 0.618 | 0.1565 | 0.081 | 0.049 | 0.034 | 0.039 | |
| test MSE(平均) | 23.6 | 0.710 | 0.258 | 0.212 | 0.310 | 0.498 | 0.615 | |
| test MSE(中位数) | 2.69 | 0.286 | 0.176 | 0.179 | 0.302 | 0.485 | 0.598 | |
| median | 41 | 6.4 | 3.6 | 2.1 | 0.93 | 0.28 | 0.14 |
- 第 1 讲 §2.5 里 的测试 MSE 是 —— 这里的中位数 说明那是一个典型的训练集,不是运气差。
- 一点点 就天翻地覆: 已经把平均误差从 拉到 23.6。 时最好(0.212,这也回答了第 1 讲 README 的”动手改一改 3”)。再往上,bias 主导; 时 bias² = 0.486,几乎就是只剩截距的常数模型(演示 1 中 的 0.490)。
- 和 是同一个”复杂度旋钮”的两种形式:ridge 不删特征,而是让 变不大,曲线就没法在点之间剧烈摆动。但最好的 ridge(0.212)仍然远不如 时的最好模型(0.112):正则化能控制 variance,不能凭空变出数据。
1.4 Approximation vs estimation error
Lecture 9 of the 2018 course frames the same trade-off from the learning-theory side. Let be the best degree- polynomial (the projection of onto the class) and the least-squares fit. Then
exactly — a Pythagorean identity, because lies in the class and is orthogonal to it. 演示 3(每格 400 个训练集;括号里是经验公式 ):
| approx. error | estimation, | ||||
|---|---|---|---|---|---|
| 1 | 0.196 | 0.0217 (0.006) | 0.0066 (0.0018) | 0.0021 (0.0006) | 0.00064 (0.00018) |
| 3 | 0.0044 | 0.0166 (0.012) | 0.0042 (0.0036) | 0.0013 (0.0012) | 0.00039 (0.00036) |
| 5 | 0.092 (0.018) | 0.0059 (0.0054) | 0.0018 (0.0018) | 0.000565 (0.00054) | |
| 7 | 0.753 (0.024) | 0.0087 (0.0072) | 0.00255 (0.0024) | 0.00073 (0.00072) | |
| best | 3 | 5 | 5 | 5 |
- Approximation error depends only on the class; more data never reduces it. Estimation error falls like and, once , matches the rule of thumb “each parameter costs ”.
- 模型错设时规则会失效: 的 estimation error 是公式的 3.5 倍,因为逼近不了的那部分 对估计来说也像噪声(有效噪声 ≈ )。、 又是重尾。
- The optimal complexity grows with (3 → 5): with more data you can afford to pay the estimation cost of more parameters. 这是”模型应该随数据一起长大”的定量版本 —— 也是今天 scaling laws 的古典祖先。
- 和 bias–variance 的关系:bias² ≈ approximation error,variance ≈ estimation error。前者以”平均模型”为参照,后者以”类里最好的模型”为参照;后者是 §3 学习理论的语言。完整地写: Bayes error approximation estimation。
2. Double descent & implicit regularization · 双下降与隐式正则化
2.1 The puzzle
§1 的 U 形曲线说:过了最佳点,参数越多 variance 越大。可是现代神经网络的参数远多于训练样本,连随机标签都能背下来(Zhang et al., 2017),却照样泛化得很好。Belkin et al.(2019)发现:容量继续增大、越过插值阈值(interpolation threshold)之后,测试误差往往会再次下降 —— 这就是 double descent。新版 CS229 讲义(Ch. 8.2)用线性模型讲它,我们也一样:随机 ReLU 特征 , 随机生成后冻结(相当于只训练最后一层的两层网络),用 lstsq 拟合 —— 时它返回最小范数的插值解(第 1 讲 §2.3)。
2.2 The curve
代码 02_double_descent.py 演示 1:,, 固定,每个 重复 50 次(只预测 0 的模型测试误差是 1.59):
| 10 | 40 | 80 | 90 | 100 | 110 | 150 | 200 | 400 | 1000 | 3000 | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| test MSE(平均) | 1.20 | 0.785 | 1.36 | 2.625 | 854 | 2.94 | 0.970 | 0.728 | 0.544 | 0.4745 | 0.448 |
| test MSE(中位数) | 1.20 | 0.767 | 1.26 | 2.29 | 43.4 | 2.795 | 0.939 | 0.697 | 0.532 | 0.450 | 0.436 |
| median | 5.3 | 22 | 68 | 105 | 494 | 124.5 | 69 | 54 | 43 | 38 | 36 |
| median | 0.67 | 0.098 | 0.016 | 0.0075 | 0.0008 | 0.0057 | 0.019 | 0.027 | 0.040 | 0.047 | 0.049 |
| ridge | 1.19 | 0.834 | 0.772 | 0.761 | 0.774 | 0.754 | 0.742 | 0.734 | 0.718 | 0.705 | 0.702 |
- First descent and a classical U for (best at ), a spike exactly at , then a second descent: training error is zero for every , yet test error falls monotonically to 0.448 at — better than the classical sweet spot.
- At the spike, collapses and explodes; past it, both recover. 参数越多,可供选择的插值解越多,最小的那个范数越小。
- A fixed ridge removes the spike entirely — but this particular is too large for big (0.702 vs 0.448). has to be tuned (§2.4).
2.3 Why is the worst place to be
The minimum-norm solution is : the component of the noise along the -th singular direction is amplified by . 最干净的版本(演示 2):标签是纯噪声 ,特征 iid ,于是测试误差 全部是 variance,而且有精确公式:
| () | 50 | 90 | 98 | 102 | 110 | 200 | 2000 |
|---|---|---|---|---|---|---|---|
| 模拟 (400 次) | 1.01 | 10.2 | 86.3 | 111 | 11.2 | 1.01 | 0.053 |
| 公式 | 1.02 | 10.0 | 98 | 100 | 11.1 | 1.01 | 0.053 |
| median | 3.16 | 0.625 | 0.18 | 0.18 | 0.60 | 4.32 | 35.0 |
| 2.93 | 0.51 | 0.10 | 0.10 | 0.49 | 4.14 | 34.7 |
(、102 时 的分布尾巴很重,400 次的平均也不稳定。)三个区间的直觉:
- :每多一个参数,就要用同样多的数据多估一个数 —— 经典的 variance 增长。
- :方程组恰好被确定,没有任何余地:为了穿过每一个噪声点,θ 必须沿着几乎奇异的方向走很远。随机矩阵理论说 (Bai–Yin),方阵时贴着 0。
- :插值解构成一个 维的仿射空间,最小范数解在里面挑最”温和”的;多出来的维度把噪声摊薄了()。代价是:真实函数不为 0 时,“偏好小范数”本身会引入 bias —— 所以第二次下降能降到多低,取决于问题。
2.4 Does the second descent beat the first?
演示 3 换三个真实函数(每个 重复 50 次;ridge 的 按测试集挑 —— oracle,只为展示潜力):
| target | predict 0 | best | ridgeless | ridge @ | best ridge | |
|---|---|---|---|---|---|---|
| linear | 1.08 | 0.268 | 192 | 0.231 | 0.148 | 0.119 |
| lin+quad | 1.64 | 0.818 | 0.420 | 0.485 | 0.351 | |
| tanh | 0.72 | 0.394 | 0.478 | 0.262 | 0.250 |
Three targets, three answers: the overparameterized interpolant beats the classical optimum clearly (lin+quad), slightly (linear), or not at all (tanh). “Interpolation is better” is not a theorem. What is robust: the spike is caused by unregularized exact interpolation, and a tuned ridge removes it in every case (Nakkiran et al., 2021, prove that optimal regularization makes the curve monotone in some settings); the best results all come from “many features + the right ”. 实践里很少看到这么夸张的尖峰,正是因为现代训练几乎总带着某种显式或隐式的正则化。真正的教训是:参数个数不是复杂度的好度量,范数(以及优化器偏好哪种解)才是。
设计说明:最初打算在一维的
make_sine上做。一维 ReLU 特征在训练点上高度共线(拐点落在同一对相邻训练点之间的特征只张成二维空间),试跑时 远大于 也看不到第二次下降,于是改用 10 维输入。三个 target 都列出来,而不是只挑一个故事最好看的(试跑记录见代码 README)。
2.5 Implicit regularization: the optimizer picks the solution
With there are infinitely many interpolants. Which one GD finds depends on where it starts. Each GD step on least squares adds , which lies in the row space of , so the null-space component of never changes:
From this is the minimum-norm solution (第 1 讲 §2.3 用 GD 验证过)。演示 4(,,同一份数据和特征):
| 初始点尺度 | 0 | 5 | 20 | 50 |
|---|---|---|---|---|
| 的零空间分量 | 0 | 4.29 | 17.2 | 42.9 |
| 2 万步后零空间分量的漂移 | ||||
| 极限 的测试 MSE | 0.598 | 0.602 | 0.628 | 0.741 |
All four limits interpolate the training set — the training loss cannot tell them apart — yet test error grows with the distance of the start from zero. We never wrote “minimize ”; starting GD at zero did it for us. The same story holds for classification (第 1 讲 §3.5:GD on separable data converges in direction to the max-margin solution), and for deep nets the initialization scale, optimizer, step size and batch size all bias which solution you get.
两个细节:(1) 这里 GD 没有跑到收敛 —— ReLU 特征全为正,,GD 要几十万步。所以演示检验的是一个每一步都精确成立的事实(零空间漂移 ),极限用公式计算。(2) 跑了 2 万步、还没收敛的 GD,测试误差(0.593)比它的极限(0.598)还低一点:GD 先拟合大奇异值方向,最后才去拟合 方向上的噪声 —— 这就是 §4.4 的 early stopping。
3. Learning theory · 学习理论(直觉为主)
3.1 ERM and the question
Classification with 0-1 loss: and . Empirical risk minimization picks . We want close to .
微妙之处在于:对一个固定的 , 是 个独立 Bernoulli 变量的平均,会集中在 附近。但 是看过数据之后才挑出来的, 不再是独立变量的平均 —— 它系统性地偏低(正是 §5.2 的选择偏差)。解决办法是要求一致收敛(uniform convergence):对 里所有的 同时有 。这样无论 ERM 选中谁,它的训练误差都可信。
3.2 Hoeffding: one hypothesis
代码 03_learning_theory.py 演示 1 把它和精确的二项分布尾概率比较():
| 10 | 50 | 100 | 200 | 500 | 1000 | |
|---|---|---|---|---|---|---|
| exact, | 0.344 | 0.119 | 0.035 | 0.0036 | ||
| exact, | 0.070 | 0.00935 | ||||
| Hoeffding | 1.64 | 0.736 | 0.271 | 0.037 |
The bound always holds and has the right exponential rate, but it is loose (useless for ; 23× too big at ) because it only uses “each variable lies in [0, 1]” — it must also cover the worst case and ignores the variance. 推论:;、 需要 18445 个样本 —— 这也是”测试集要多大”的经验法则:精度提高 10 倍,样本多 100 倍。
3.3 Finite : union bound ⇒ sample complexity
Apply Hoeffding to each of the hypotheses and add the failure probabilities (union bound): . Setting this to :
The ERM guarantee is three lines: . Note the shape: a bigger lowers (approximation) and raises (estimation) — §1.4 again, now as a theorem. And enters only through : a class 1000 times bigger only adds to the numerator.
演示 2 用一个真实误差能精确计算的问题检验:, 再以 10% 概率翻转; = 个等间距阈值 ,。,每个设置 2000 次:
| 2 | 11 | 101 | 1001 | 10001 | |
|---|---|---|---|---|---|
| bound () | 0.148 | 0.174 | 0.204 | 0.230 | 0.254 |
| 实际 sup dev 的 95% 分位 | 0.090 | 0.120 | 0.134 | 0.135 | 0.138 |
| 0.0015 | 0.0010 | 0.0005 | 0 | 0 | |
| ERM 的平均超额误差 | 0.0002 | 0.0001 | 0.0088 | 0.0110 | 0.0112 |
上界处处成立,但它随 一直增长,实际偏差却在 之后停在 0.135 左右。union bound 把 个坏事件当成互不相关的来相加;而相邻的阈值几乎做同样的预测,偏差高度相关,“有效”的假设个数远小于 。固定 、把 从 25 增加到 1600, 每乘以 4, 和实际偏差都减半( 的速率是对的),两者之比稳定在 1.6–1.7。学习理论的界应该这样读:看它的 scaling,而不是它的常数。
3.4 VC dimension: counting what an infinite class can actually do
A set of points is shattered by if every one of the labelings is realized by some ; the VC dimension is the size of the largest shatterable set. 演示 3 精确枚举平面上半平面(2-D 线性分类器 + 截距)能实现的标注:
| 点集 | 实现的标注 / | 缺的标注 |
|---|---|---|
| 3 点,一般位置 | 8 / 8 | 无 —— shattered |
| 3 点,共线 | 6 / 8 | 010、101(中间的点和两端不同) |
| 4 点,正方形 | 14 / 16 | 1010、0101(XOR) |
| 4 点,一点在三角形内 | 14 / 16 | 1110、0001 |
| 个随机点 | 3 | 4 | 5 | 6 | 8 | 10 | 15 | 20 |
|---|---|---|---|---|---|---|---|---|
| 实现的标注数 | 8 | 14 | 22 | 32 | 58 | 92 | 212 | 382 |
| 8 | 16 | 32 | 64 | 256 | 1024 | 32768 | 1048576 | |
| Sauer 上界 | 8 | 15 | 26 | 42 | 93 | 176 | 576 | 1351 |
- VC dim of half-planes is 3: some 3 points are shattered (collinear ones are not — the definition only needs existence), and no 4 points are (Radon’s theorem). In general, linear classifiers in have VC dimension = number of parameters. 阈值类 的 VC 维是 1,这解释了 §3.3 的饱和。
- Sauer’s lemma: if VC, then on points realizes at most labelings — polynomial, not . The counts match Cover’s (1965) formula exactly.
- 于是 union bound 里的 可以换成 ,得到 Vapnik 的结果:以 的概率
样本复杂度和 VC 维成正比。对很多模型 VC 维 ≈ 参数个数,所以 Ng 课上的经验法则是”需要的样本数大致和参数个数成正比”(练习 3 给出另一个推导:把参数当成 64 位浮点数,)。
3.5 What the classical theory does and does not explain
它解释了: 远大于模型容量时,训练误差为什么可以当作测试误差的代理;为什么 或 VC 维是衡量复杂度的正确”货币”;为什么 ERM 是一致的(consistent)。它解释不了 §2:一个 参数的网络在 个样本上训练,VC 界是空洞的(vacuous),它却照样泛化。现代理论把”参数个数”换成依赖于实际找到的那个解的量 —— 范数和间隔(§2 的最小范数解、第 1 讲的 max-margin 方向)、PAC-Bayes 界,以及研究”插值带噪声的数据什么时候无害”的 benign overfitting(Bartlett et al., 2020)。一句话:经典理论把复杂度算在假设类上,现代理论把它算在算法实际找到的解上。
4. Regularization · 正则化
4.1 Ridge = MAP with a Gaussian prior — and what that prior assumes
With and prior , the negative log-posterior is ; multiplying by gives our ridge objective with . Regularization = a prior; strong regularization = a confident prior. In SVD coordinates ridge multiplies the least-squares coefficient along the -th direction by : it shrinks the low-variance directions of the data most, not each coefficient toward zero.
第 1 讲 §2.4 留了一个问题:面积和卧室数相关 0.88,卧室系数的估计方差很大(标准差约 4,真值才 10)。代码 04_regularization.py 演示 1 用和第 1 讲完全相同的 2000 份数据加 ridge(特征标准化, 一行精确复现第 1 讲):
| 真值 | 卧室系数均值 | 标准差 | RMSE | 面积系数 RMSE | test MSE | |
|---|---|---|---|---|---|---|
| 卧室 10(第 1 讲) | 0 | 9.96 | 3.95 | 3.95 | 0.0062 | 1600.7 |
| 0.003 | 10.65 | 3.86 | 3.91 | 0.0062 | 1601.2 | |
| 0.03 | 15.57 | 3.29 | 6.46 | 0.0121 | 1628.2 | |
| 0.1 | 22.23 | 2.56 | 12.49 | 0.0266 | 1760.1 | |
| 卧室 80(对照) | 0 | 79.96 | 3.95 | 3.95 | 0.0062 | 1600.7 |
| 0.03 | 78.31 | 3.24 | 3.66 | 0.0052 | 1609.2 | |
| 0.1 | 75.05 | 2.38 | 5.50 | 0.0059 | 1718.1 |
原计划的故事是”ridge 治好共线性”;实际结果是:在第 1 讲的真值下,ridge 让卧室系数更差(RMSE 3.95 → 12.49)。方差确实下降了(3.95 → 2.56),但卧室系数的平均值被往上拉到 22。原因就在上面那句话:共线时方差最小的方向是”两个标准化系数之差”,ridge 把它们往一起拉。标准化后的真系数是 [111, 14.6],相差悬殊 —— 先验”各系数差不多大、都在 0 附近”在这里是错的,偏差的方向恰好不对。对照实验把卧室的真系数换成 80(标准化后 [111, 117],几乎相等),先验对了,ridge 才帮上忙(3.95 → 3.66);即便如此改善也不大:、只有 2 个特征,方差本来就不是主要矛盾。第 1 讲那份不走运的样本被 ridge 从 −3.32 拉到 14.92(),看起来”修好了”,其实只是收缩方向碰巧和这份样本的误差方向相反。
Lesson: ridge is not a free fix for collinearity. It helps when variance dominates (many features, few samples — §1.3, §2.4, §4.3) and the prior is roughly right. Its bias is along the data’s weak directions, which is exactly where the data can’t tell you the prior is wrong.
4.2 MAP vs the full posterior
The posterior here is Gaussian, so its mode (MAP = ridge) equals its mean — but the posterior also has a covariance , which gives an uncertainty for every prediction. A full Bayesian predicts with instead of plugging in one . 演示 2(一份新的房价数据,相关系数 0.89;表中是预测均值及其标准差,单位千美元):
| 房子 | 真值 | 平坦先验(OLS):均值 ± sd | ():均值 ± sd |
|---|---|---|---|
| typical:2000 ft²,3 间 | 320 | 316.9 ± 3.1 | 321.0 ± 3.1 |
| 大而卧室少:3800 ft²,1 间 | 516 | 516.8 ± 19.6 | 452.6 ± 15.0 |
| 小而卧室多:1000 ft²,6 间 | 230 | 224.7 ± 19.5 | 288.3 ± 14.9 |
| 外推:6000 ft²,8 间 | 850 | 855.2 ± 12.4 | 816.0 ± 11.4 |
- Same model, very different confidence: the “big house with one bedroom” is 6× less certain than a typical house — the training data barely constrain the coefficients in that direction. A MAP point estimate throws this information away.
- The extrapolated house (6000 ft², far outside the data) is more certain than the 3800 ft² one-bedroom house, because it lies along the data’s main direction. Uncertainty is about how far you move in directions the data don’t constrain, not about distance from the data.
- 先验错了,后验会自信地错: 时”大而卧室少”被拉到 452.6,离真值偏了 4.2 个后验标准差。不确定性估计只在模型假设(包括先验)合理时才可信。
- 为什么还要贝叶斯?主动学习、Bayesian optimization、“模型什么时候该说’我不知道‘“都需要它。线性高斯有封闭解;神经网络只能近似(Laplace、变分推断、deep ensembles —— 第 4 讲的 bagging 是最朴素的版本)。
4.3 Lasso and sparsity
Replace the Gaussian prior by a Laplace prior and you get the L1 penalty . Why does it produce exact zeros? In one dimension, is solved by soft-thresholding : the corner of at 0 has subgradient , so as long as the data’s “pull” is below , zero is optimal. Ridge’s has no corner; its solution shrinks but never hits zero. (Geometrically: the L1 ball is a diamond whose corners lie on the axes.) Coordinate descent applies this 1-D solution to one coordinate at a time — that is glmnet.
演示 3:,30 个相邻相关()的特征,只有 5 个非零([3, −2, 1.5, 1, −1]),(测试 MSE 下限 1)。lasso 路径从 (解恰好全为 0)往下扫:
| 1.43 | 0.451 | 0.254 | 0.143 | 0.080 | 0.045 | 0.014 | 0.0025 | |
|---|---|---|---|---|---|---|---|---|
| 非零个数(真 / 假) | 3 (3/0) | 6 (5/1) | 7 (5/2) | 7 (5/2) | 14 (5/9) | 19 (5/14) | 27 (5/22) | 29 (5/24) |
| lasso test MSE | 11.8 | 3.27 | 1.78 | 1.325 | 1.19 | 1.26 | 1.97 | 3.20 |
| relaxed lasso test MSE | 3.05 | 1.08 | 1.12 | 1.15 | 1.48 | 2.08 | 3.17 | 3.55 |
对照( 都按测试集挑,只比潜力):OLS(30 个特征)3.68;最好的 ridge 2.065(ridge 没有一个系数恰好为 0);知道真实支撑集的 oracle OLS 1.08。
- 真相稀疏时 lasso 远好于 ridge(1.19 vs 2.065):Laplace 先验”大部分系数是 0”说对了。真相是”很多小系数”时结论反过来(练习 4)。
- The prediction-optimal lasso is not the selection-optimal lasso: at it keeps 9 false features. One has to do two jobs — select and shrink — and a large enough to drop the false features also shrinks the true coefficients by (4 of the 5 true coefficients come out smaller than the truth). Relaxed lasso decouples the jobs: select with lasso at a larger (6 features, 5 true), refit by OLS → 1.08, matching the oracle. But refitting at the small is worse (1.48): without shrinkage, the false features’ variance comes in at full price.
- 相关特征里 lasso 倾向于挑一个代表,挑哪个对数据很敏感;elastic net(L1 + L2)是常见的折中。
4.4 Regularizers in disguise: early stopping and data augmentation
Early stopping. GD from on least squares, after steps, has fitted a fraction of the -th singular direction; ridge fits . Both are filters that pass large- directions and block small ones, with (roughly , 练习 2)。演示 4(a):、 的多项式,200 个训练集同时跑 GD:
| GD 步数 | 10 | 30 | 100 | 300 | 1000 | 3000 | 10000 | 30000 |
|---|---|---|---|---|---|---|---|---|
| GD test MSE | 0.331 | 0.267 | 0.229 | 0.253 | 0.320 | 0.412 | 0.656 | 1.34 |
| ridge, | 0.326 | 0.258 | 0.210 | 0.208 | 0.252 | 0.332 | 0.527 | 1.02 |
Same U, traversed in the opposite direction; the correspondence is rough (the two filters have different shapes, and the valleys are offset by about one column). This is why “stop when the validation loss stops improving” is the cheapest and most widely used regularizer in deep learning — it needs no change to the loss, just a validation set.
Data augmentation. Add noise to the (standardized) inputs and average over it:
so OLS on many noisy copies converges to ridge with . 演示 4(b) 在第 1 讲那份房价数据上:每个样本复制 份、加 噪声,重复 20 次 —— 卧室系数(标准化坐标)的平均值 18.47 / 19.26 / 19.58 / 19.60( = 1 / 10 / 100 / 1000)趋向 ridge 的 19.62(OLS 是 −4.68),离 ridge 的 RMS 距离按 缩小(6.76 → 0.22)。“对输入的小扰动不敏感”和”惩罚 “是一回事;dropout(Wager et al., 2013 证明它近似一种自适应的 L2)、随机裁剪、mixup 都是这个思路的推广。反过来也成立:代码里的 fit_ridge 就是往训练集里加 行假数据(第 行只有第 个特征为 ,标签为 0)来实现 ridge 的。正则化 = 把先验知识写成数据或写成损失,两种写法等价。
5. Model selection · 模型选择
5.1 Hold-out, k-fold, LOOCV
- Hold-out:留出 20–30% 当验证集,在剩下的数据上训练每个候选模型,挑验证误差最小的,再用全部数据重新训练。简单,但浪费数据,而且验证误差本身的方差大。
- k-fold CV:分成 折,每折轮流当验证集,平均 个验证误差。估计的是”用 个样本训练”的误差: 小 → 训练集小 → 偏悲观; 大 → 偏差小,但各折训练集几乎相同,估计的方差可能更大,计算也更贵。
- LOOCV():一般要训练 次;但对 ridge 这样的线性平滑器 ,留一残差恰好是 ,训练一次就够(
gen_tools.ridge_loocv; 叫 leverage)。
代码 05_selection_diagnostics.py 演示 1: 的多项式、,用 CV 在 17 个 ( 到 10)里选一个,200 个数据集:
| 方法 | 选中 λ 的 test MSE(中位数) | (平均) | 翻车率 oracle | 中位 |
|---|---|---|---|---|
| 2-fold | 0.138 | 0.179 | 0.100 | −2.5 |
| 5-fold | 0.135 | 1.08 | 0.055 | −3.0 |
| 10-fold | 0.137 | 2.94 | 0.100 | −3.0 |
| LOOCV | 0.136 | 2.96 | 0.110 | −3.0 |
| 10-fold + 1-SE rule | 0.146 | 0.162 | 0.055 | −2.5 |
| oracle(按测试集挑) | 0.114 | 0.119 | – | – |
- 典型情况下各种方法都选得不错(中位数 0.135–0.138)。平均值被少数”翻车”主导:CV 选了极小的 λ,测试误差爆炸。CV 为什么看不见?它只在训练输入所在的位置检查预测,而爆炸发生在 附近没有训练点的空白处。在演示 (a) 的那个数据集上,LOOCV 在 处报告 0.097,真实测试误差却是 23.9。
- 2 折偏悲观、偏向更大的 λ —— 在这里反而成了保护(平均 0.179)。
- The one-standard-error rule (ESL §7.10): among ‘s whose CV error is within one SE of the minimum, take the simplest. It gives up a little in the typical case (0.146) and has the best mean (0.162). It acknowledges that the CV curve itself is noisy.
5.2 The optimism of the winner, and nested CV
Selecting the best of noisy scores is itself a fit. 演示 2 用最极端的版本:纯噪声,,, 个与 无关的特征;候选模型 = 只有截距,或截距 + 一个特征。用 5 折 CV 挑最好的,重新拟合后它的真实 MSE 可以精确算出()。200 个数据集的平均:
| 1 | 10 | 100 | 1000 | |
|---|---|---|---|---|
| winner 的 CV 分数 | 1.0223 | 0.9905 | 0.9081 | 0.8489 |
| winner 的真实 MSE | 1.0297 | 1.0841 | 1.1807 | 1.2633 |
| nested CV 估计 | 1.0432 | 1.1235 | 1.1975 | 1.3225 |
| 选中”只有截距”的比例 | 0.740 | 0.115 | 0 | 0 |
时,被选中的模型看起来”解释了 15% 的方差”,实际上比只预测均值还差。 个带噪声的估计里取最小值,大约会比典型值低 (练习 5)—— 和 §3.3 union bound 里的 是同一个东西:验证集本身就是一个”有限假设类”的学习问题,你看过的每一个假设都要付费。Nested CV 把”选择 + 重新拟合”当成一个整体算法,在外层循环里评估它,给出诚实的估计(略偏悲观,因为外层每次只用 4/5 的数据)。Kaggle leaderboard 过拟合、在测试集上调参、p-hacking,说的都是这张表。
实践规则:测试集只在最后碰一次。任何包含”选择”(选 λ、选特征、选架构)的流程,都应该报告 nested CV(或一个全新的 hold-out 集)上的数字。
5.3 The Bayesian view of model selection(简述)
Bayesians select models by the marginal likelihood : a flexible model spreads its prior over many datasets, so it assigns less probability to the one you observed unless it needs the flexibility — an automatic Occam’s razor. Its large- approximation is BIC ; AIC estimates out-of-sample log-likelihood instead. Both penalize parameter count, so both inherit §3.5’s caveat for overparameterized models. In everyday practice CV wins because it makes no assumption about the model being correct.
6. Advice for applying ML · 怎样决定下一步
Ng 在 L13 和 “Advice for applying machine learning” 里的核心论点:与其凭直觉试(更多数据?更多特征?换模型?),不如先做诊断,弄清楚问题出在哪。 每一种”修复”只对一种问题有效,用错了就白干几个月。
6.1 Learning curves: high bias or high variance?
Plot training and validation error against training-set size . 演示 3(200 个训练集的中位数;目标是测试 MSE ≤ 0.12,噪声下限 0.09):
| 20 | 40 | 80 | 160 | 320 | 640 | |
|---|---|---|---|---|---|---|
| line()train / test | 0.2465 / 0.3171 | 0.2699 / 0.3035 | 0.2741 / 0.2974 | 0.2795 / 0.2947 | 0.2855 / 0.2933 | 0.2846 / 0.2924 |
| ,不加正则 | 0.0272 / 56.23 | 0.0593 / 0.2196 | 0.0738 / 0.1131 | 0.0827 / 0.0984 | 0.0852 / 0.0941 | 0.0873 / 0.0918 |
| ,ridge | 0.0648 / 0.1394 | 0.0782 / 0.1140 | 0.0851 / 0.1010 | 0.0906 / 0.0968 | 0.0923 / 0.0953 | 0.0932 / 0.0946 |
| 症状 | 诊断 | 有用的修复 | 没用的修复 |
|---|---|---|---|
| train ≈ test,都高于目标(直线) | high bias | 更多 / 更好的特征、更大的模型、减小正则化 | 更多数据(n 从 80 到 640,0.297 → 0.292) |
| train ≪ test,差距随 n 缩小() | high variance | 更多数据、更少的特征、更强的正则化 | 更大的模型 |
Two reading tips: the gap diagnoses variance, and the training error relative to the target diagnoses bias — if even the training error misses the target, more data cannot help, because training error usually only rises with . Ridge at is “a prior worth some data”: it matters enormously at (0.139 vs 56.2), and at it is even slightly worse (0.0946 vs 0.0918) — its small bias now outweighs the variance it removes.
6.2 Is it the optimizer or the objective?
Ng 的例子:用 Bayesian logistic regression(= 带高斯先验的逻辑回归)做垃圾邮件过滤,误删正常邮件的比例高得不可接受;一个线性 SVM 在你真正关心的指标上更好,但出于别的原因你想继续用逻辑回归。问题出在优化算法(没把 优化好)还是目标函数( 本身不对)?诊断只需要一次比较:用 A 自己的目标 去评价 B 的参数。
- (我们的 是损失,越小越好):B 在 A 自己的目标上都赢了 A ⇒ A 的优化器没完成任务。
- : 认为 A 更好,而真正的指标 认为 B 更好 ⇒ 和 不一致 ⇒ 目标函数有问题。
演示 4 的玩具版:误删正常邮件代价 10、漏掉垃圾邮件代价 1, = 测试集平均代价。A = 不加权的逻辑回归,用 GD 训练(特征没标准化,条件数很差);B = 正常邮件权重 10 的加权逻辑回归:
| 参数 | (A 的目标,train) | (代价,test) | FP rate | FN rate | 诊断 |
|---|---|---|---|---|---|
| B:加权 LR | 0.3590 | 0.202 | 0.012 | 0.282 | 参照 |
| A:GD 5 步 | 0.3616 | 0.874 | 0.160 | 0.152 | optimizer |
| A:GD 50 步 | 0.3547 | 0.873 | 0.159 | 0.152 | objective |
| A:GD 5000 步 | 0.3142 | 0.743 | 0.136 | 0.129 | objective |
| A:Newton(收敛) | 0.1991 | 0.448 | 0.081 | 0.082 | objective |
Once the optimizer is fixed (Newton), the diagnosis is unambiguous: A minimizes its , yet its cost is twice B’s — unweighted log loss treats both errors as equally bad, and we don’t. Fix the objective (weighted loss, or move the decision threshold), not the optimizer.
A subtlety the toy exposed: the two outcomes are not symmetric. After 50 steps GD is far from converged ( vs optimum 0.1991), yet the diagnosis already says “objective”. "" proves an optimizer problem; "" proves that and disagree, but not that the optimizer is fine — both problems can coexist. 修好一个之后要再诊断一次。(原计划是”50 步 = optimizer 问题”的演示;实际输出不支持,于是改成扫步数,把这个不对称性写了进来。)同样的思路第 6 讲还会用到:RL 里是模拟器、算法还是奖励函数的问题?
6.3 Error analysis and ablative analysis
Error analysis asks: which component is worth improving? For a pipeline, replace one component’s output by ground truth and see how much the final metric improves. 演示 5:组件 A 估计潜变量 (噪声 1.0),组件 B 估计 (噪声 0.3),最后的逻辑回归预测 :
| 换成真值的组件 | 无 | A | B | A + B |
|---|---|---|---|---|
| 测试准确率 | 0.7367 | 0.8445 | 0.7433 | 0.8504 |
改进 A 最多能涨 10.8 个百分点,改进 B 最多 0.65 个 —— 即使 B 也不完美,也该先改 A。“哪个组件误差最大”不是正确的问题:组件误差对最终指标的影响取决于下游怎么用它;error analysis 直接量这个影响。Ng 的人脸识别例子(预处理 → 人脸检测 → 眼 / 鼻 / 嘴分割 → 逻辑回归)就是沿着流水线逐个插入真值。
Ablative analysis asks the reverse: which existing components actually help? Remove them and measure the drop. 演示 6:A 和 A′ 是同一个 的两个独立测量(冗余),B 测 ,junk 是 10 个噪声特征;全部特征 0.795:
| 去掉 | A | A′ | B | junk | A + A′ |
|---|---|---|---|---|---|
| 准确率变化 | −0.0268 | −0.0281 | −0.0475 | +0.0013 | −0.1750 |
Leave-one-out ablation says B is the most important group — yet ‘s true coefficient is twice ‘s. A and A′ cover for each other, so removing either alone barely hurts; removing both costs 17.5 points. Cumulative ablation (Ng’s style: remove one group after another) is order-dependent for the same reason: in the order junk → A′ → B → A, A′ “costs” 2.8 points and A “costs” 22.4. Redundancy makes ablation tables lie: state the order, and report pairwise removals for components that might be redundant.
Ng 的总建议:先快速做一个粗糙的系统(quick-and-dirty),然后用上面的诊断决定把时间花在哪里,而不是一开始就精心设计每个组件(“premature statistical optimization”)。
7. 小结 · Summary
| 概念 | 一句话 | 本讲的证据 |
|---|---|---|
| Bias–variance | 期望测试误差 = + bias² + variance,恒等式 | 公式和直接测量相差约 0.1%; 最好;高次的 variance 重尾 |
| Approx. vs estimation | 前者只看假设类,后者 ≈ 每个参数 | 最优次数随 从 3 增到 5 |
| Double descent | 时 ,噪声被放大 | 尖峰 854; 降到 0.448;调好的 ridge 没有尖峰 |
| Implicit regularization | GD 不改变零空间分量 ⇒ 从 0 出发 = 最小范数解 | 漂移 ;起点越远测试误差越差 |
| Uniform convergence | ;ERM 差 不超过 | 界成立;随 涨,实际偏差在 VC 维 1 处饱和 |
| VC dimension | 能 shatter 的最大点集;Sauer: 种标注 | 半平面:3 点 8/8,4 点 14/16,20 点 382 种 |
| Ridge / lasso | 高斯 / Laplace 先验的 MAP;ridge 沿弱方向收缩,lasso 产生 0 | 先验不对时 ridge 更差;lasso 1.19 vs ridge 2.065 |
| Early stopping / augmentation | 都是隐藏的 ridge | U 形对应 ;增强 → ridge, |
| CV & selection | CV 选得典型地好,但取最小值是乐观的 | 1-SE 规则平均最好;纯噪声上 CV 0.85、真实 1.26 |
| Diagnostics | 先诊断再修:bias/variance、optimizer/objective、组件 | 学习曲线;诊断的不对称性;冗余让消融表失真 |
Six takeaways:
- Bias and variance only exist across training sets. Resampling makes them visible; one training set can’t. And variance can be heavy-tailed — “expected” and “typical” test error can differ by orders of magnitude.
- Parameter count is a poor measure of complexity. What matters is the norm of the solution and which solution the optimizer prefers (min-norm, max-margin). Exact interpolation at is the danger zone, not .
- Learning-theory bounds are for scaling, not numbers: , , VC dimension. They are valid and loose.
- Every regularizer is a prior, and a prior can be wrong. Ridge shrinks along the data’s weak directions; when the truth isn’t small there, it adds bias exactly where the data can’t correct it.
- Selection is fitting. The best of validation scores is optimistic by ; use nested CV or an untouched test set.
- Diagnose before fixing. Learning curves, the optimizer-vs-objective test, error analysis and ablation each answer a different “what next?” — and each has failure modes (asymmetry, redundancy).
8. 常见误区 · Common pitfalls
- “Low training error means a good model.” Training error falls monotonically with complexity (§1.2, §6.1). It measures fit, not learning.
- “More parameters always means more overfitting.” Past the interpolation threshold, min-norm solutions can get better with more parameters (§2.2) — though not always better than the classical optimum (§2.4).
- “Ridge fixes collinearity.” It reduces variance but biases correlated coefficients toward each other; with a very unequal truth it made the estimate worse (§4.1).
- “The CV score of the selected model is an estimate of its test error.” It is optimistic; the more models you compared, the more so (§5.2).
- “LOOCV is always the best CV.” It has the least bias but can be unstable, and like all CV it only checks predictions where the training inputs are (§5.1).
- “If the diagnostic says ‘objective’, the optimizer is fine.” The test is asymmetric; both can be broken (§6.2).
- “Removing a component barely hurt, so it’s useless.” Not if another component is redundant with it (§6.3).
9. 练习 · Exercises
练习 1(Bias–variance identity). (a) Prove the decomposition in §1.1, showing each cross term vanishes. (b) For -nearest-neighbour regression with fixed training inputs, , show that the variance at is and write the bias. Which way does move the trade-off?
参考答案
(a) 。三个交叉项:( 均值为 0, 是常数);(测试噪声和训练集独立);。(b) ;bias ,邻居越远越大。 大 → variance 小、bias 大: 和 LWR 的带宽 (第 1 讲 §2.5)是同一种旋钮。
练习 2(Early stopping as a spectral filter). For , GD from 0 with step . Show , compare with ridge’s filter , and argue why .
参考答案
在 的坐标下各分量独立:,不动点 ,误差每步乘以 ,从 出发得到上式。令 :GD 的滤波 ,ridge 的滤波 。两者都在 (或 )时 ≈ 1,在 时 ≈ (或 )。令两个转折点重合:,即 。形状不同(指数 vs 有理函数),所以 §4.4 的两条曲线谷底错开了一点。零空间方向()两者都不碰 —— 这就是 §2.5。
练习 3(Parameters as 64-bit floats). A model has real parameters, each stored as a 64-bit float. (a) Bound and give the sample complexity for , . (b) Evaluate for , , . (c) Why is this argument both reassuring and unsatisfying?
参考答案
(a) ,所以 。(b) 。(c) 让人放心:样本复杂度线性于参数个数(和 VC 维的结论一致),这是 Ng 的经验法则的来源。不满意:它依赖于浮点表示这种与学习无关的细节;常数很大;而且对 的现代模型完全失效(§3.5)—— 真正决定泛化的是算法找到的解,不是可表示的假设有多少。
练习 4(When does ridge beat lasso?). With an orthonormal design (), the least-squares coefficients are ; show ridge gives and lasso gives . Use this to explain why lasso wins when the truth is sparse with a few large coefficients, and ridge wins when the truth has many small coefficients of similar size. (Code version: 动手改一改 4 in the README.)
参考答案
正交设计下目标函数按坐标分解: 的解是 , 的解是软阈值。稀疏真值:大部分 只是噪声,软阈值把它们整个去掉(variance 归零),少数大系数只付出固定的 偏差;ridge 对每个噪声坐标只按比例缩小,留下 的 variance。稠密的小系数:真系数和噪声大小相当,软阈值会把很多真系数也砍成 0(bias = 整个系数),而 ridge 按比例保留每一个的一部分。贝叶斯说法:Laplace 先验押注”大部分为 0”,高斯先验押注”都小但都不为 0”,谁的先验对谁赢。
练习 5(Why ). Let be , not necessarily independent. Show . Relate this to the optimism in §5.2 and to the in §3.3.
参考答案
对任意 ,由 Jensen:,所以 ,取 得证。“把 max 换成 sum”正是 union bound 的思路,所以同一个 出现在两处。§5.2 里每个候选模型的 CV 分数 = 真实误差 + 估计噪声;在 个噪声里取最小值,会比典型值低约 ( 是噪声的标准差)。只要 大到 超过真实的差距,选择就主要由噪声决定 —— 纯噪声数据上 时,“只有截距”再也没被选中过。注意上界不需要独立性;相关的分数(比如共用同一个 )实际的乐观偏差更小。
练习 6(Reading the optimizer-vs-objective test). (a) Explain in your own words why "" does not imply that A’s optimizer converged. (b) In demo 4 the costs are 10 (false positive) vs 1 (false negative). If A’s probabilities were perfectly calibrated, what threshold minimizes expected cost? Is changing the threshold a change of “objective” or of “optimizer”?
参考答案
(a) 这个比较只涉及两个点 、。 只说明”在 的排序下 A 不比 B 差”,再加上 ,就说明 和 的排序矛盾 —— 目标函数有问题。但 离 的最优点可能还很远(演示 4 的 50 步:0.3547 vs 0.1991),只要 B 的 更差,比较就看不出来。(b) 判为垃圾邮件的期望代价是 ,判为正常的是 ;当 时才判为垃圾。改阈值改的是决策规则,属于”目标”那一侧:训练出的概率模型不变,变的是你如何把概率变成行动(README 动手改一改 6 可以验证它能追上加权逻辑回归多少)。
代码相关的练习(“动手改一改”)见 code/lecture03/README.md。
10. 延伸阅读 · Further reading
- CS229 main notes(Ng & Ma),Ch. 8(bias–variance、double descent、sample complexity bounds)和 Ch. 9(regularization、implicit regularization、cross-validation、Bayesian statistics):本讲的主要来源。
- CS229 learning theory notes(Ng):Hoeffding、union bound、有限 / 无限假设类、VC 维的原版推导。
- Andrew Ng, “Advice for applying machine learning”(CS229 讲稿)以及 Machine Learning Yearning:§6 的诊断方法。
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, Ch. 3(ridge / lasso)、Ch. 7(bias–variance、CV、one-standard-error rule、AIC/BIC)。
- Belkin, Hsu, Ma & Mandal, “Reconciling modern machine learning practice and the bias-variance trade-off”, PNAS 2019;Nakkiran et al., “Deep Double Descent”, ICLR 2020;Nakkiran, Venkat, Kakade & Ma, “Optimal Regularization Can Mitigate Double Descent”, ICLR 2021。
- Hastie, Montanari, Rosset & Tibshirani, “Surprises in High-Dimensional Ridgeless Least Squares Interpolation”, Annals of Statistics 2022:§2.3 公式的精确渐近版本。
- Zhang, Bengio, Hardt, Recht & Vinyals, “Understanding deep learning requires rethinking generalization”, ICLR 2017;Bartlett, Long, Lugosi & Tsigler, “Benign overfitting in linear regression”, PNAS 2020。
- Tibshirani, “Regression Shrinkage and Selection via the Lasso”, JRSS-B 1996;Friedman, Hastie & Tibshirani, “Regularization Paths for GLMs via Coordinate Descent” (glmnet), JSS 2010。
- Wager, Wang & Liang, “Dropout Training as Adaptive Regularization”, NeurIPS 2013:§4.4 的推广。
- Shalev-Shwartz & Ben-David, Understanding Machine Learning, Ch. 2–6:学习理论最清楚的教材。
11. 下一讲预告 · Next lecture
Lecture 4:非线性模型 —— 决策树、集成与神经网络(Trees, ensembles & neural networks)。
本讲的模型都对参数是线性的,非线性全靠手工特征(多项式、随机特征)。第 4 讲换一种思路:让模型自己学特征。决策树贪心地切分输入空间 —— 可解释,但 variance 极高;于是 §1 的分解立刻派上用场:bagging 把 个高方差的树平均,方差变成 ,random forest 再用特征子采样降低相关性 ;boosting 则反过来,一步步减小 bias。最后从 GLM(第 1 讲 §4.7)走到多层感知机:学出来的特征 + 一个 GLM 输出头,用 backprop 训练 —— 本讲的 double descent、implicit regularization 和 early stopping,在那里会以更大的规模再出现一次。