Andrew Ng machine learning study notes (a) univariate regression models

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_40759863/article/details/86036336

Recently, Netease cloud classroom to watch the video Andrew Ng machine learning, write down the series of blog knowledge so that they can watch the review in the future.

As shown above, this is a scatter plot of the relationship between house prices and the size of the house, if you have a friend who wants to sell his house, then you can evaluate him as your friend based on these data the price of the house. Now observation image, we seem to be able to draw a straight line inclined to fit in this picture points, as shown below:

By this line, since we can according to the size of your friend's house to estimate the price of his house, but the problem here? You can clearly know, on this picture, we can draw a lot of lines, then a straight line which best fits these data points is it? This is the machine learning problem to be solved.

As shown above, picture shows some of the dimensions x and data house prices y corresponding to the house, we have provided to be obtained straight line is h (x) function on the map, in the function \Theta 0and \Theta 1what we claim the parameters of the model because there is only one variable x, so we called "single-variable regression model." So how best to solve the two parameters? This time we will use the above said data collection by some algorithm to train our function (in practice, we usually put the data set is divided into two parts: training and test sets, a training set for training function to get together social parameter, and then use the test set to evaluate the performance of our function), to obtain the appropriate parameters.

 

如上图所示,现在我们有一个假设的线性函数h(x),现在要做的就是求出该函数的两个参数\Theta 1\Theta 0,我们设一个关于这两个函数的函数J(\Theta 0,\Theta 1),看上图,J(\Theta 0,\Theta 1)是将每一个样本x^{(i)}(i为样本的编号)输入到h(x)中得到一个预测值,再与该样本的真实房价y^{(i)}相减得到一个差值,将这些差值取平方加起来得到一个总和除以m再乘以1/2(m为样本的 总数,乘1/2是为了之后对J(\Theta 0,\Theta 1)求导方便),我们称该函数为“损失函数”。所以,J(\Theta 0,\Theta 1)的值就是我们的预测值与真实值的差距,显然,我们希望该差距应该越小越好,所以我们的目标就转为求解上图中的Goal函数:

下面我们来介绍求解该函数的方法:梯度下降。

首先我们先看上图,该图的纵坐标是损失函数的值,横坐标为它的两个变量\Theta 0\Theta 1,现在我们想象该图是两个山峰,而你此时身处其中一个小的山峰中,如上图中红色顶点的叉点,你沿着某个方向往前踏出一步,然后再选择一个方向再往前前进一步,每走一步,你就离山底更进一步,直至最后抵达山底。

在单变量回归模型中,损失函数J(\theta 0, \theta 1)的值与两个参数的值之间的关系如上图,我们可以看到,该图与上面“爬山”图的不同之处在于J(\theta 0, \theta 1)这个函数没有局部最优解,只有一个全局最优解,这使得我们进行梯度下降后,总会使得每一个参数\theta _{i}都求得一个最佳值,我们称这种函数为“凸函数”(事实上,线性回归模型无论是多变量还是单变量,它的损失函数都是“凸函数”)。梯度下降的过程就类似与上述“爬山过程”,下面给出梯度下降的算法公式(下面:=符号表示把右边的值赋予左边):

 

如上图所示:我们通过对 J(\Theta 0,\Theta 1)求关于\Theta j(在本文中,j为0,1)求偏导数再乘以一个学习率\alpha,然后再用\Theta j减去该值来更新\Theta j,其中学习率\alpha就是“每次爬山的步长”,偏导数就是“下山的方向”,反复重复公式中的过程,直到\Theta j不再更新(梯度下降算法执行时,随着\theta j越来越接近最优值,偏导数部分的值会越来越小,更新的幅度越来越小,当到达最低点时,偏导数的值为0,所以公式等号右边部分会等于左边部分,偏导数便不再更新)。下面给出参数更新的两种情况:

左边那张图的情况:当我们的参数\theta1大于最优值时,执行梯度下降算法,偏导数部分求得的结果大于0,带入到公式中我们显然可以看出\theta1被减小了,即“不断往左移动”直到最优值。

右边那张图的情况:但我们的参数\theta1小于最优值时,执行梯度下降算法,偏导数部分求得的结果小于0,带入到公式中我们显然可以看出\theta1被增大了,即“不断往右移动”直到最优值。

在使用公式进行梯度下降的过程中,我们要注意得“同步更新”两个参数的值:

上图中,左边就是进行了“同步更新” ,即先将每个参数进行一次梯度下降的值保存在临时变量中,再将这些临时变量的值同时赋给对应的参数,于此相反的,右边的更新参数就不是同步更新(\Theta 1进行梯度下降的时候 J(\Theta 0,\Theta 1中的\Theta 0已经更新了)。将J(\Theta 0,\Theta 1)关于\Theta 0\Theta 1的偏导数求出后带入公式中,可以得到下面公式:

下面我们再讲一下 学习率\alpha的选取,它选择过小或者过大都会造成不好的影响,如果取值太小,则每次进行梯度下降的幅度太小,那么我们得进行很多次梯度下降才能取到参数的最优值;如果取值过大,那么参数梯度下降的幅度将会很大,则可能会出现下面一种情况:如果你的\theta取值已经很接近最优值,但可能会因为你的更新步长\alpha太大而更新后的点“恰好”错过最低点,此时它会继续执行梯度下降算法继续更新参数的值,但永远得不到能使损失函数取到最小值的那个参数值。如下图所示情况: 

 (左边的点已经接近最低点,但如果你的更新步长过大,则它下一步更新就得到右边的点,这将错过那个能使J(\theta 1)值最小的最优值)

通常,我们可以画出\underset{\theta }{minJ(\theta )}的值与\theta的梯度下降迭代次数的关系图来观察学习率的大小,如下图:

如果你的参数进行一定次数的梯度下降后, \underset{\theta }{minJ(\theta )}的值能够逐渐减小最后收敛于某值时,则说明你的学习率\alpha选择恰当,如果是关系图是下面的图像,则说明你的学习率选取过大,错过了最优解,则你应该选取更小的学习率:

吴恩达则通常会尝试一系列的学习率取值,再画关系图观察后,按比例增大学习率的值,直到得到恰当的\alpha的值,比如先取0.001,然后依次取0.003,0.01,0.03,....(每次扩大三倍)。

以上,就是我们单变量模型的主要内容,下次我们将介绍单变量回归模型的扩展——多变量回归模型。

 

 

 

Guess you like

Origin blog.csdn.net/weixin_40759863/article/details/86036336