统计与机器学习 / 15

偏差、方差与正则化

从假设、目标到模型和评估。 偏差、方差与正则化。

阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。

1. Overfitting

Fundamental causes of overfitting:

  • Complicated Model;
  • Limited learning data/lables

Two types of error:

  • Loss里的error:(手段)
    针对训练数据:观测到的训练数据里的y和模型预测值之前的差别
  • 预测error(Model Error):(目标)
    针对未来的未知数据: 未来的y和模型之间的差别

2. Model Error

2.1 定义

在未来未知数据(test数据)上的error(MSE)

Mean Square Error=1n∑i=1n(yi−f(xi))2\text{Mean Square Error} = \frac{1}{n} \sum^{n}_{i=1}(y_i-f(x_i))^2

Model error与loss区别:

  • Loss function用来构建模型
  • model error用来检测模型效果

2.2 Expected error of regression models:

E(Error)=bias2+variance+Irreducible  errorE(Error)=bias^2+variance+ Irreducible\;error

Error的期望组成:

  • error: 自己model的y’与真实值y之间差异
  • bias:measures how far off in general the models’ predictions are from the correct value
    最优model的y^\hat{y}与真实值y间差异
  • variance: is how much the predictions for a given point vary between different realizations of the model
    许多的model的y’与最优的model的y^\hat{y}间的平均差异

2.3 理解bias和variance

偏差与方差: 固定x,考虑多次重新抽训练集得到的预测。其平均预测与真实回归函数f∗(x)f^*(x)的差是偏差;不同训练集预测围绕平均预测的波动是方差。不是把某个训练出来的模型当作不可知的“最优模型”。

  • 模型的精确性bias
    • model在训练数据有变换下的平均输出结果与真实值相比,得到的平均准确性
  • 模型的稳定性variance
    • 第一层:模型输出结果的稳定性
    • 第二层:“某一次model的数据结果与这个model的平均水平的差距”的平方的期望

2.4 The trade-off between Bias and Variance

容量与误差示意: 容量太小常见训练/验证误差都高;容量增加后训练误差可持续降低,验证误差可能先降后升。两者差距提示泛化问题,但不是对任意数据都成立的定量曲线。

2.5 Bias vs. Variance

预测中心波动小波动大
接近真实目标低偏差、低方差低偏差、高方差
偏离真实目标高偏差、低方差高偏差、高方差
\leftright
uplow bias, low varlow bias, high var
downhigh bias, low varhigh bias, high var

(Advanced):

估计量的平方误差分解:

E[(θ^−θ)2]=Var(θ^)+(E[θ^]−θ)2.E[(\hat\theta-\theta)^2]=Var(\hat\theta)+(E[\hat\theta]-\theta)^2.

展开时交叉项含E[θ^−Eθ^]=0E[\hat\theta-E\hat\theta]=0而消失。回归预测误差再加不可约噪声项。

2.6 How to solve the overfitting problem?

  • Increase training data size
  • Avoid over-training dataset:
    • Filter out features, e.g. feature reduction
      Principal component analysis (PCA)
    • Regularization
      Ridge regression, Least absolute shrinkage and selection operator (LASSO)
      Logistic Regression-L2, Logistic Regression-L1
    • Ensemble Learning

3. Regularization

Ridge Regression and LASSO (Least absolute shrinkage and selection operator) are all regularization methods.

3.1 Hyperparameter Optimization

下面L2 penalty公式中λ\lambda就是Hyperparameter。

通过修改λ\lambda的值可以使得在var和bias间达到平衡:随着λ\lambda增大,模型var减少而bias增大

λ\lambda值的确定一般是通过尝试的方法得到:利用含有label data的training data去做cross-validation,来判断最好λ\lambda取值。

3.2 Ridge Regression

Choose model -> calculate loss function -> Numerical Optimization

Ridge Regression improves the loss function definition of Linear Regression by adding an L2 coefficient penalty to the objective.

Regularization is nothing but adding a penalty term to the objective function and controlling the model complexity using that penalty term.

Ridge Regression: L2 penalty in loss function

  • Traing stage:
Loss=∑i=1n(yi−β0−∑j=1pβjxij)2+λ∑j=1pβj2Loss = \sum^{n}_{i=1}(y_i-\beta_0-\sum^p_{j=1}\beta_j x_{ij})^2+\lambda\sum^p_{j=1}\beta^2_j
  • Testing stage:
MSE=1m∗∑i=1m(yi−β0−∑j=1pβjxij)2MSE=\frac{1}{m}*\sum^m_{i=1}(y_i-\beta_0-\sum^p_{j=1}\beta_j x_{ij})^2

Ridge regression就是llinear regression加上平方项的penalty term

3.3 LASSO

LASSO: L1 penalty in loss function

Loss=∑i=1n(yi−β0−∑j=1pβjxij)2+λ∑j=1p∣βj∣Loss = \sum^{n}_{i=1}(y_i-\beta_0-\sum^p_{j=1}\beta_j x_{ij})^2+\lambda\sum^p_{j=1}|\beta_j|

LASSO就是linear regression加上绝对项的penalty term

3.4 Logistic Regression + Regularization

hβ(x)=11+e−βTxh_\beta(x)= \frac{1}{1+e^{-\beta^T x}}
  • L1-Regularization
argminβ∑i=1n[−yilog⁡(hβ(xi))−(1−yi)log⁡(1−hβ(xi))]+λ∣∣β∣∣1argmin_\beta\sum^n_{i=1}{[-y_i\log(h_\beta(x_i))-(1-y_i)\log(1-h_\beta(x_i))]+\lambda||\beta||_1}
  • L2-Regularization
argminβ∑i=1n[−yilog⁡(hβ(xi))−(1−yi)log⁡(1−hβ(xi))]+λ∣∣β∣∣22argmin_\beta\sum^n_{i=1}{[-y_i\log(h_\beta(x_i))-(1-y_i)\log(1-h_\beta(x_i))]+\lambda||\beta||^2_2}

范式Norm p:

  • ∣∣β∣∣p=(∑j=1n∣βj∣p)1/p||\beta||_p=(\sum^n_{j=1}|\beta_j|^p)^{1/p}
  • ∣∣β∣∣pp=(∑j=1n∣βj∣p)1p∗p=(∑j=1n∣βj∣p)||\beta||_p^p=(\sum^n_{j=1}|\beta_j|^p)^{\frac{1}{p} * p}=(\sum^n_{j=1}|\beta_j|^p)

Example:

  • ∣∣β∣∣1=∑j=1n∣βj∣||\beta||_1=\sum^n_{j=1}|\beta_j|
  • ∣∣β∣∣22=∑j=1n∣βj∣2||\beta||_2^2=\sum^n_{j=1}|\beta_j|^2

总结:

  1. L1 regularization和L2 regularization区别:
    • L1 tend to provide sparse results,可以做feature selection,对于feature之间有correlation,给出的结果sparse而且不stable,一般用的比较少
    • L tend to provide stable solution,对于correlation的情况处理的比较好,但是不能做feature selection
    • 各有优劣,选择?很多lib默认用L2,比如Ridge 使用 L2;普通 LinearRegression 不自动加正则项
  2. 算MSE=1m∗∑i=1m(yi−β0−∑j=1pβjxij)2MSE=\frac{1}{m}*\sum^m_{i=1}(y_i-\beta_0-\sum^p_{j=1}\beta_j x_{ij})^2时,要不要加penalty term?不要

整理参考资料