多元梯度下降 | Linear regression with multiple variables

226 阅读4分钟

这是我参与8月更文挑战的第14天,活动详情查看:8月更文挑战


Practice 1

feature scalling 特征缩放

Idea:Make sure features are on a similar scale.

If you oake sure that the features are on a simillar scale (the diffferent features take on similar ranges of values), then gradient descents can converge more quickly.

特征缩放就是确保不同的特征量取值范围差不多,这样会让梯度下降执行得更快。

举个栗子:

x1x_1=size(0-2000 feet2feet^2

x2x_2=number of bedrooms(1-5)

对于这个例子,预测函数为hθ(x)=θ0+θ1x1+θ2x2h_{\theta}(x)=\theta_0+\theta_1x_1+\theta_2x_2,现在我们依旧将其简化然后画出图像。

θ0=0\theta_0=0,画出hθ(x)=θ1x1+θ2x2h_{\theta}(x)=\theta_1x_1+\theta_2x_2的图像如下,不出意外这个椭圆长轴和短轴的比例也将达到2000:5。如果你不幸起始从长轴某处开始进行梯度下降,那整个梯度下降的过程会非常长。

image.png

而使用特征缩放就是将这两个特征量的取值放到差不多大的范围里,比如:

x1=size( feet 2)2000x_{1}=\frac{\operatorname{size}\left(\text { feet }^{2}\right)}{2000}
x2= number of bedrooms 5x_{2}=\frac{\text { number of bedrooms }}{5}

这样就会变成

x1x_1=size(0-1 feet2/2000feet^2/2000

x2x_2=number of bedrooms(0.2-1 number/5number/5

图像也会更接近圆形:

image.png

而对于这种比较圆的图像来说,梯度下降几乎就是沿直线进行的,更节省时间。

Feature scaling :Get every feature into approximately a 1al-1≤a≤l range.

More generally, when were performing feature scaling, what we often want to do is get every feature into approximately a -l to +l range. And concretely, your feature x0x_0 is always equal to 1. So, that's already in that range,but you may end up dividing other features by diiferent mumbers to get theo to this range.

通常来说我们进行特征缩放是让特征量范围集中在[1,+1][-1,+1]之间。具体来讲,我们开始已经假设x0=1x_0=1,所以对他不用进行缩放,而对于其他特征量我们则要乘以或者除以不同的数使其达到对应范围。

当然这个[1,+1][-1,+1]不是硬性要求,其实只要取值范围不是特别离谱就不用非要进行缩放。比如[0,3][0,3][1,2.5][-1,2.5][2,1][-2,-1]……这些也不用非要进行缩放。但是如果你的数据是[100,+1000][-100,+1000][0.00001,+0.0001][-0.00001,+0.0001]这种,请务必进行缩放。

Mean normalization 均值归一化

Replace xix_i with xiμix_i-\mu_{i} to make features have approximately zero mean (Do not apply to x0=1x_0= 1

xix_i减去一个值,使其平均值约为0。取值范围一般在[0.5,+0.5][-0.5,+0.5]之间,当然这也不是硬性要求,比0.5稍微大一点也没什么啦。

还是上边那个例子。

x1=size10002000x_{1}=\frac{\operatorname{size}-1000}{2000}
x2=# bedrooms 25x_{2}=\frac{\# \text { bedrooms }-2}{5}

总结

对于上边的特征缩放和均值归一化总结起来就是:

x1x1μ1s1x_{1} \leftarrow \frac{x_{1}-\mu_{1}}{s_{1}}
  • μ1\mu_{1}一般取值样本的平均值(average value)
  • s1s_{1}一般取值最大值-最小值或者标准差(standard deviation)

以上的操作都不是硬性要求,也没有严格的标准必须缩放到什么程度,一切操作都只是为了让梯度下降更快而已。


Practice 2

Debugging

How to make sure gradient descent is working correctly

梯度下降的目的是找到合适的θ使代价函数最小化。

图像

image.png

上图横坐标为100的那个点代表梯度下降算法迭代100次以后J(θ)的值。同理横坐标为200的点表示梯度下降算法迭代200次以后J(θ)的值。

So what this plot is showing, is it's showing the value, of your cost function after each iteration of gradient descent. And, if gradient descent is working properly then J of theta shoulld decrease after every it iteration.

所以这个图表表示的是你的梯度下降算法迭代次数和代价函数J(θ)的对应关系。如果你的梯度下降算法运行正常,那代价函数J的图像应该是一个非递增函数。

再看上图300-400,曲线已经趋于水平,证明梯度下降已经收敛。

除了告诉你梯度下降是否已经收敛,还能提前告诉你算法是否出现错误,

当然如果你看到这种图,随着梯度下降迭代次数的提高,代价函数J居然越来越大,那你一定是α取值太大,导致梯度下降越过最低点而左右横跳。(不理解的可以看看之前的文章)

image.png

如果你看到的图像如下图,那也是α取值太大了。

image.png

image.png

自动收敛测试 automatic convergence test

上述图像能让你知道梯度下降算法是否已经收敛,当然你一可以用别的方法,比如自动收敛测试。

给定一个“极小值ε\varepsilon”,当每次迭代J(θ)的差值小于这个“极小值ε\varepsilon”的时候则认为已经成功收敛。

比如最常用的:If J(θ)J(θ) decreases by less than 10310^{-3} in one iteration. 在这里ε=103\varepsilon = 10^{-3}

但是并不是每个实验都要取ε=103\varepsilon = 10^{-3}要取得一个合适的阈值ε\varepsilon是很困难的。所以建议你用上边的图像法。

How to choose learning rate α

Summary:

  • If α is too small: slow convergence
  • If α is too large: J(θ) may not decrease on every iteration, may not converge.

总结:如果你选择的α太小,那梯度下降速率会非常慢;如果你选择过大,那可能并不会每次梯度下降都会使代价函数减小,甚至可能使代价函数越来越大。

选择α一般这样尝试。

To choose α,try

  • 0.001,
  • 0.01,
  • 0.1,
  • 1
  • ...