L1/L2 正则项

🔍 L1 / L2 Norm and Penalty Calculator

Compute the L1 and L2 norms of a weight vector together with the matching penalty terms, to build intuition about sparsity and shrinkage.

📐 计算公式 / 原理
L1/L2 范数(本页代码口径,四个权重 w1–w4):L1=|w1|+|w2|+|w3|+|w4|;L2=√(w1²+w2²+w3²+w4²),两项均保留 3 位小数。校验:无强校验,空值按 0 兜底。判据:无固定分级,范数随权重绝对值单调增长,权重全零时 L1 与 L2 同时为 0。
两个范数量化权重矩阵的整体规模:L1 反映绝对值总量,对少量大权重敏感;L2(Frobenius 范数)反映能量,常被直接写进损失作为惩罚项。监控范数随训练的变化能快速判断是否出现权重爆炸。

📐 计算公式与说明

判读与对照(本页代码口径,与上方公式互补):由范数不等式恒有 L1≥L2,等号仅当至多一个权重非零时成立;权重同乘 c 后两范数均乘 |c|(一次齐次);对照图按各权重绘制 |w| 与 w² 两列——|w|<1 时 w² 更小、|w|>1 时 w² 更大,可直观看出 L1 由大权重主导而 L2 相对压低大权重贡献。校验:与上方共用同一组输入。判据:无固定分级。

L1 倾向稀疏,L2 倾向小权重。

📚 深度解析:L1/L2 正则项

💡 常见使用场景

范数计算
w=[3,−4]:‖w‖₁=|3|+|−4|=7;‖w‖₂=√(9+16)=5,L2 惩罚=(λ/2)·25。L1 对大权重同等惩罚,L2 更压大权重。

❓ 常见问题(FAQ)

L1 为什么能稀疏?
L1 在零点不可导、等高线为菱形,最优解易落在坐标轴上使部分权重恰为 0,实现特征选择;L2 只缩小不置零。
正则系数 λ 怎么选?
用验证集网格/贝叶斯搜索;λ 过小过拟合、过大欠拟合。通常对权重先标准化,再在 10⁻⁴~10 量级扫描。

Formula & Standard References

Item Formula / Note Source / Standard
L1 penalty the L1 penalty adds the sum of absolute weights and yields sparse solutions Tibshirani 1996, JRSS-B
L2 penalty the L2 penalty adds the sum of squared weights and shrinks large weights Hoerl & Kennard 1970, Technometrics
Strength coefficient a larger penalty coefficient tightens the constraint and over-regularises when too large Hastie et al., Elements of Statistical Learning