L1 和 L2 regularization 有什么区别?什么时候用谁?
为什么 L1 会得到 sparse 解?λ 怎么选?
先弄清楚:每个概念是什么
- Regularization
- loss = data loss + \(\lambda\) × penalty。目的是防 overfitting(见 overfit / bias-variance)。
- L2(Ridge、weight decay)
- 罚平方和 \(\lambda\sum_j w_j^2\)。大 weight 重罚、小 weight 几乎不罚,所以都被压小,但不会正好到 0。 例:\(w = 3\) 罚 9,\(w = 0.1\) 只罚 0.01。
- L1(Lasso)
- 罚绝对值之和 \(\lambda\sum_j |w_j|\)。不管 weight 大小都按同一个单价罚,用处不大的 feature 直接被砍成 0。 例:\(w = 0.1\) 也要罚 0.1,比 L2 狠 10 倍。
- Sparse 与 feature selection
- 大部分 weight 正好是 0,相当于自动做了 feature selection。
- \(\lambda\)(sklearn 里叫 alpha)
- penalty 的力度,太小 overfit,太大 underfit。
30 秒回答
Both add a weight penalty to the loss: L1 adds λ·Σ|w|, L2 adds λ·Σw². L2 shrinks all weights but almost never to exactly zero; L1 sets many to exactly zero, so it also selects features. L1's gradient has constant magnitude λ however small the weight is, so it pushes the weight all the way to zero; L2's gradient, 2λw, fades as the weight shrinks. Geometrically, the L1 ball has corners on the axes. As priors, L2 is Gaussian and L1 is Laplace. I'd use L1 when few features matter, L2 by default or with correlated features, elastic net for both, and pick λ by cross-validation.
一张表
| L1(Lasso) | L2(Ridge / weight decay) | |
|---|---|---|
| penalty | \(\lambda\sum_j |w_j|\) | \(\lambda\sum_j w_j^2\) |
| penalty 的 gradient | \(\lambda\,\operatorname{sign}(w)\),大小恒为 \(\lambda\) | \(2\lambda w\),跟着 \(w\) 一起变小 |
| 效果 | sparse,自动 feature selection | 都变小,都不为 0 |
| 相关的 feature | 随便留一个,不稳定 | 平分 weight,稳定 |
| MAP 的 prior | Laplace | Gaussian |
推导
为什么 L1 会到 0:只看一个 weight
不加 regularization 时最优解是 \(a\),loss 在附近近似为 \(\tfrac12(w - a)^2\),L2 项写成 \(\tfrac{\lambda}{2}w^2\):
\[ \text{L2:}\quad \min_w\ \tfrac12(w-a)^2 + \tfrac{\lambda}{2}w^2 \;\Longrightarrow\; w = \frac{a}{1+\lambda} \] \[ \text{L1:}\quad \min_w\ \tfrac12(w-a)^2 + \lambda|w| \] \[ \Longrightarrow\; w = \operatorname{sign}(a)\,\max(|a| - \lambda,\ 0) \]\(|w|\) 在 0 处的 subgradient 是 \([-1, 1]\),所以只要 \(|a| \le \lambda\),\(w\) 就停在正好 0。L2 只有 \(a = 0\) 时才得到 0。
手算,\(\lambda = 1\):\(a = 3\) → L2 得 1.5,L1 得 2;\(a = 0.5\) → L2 得 0.25,L1 得 0。L2 按比例缩,L1 每个都减 \(\lambda\)、减到 0 为止。
几何与 MAP
等价于在 \(\|w\|_1 \le B\) 里最小化 loss。等高线往外扩,最容易先碰到菱形在坐标轴上的角,角上某些坐标正好是 0。L2 的圆没有角(Bishop Fig. 3.4)。MAP 的推导见 regression。
λ 怎么选
- 先 standardize 每个 feature,因为所有 weight 共用一个 \(\lambda\)。intercept 不罚。
- 取一串按 log 等距的 \(\lambda\),每个做 5-fold CV,算平均 validation MSE。
- 选 CV MSE 最小的 \(\lambda\)(想更 sparse 可以用 one-standard-error rule 选更大的),再在全部数据上重新训练。
例:100 个样本、20 个 feature,只有前 3 个有用,noise \(\sigma = 1\)。Lasso:
| \(\lambda\)(alpha) | 0.01 | 0.03 | 0.1 | 0.3 | 1 |
|---|---|---|---|---|---|
| 5-fold CV MSE | 1.19 | 1.13 | 1.08 | 1.40 | 4.93 |
| 非零 weight 个数 | 20 | 18 | 7 | 3 | 3 |
选 \(\lambda = 0.1\),留下 3 个真的和 4 个很小的 weight,其余 13 个正好是 0。\(\lambda = 1\) 把有用的 weight 也压得太小,是 underfit。
必答点
- L1 = \(\lambda\sum|w|\),L2 = \(\lambda\sum w^2\);\(\lambda\) 太大 → underfit
- L1 → sparse、feature selection;L2 → 都变小、不为 0
- 为什么 sparse:gradient 大小恒为 \(\lambda\) vs \(2\lambda w\);一维解是 soft-thresholding;菱形有角
- MAP:L2 = Gaussian prior,L1 = Laplace prior
- 少数 feature 有用 → L1;默认、feature 相关 → L2;两样都要 → Elastic Net;\(\lambda\) 用 CV 选
追问
Feature 高度相关时 L1 会怎样?
把同一个 feature 复制一份,Lasso 给这两列 (1.89, 0),Ridge 给 (1.00, 1.00)。Elastic Net 会把相关的 feature 一起留下(Murphy §11.4.8)。
Weight decay 和 L2 regularization 是一回事吗?
SGD 下是,差一个 learning rate 的缩放。Adam 下不是,所以有 AdamW,见 optimizer。
L1 在 0 处不可导,怎么优化?
常用 coordinate descent,每次只更新一个 weight,一维的解就是上面的 soft-thresholding。也可以用 proximal gradient,走一步 gradient 再做一次 soft-thresholding。
为什么不直接罚非零 weight 的个数(L0)?
L0 是 non-convex 的,等于要在所有 feature 子集里挑。L1 是 L0 最紧的 convex relaxation,既 sparse 又好解(Murphy §11.4.1)。
来源Murphy, PML1 §4.5.3–4.5.5(pp.123–126)、§11.3(pp.381–384)、§11.4.1–11.4.3(pp.385–389)、§11.4.8–11.4.9(pp.396–397)· Bishop, PRML §3.1.4(pp.144–146)· Goodfellow et al., Deep Learning §7.1.1–7.1.2(pp.227–233)· Loshchilov & Hutter, AdamW