正则化(Regularization - Solving the Problem of Overfitting)

欠拟合(高偏差) VS 过度拟合(高方差)

Underfitting, or high bias, is when the form of our hypothesis function h maps poorly to the trend of the data.

It is usually caused by a function that is too simple or uses too few features.

欠拟合(高偏差):没有很好的拟合训练集数据;

At the other extreme, overfitting, or high variance, is caused by a hypothesis function that fits the available data but does not generalize well to predict new data.

It is usually caused by a complicated function that creates a lot of unnecessary curves and angles unrelated to the data.

过度拟合(高方差):可以很好的拟合训练集数据,但是函数太过庞大,变量太多,且缺少足够多的数据约束该模型(m < n),无法泛化到新的数据样本。

This terminology is applied to both linear and logistic regression. There are two main options to address the issue of overfitting:

两种方法解决过度拟合:

  1. Reduce the number of features
  • Manually select which features to keep
  • Use a model selection algorithm (studied later in the course)
  1. Regularization
  • Keep all the features, but reduce the magnitude of parameters \(\theta_j\).
  • Regularization works well when we have a lot of slightly useful features.

正则化 - 线性回归代价函数

所有正则化均不包括 \(\theta_0\) 项

\(J(\theta)=\frac{1}{2m} \Bigg[ \sum\limits_{i=1}^m \Big( h_\theta(x^{(i)}) - y^{(i)} \Big)^2 + \lambda \sum\limits_{j=1}^n \theta_j^2 \Bigg]\)

向量化表示为(A vectorized implementation is):

\(\overrightarrow{h}=g(X \overrightarrow{\theta})\)

\(J(\theta)=\frac{1}{2m} \cdot \Bigg[ (\overrightarrow{h}-\overrightarrow{y})^T \cdot (\overrightarrow{h}-\overrightarrow{y}) + \lambda \cdot (\overrightarrow{l} \cdot \overrightarrow{\theta}^{.2}) \Bigg]\)

\(\overrightarrow{l} = [0, 1, 1, ...1]\)

代码实现:

m = length(y);

l = ones(1, length(theta)); l(:,1) = 0;
J = 1/(2*m) * ((X * theta - y)' * (X * theta - y) + lambda * (l * (theta.^2)); or J = 1/(2*m) * ((X * theta - y)' * (X * theta - y) + lambda * (theta'*theta - theta(1,:).^2);

正则化 - 逻辑回归代价函数

所有正则化均不包括 \(\theta_0\) 项

\(J(\theta)=-\frac{1}{m} \sum\limits_{i=1}^m \Bigg[ y^{(i)} \cdot log \bigg(h_\theta(x^{(i)}) \bigg) + (1-y^{(i)}) \cdot log \bigg(1-h_\theta(x^{(i)}) \bigg) \Bigg] + \frac{\lambda}{2m} \sum\limits_{j=1}^n \theta_j^2\)

向量化表示为(A vectorized implementation is):

\(\overrightarrow{h}=g(X \overrightarrow{\theta})\)

\(J(\theta)=\frac{1}{m} \cdot \Big( -\overrightarrow{y}^T \cdot log(\overrightarrow{h}) - (1- \overrightarrow{y})^T \cdot log(1- \overrightarrow{h}) \Big) + \frac{\lambda}{2m} (\overrightarrow{l} \cdot \overrightarrow{\theta}^{.2})\)

\(\overrightarrow{l} = [0, 1, 1, ...1]\)

代码实现:

m = length(y);

l = ones(1, length(theta)); l(:,1) = 0;
J = (1/m)*(-y'*log(sigmoid(X*theta))-(1 - y)'* log(1-sigmoid(X*theta))) + ...
(lambda/(2*m))*(l*(theta.^2)); or J = (1/m)*(-y'*log(sigmoid(X*theta))-(1 - y)'* log(1-sigmoid(X*theta))) + ...
(lambda/(2*m))*(theta'*theta - theta(1,:).^2);

正则化后的线性回归和逻辑回归梯度下降

所有正则化均不包括 \(\theta_0\) 项

\(\begin{cases} \theta_0:=\theta_0 - \alpha \frac{1}{m} \sum\limits_{i=1}^m \Big( h_\theta(x^{(i)}) - y^{(i)} \Big) \cdot x_0^{(i)} \\ \\ \theta_j:=\theta_j - \alpha \Bigg[ \frac{1}{m} \sum\limits_{i=1}^m \Big( h_\theta(x^{(i)}) - y^{(i)} \Big) \cdot x_j^{(i)} + \frac{\lambda}{m} \cdot \theta_j \Bigg] \end{cases}\)

向量化表示为(A vectorized implementation is):

\(\frac{1}{m} \cdot \Big( X^T \cdot (\overrightarrow{h} - \overrightarrow{y}) \Big) + \frac{\lambda}{m} \cdot \theta^{'}\)

\(\theta^{'} = \begin{bmatrix} 0\\[0.3em]\theta_1\\[0.3em]\theta_2\\[0.3em].\\[0.3em].\\[0.3em].\\[0.3em]\theta_n \end{bmatrix}\)

代码实现:

reg_theta=theta; reg_theta(1, :) = 0;
grad = (1/m)*(X'*(sigmoid(X*theta) - y)) + (lambda/m)*reg_theta;

最终形式:对 \(\theta_j\) 的梯度下降公式进行整理变形(With some manipulation our update rule can also be represented as):

\(\begin{cases} \theta_0:=\theta_0 - \alpha \frac{1}{m} \sum\limits_{i=1}^m \Big( h_\theta(x^{(i)}) - y^{(i)} \Big) \cdot x_0^{(i)} \\ \\ \theta_j:=\theta_j (1- \alpha \frac{\lambda}{m}) - \alpha \frac{1}{m} \sum\limits_{i=1}^m \Big( h_\theta(x^{(i)}) - y^{(i)} \Big) \cdot x_j^{(i)} \end{cases}\)

对线性回归正规方程进行正则化

所有正则化均不包括 \(\theta_0\) 项

\(1 - \alpha\frac{\lambda}{m}\) will always be less than 1. Intuitively you can see it as reducing the value of \(\theta_j\) by some amount on every update. Notice that the second term is now exactly the same as it was before.

Now let's approach regularization using the alternate method of the non-iterative normal equation.

To add in regularization, the equation is the same as our original, except that we add another term inside the parentheses:

原始形态 \(\overrightarrow{\theta} = (X^TX)^{-1}X^T \overrightarrow{y}\)

正则化后 \(\overrightarrow{\theta} = (X^TX + \lambda L)^{-1}X^T \overrightarrow{y}\)

\(L = \begin{bmatrix} 0&&&&&&\\[0.3em]&1&&&&&\\[0.3em]&&1&&&&\\[0.3em]&&&·&&&\\[0.3em]&&&&·&&\\[0.3em]&&&&&·&\\[0.3em]&&&&&&1\end{bmatrix}\)

L is a matrix with 0 at the top left and 1's down the diagonal, with 0's everywhere else. It should have dimension (n+1)×(n+1).

Intuitively, this is the identity matrix (though we are not including \(x_0\))multiplied with a single real number \(\lambda\).

Recall that if m < n, then \(X^TX\) is non-invertible. However, when we add the term \(\lambda⋅L\), then \(X^TX + \lambda⋅L\) becomes invertible.

程序代码

正则化的特性已经全部添加到了其他练习代码中,如线性回归,逻辑回归,神经网络等。可在其他练习中查看到,如需非正则化,只要将Lambda=0即可。

获取源码以其他文件,可点击右上角 Fork me on GitHub 自行 Clone。

[C3] 正则化(Regularization)的更多相关文章

  1. [DeeplearningAI笔记]改善深层神经网络1.4_1.8深度学习实用层面_正则化Regularization与改善过拟合

    觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.4 正则化(regularization) 如果你的神经网络出现了过拟合(训练集与验证集得到的结果方差较大),最先想到的方法就是正则化(re ...

  2. zzL1和L2正则化regularization

    最优化方法:L1和L2正则化regularization http://blog.csdn.net/pipisorry/article/details/52108040 机器学习和深度学习常用的规则化 ...

  3. 7、 正则化(Regularization)

    7.1 过拟合的问题 到现在为止,我们已经学习了几种不同的学习算法,包括线性回归和逻辑回归,它们能够有效地解决许多问题,但是当将它们应用到某些特定的机器学习应用时,会遇到过拟合(over-fittin ...

  4. 斯坦福第七课:正则化(Regularization)

    7.1  过拟合的问题 7.2  代价函数 7.3  正则化线性回归 7.4  正则化的逻辑回归模型 7.1  过拟合的问题 如果我们有非常多的特征,我们通过学习得到的假设可能能够非常好地适应训练集( ...

  5. (五)用正则化(Regularization)来解决过拟合

    1 过拟合 过拟合就是训练模型的过程中,模型过度拟合训练数据,而不能很好的泛化到测试数据集上.出现over-fitting的原因是多方面的: 1) 训练数据过少,数据量与数据噪声是成反比的,少量数据导 ...

  6. [笔记]机器学习(Machine Learning) - 03.正则化(Regularization)

    欠拟合(Underfitting)与过拟合(Overfitting) 上面两张图分别是回归问题和分类问题的欠拟合和过度拟合的例子.可以看到,如果使用直线(两组图的第一张)来拟合训,并不能很好地适应我们 ...

  7. CS229 5.用正则化(Regularization)来解决过拟合

    1 过拟合 过拟合就是训练模型的过程中,模型过度拟合训练数据,而不能很好的泛化到测试数据集上.出现over-fitting的原因是多方面的: 1) 训练数据过少,数据量与数据噪声是成反比的,少量数据导 ...

  8. 1.4 正则化 regularization

    如果你怀疑神经网络过度拟合的数据,即存在高方差的问题,那么最先想到的方法可能是正则化,另一个解决高方差的方法就是准备更多数据,但是你可能无法时时准备足够多的训练数据,或者获取更多数据的代价很高.但正则 ...

  9. 机器学习(五)--------正则化(Regularization)

    过拟合(over-fitting) 欠拟合 正好 过拟合 怎么解决 1.丢弃一些不能帮助我们正确预测的特征.可以是手工选择保留哪些特征,或者使用一 些模型选择的算法来帮忙(例如 PCA) 2.正则化. ...

随机推荐

  1. 洛谷 P5639 【CSGRound2】守序者的尊严

    洛谷 P5639 [CSGRound2]守序者的尊严 洛谷传送门 题目背景 由于Y校最近进行了对学校食堂的全面改革与对小卖部的全面整治(乱搞),导致学校小卖部卖的零食被禁售了:学校食堂的炸鸡窗口也消失 ...

  2. 基础知识 Asp.Net MVC EF各版本区别

    原文:https://www.cnblogs.com/liangxiaofeng/p/5840754.html 2009年發行ASP.NET MVC 1.0版 2010年發行ASP.NET MVC 2 ...

  3. Java并发编程:Java中的锁和线程同步机制

    锁的基础知识 锁的类型 锁从宏观上分类,只分为两种:悲观锁与乐观锁. 乐观锁 乐观锁是一种乐观思想,即认为读多写少,遇到并发写的可能性低,每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新 ...

  4. HTML文件通过jQuery引入其他HTML文件报错has been blocked by CORS policy

    HTML通过jQuery引入模板 完整报错 新创建一个chrome快捷方式,命名为chrome-debug 右键属性,在目标后添加参数,原始路径如下 "C:\Program Files (x ...

  5. [Pytorch Bug] "EOFError: Ran out of input" When using Dataloader with num_workers=x

    在Windows上使用Dataloader并设置num_workers为一个非零数字,enumerate取数据时会引发"EOFError: Ran out of input"的报错 ...

  6. python学习第一天第二天总结

    变量赋值 1, 变量由字⺟, 数字,下划线搭配组合⽽成 2, 不可以⽤数字开头,更不能是全数字 3,不能是pythond的关键字, 这些符号和字⺟已经被python占⽤, 不可以更改 4,不要⽤中⽂ ...

  7. 【raid级别】RAID级别工作模式

    友情链接 磁盘分区,格式化,挂载,创建交换分区:https://www.cnblogs.com/HeiDi-BoKe/p/11936998.html RAID工作级别:https://www.cnbl ...

  8. selenium常用的三种等待方式

    一.强制等待 使用方法:sleep(X),等待X秒后,进行下一步操作. 第一种也是使用最简单的一种办法就是强制等待sleep(X),强制让浏览器等待X秒,不管当前操作是否完成,是否可以进行下一步操作, ...

  9. spring的事件

    理论 异步的实现方式可以使用事件,或者异步执行: spring中自带了事件的支持,核心是ApplicationEventPublisher; 事件包括三个要点: 事件的定义: 事件监听的定义: 发布事 ...

  10. 洛谷 P4999(数位DP)

    ###洛谷 P4999 题目链接 ### 题目大意:给你一个区间,求这段区间中所有数的,数位上的,数字之和. 分析: 这题与 洛谷 P2602 相似,稍微改一下就可以了. 求出 0 ~ 9 的个数,然 ...