method_SGD(Stochastic Gradient Descent)

来源:互联网 发布:瘦大腿内侧 知乎 编辑:程序博客网 时间:2024/05/17 01:26

刚刚看完斯坦福大学机器学习第四讲(牛顿法),也对学习过程做一次总结吧。

一、误差准则函数与随机梯度下降:

数学一点将就是,对于给定的一个点集(X,Y),找到一条曲线或者曲面,对其进行拟合之。同时称X中的变量为特征(Feature),Y值为预测值。

如图:


一个典型的机器学习的过程,首先给出一组输入数据X,我们的算法会通过一系列的过程得到一个估计的函数,这个函数有能力对没有见过的新数据给出一个新的估计Y,也被称为构建一个模型。

我们用X1、X2…Xn 去描述feature里面的分量,用Y来描述我们的估计,得到一下模型:


我们需要一种机制去评价这个模型对数据的描述到底够不够准确,而采集的数据x、y通常来说是存在误差的(多数情况下误差服从高斯分布),于是,自然的,引入误差函数:


关键的一点是如何调整theta值,使误差函数J最小化。J函数构成一个曲面或者曲线,我们的目的是找到该曲面的最低点:


假设随机站在该曲面的一点,要以最快的速度到达最低点,我们当然会沿着坡度最大的方向往下走(梯度的反方向)

用数学描述就是一个求偏导数的过程:


这样,参数theta的更新过程描述为以下:

   (α表示算法的学习速率)

二、不同梯度下降算法的区别:

  • 梯度下降:梯度下降就是我上面的推导,要留意,在梯度下降中,对于http://www.w3.org/1998/Math/MathML">θ” role=”presentation” style=”margin: 0px; padding: 0px; display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; position: relative;”>θθ的更新,所有的样本都有贡献,也就是参与调整http://www.w3.org/1998/Math/MathML">θ” role=”presentation” style=”margin: 0px; padding: 0px; display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; position: relative;”>θθ.其计算得到的是一个标准梯度。因而理论上来说一次更新的幅度是比较大的。如果样本不多的情况下,当然是这样收敛的速度会更快啦~

  • 随机梯度下降:可以看到多了随机两个字,随机也就是说我用样本中的一个例子来近似我所有的样本,来调整http://www.w3.org/1998/Math/MathML">θ” role=”presentation” style=”margin: 0px; padding: 0px; display: inline; line-height: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; position: relative;”>θθ,因而随机梯度下降是会带来一定的问题,因为计算得到的并不是准确的一个梯度,容易陷入到局部最优解中

  • 批量梯度下降:其实批量的梯度下降就是一种折中的方法,他用了一些小样本来近似全部的,其本质就是我1个指不定不太准,那我用个30个50个样本那比随机的要准不少了吧,而且批量的话还是非常可以反映样本的一个分布情况的。

三、算法实现与测试:

通过一组数据拟合 y = theta1*x1 +theta2*x2

[python] view plain copy
print?
  1. #Python 3.3.5  
  2. import random  
  3. # matrix_A  训练集  
  4. matrix_A = [[1,4], [2,5], [5,1], [4,2]]  
  5. Matrix_y = [19,26,19,20]  
  6. theta = [2,5]  
  7. #学习速率  
  8. leraing_rate = 0.005  
  9. loss = 50  
  10. iters = 1  
  11. Eps = 0.0001  
  12. #随机梯度下降  
  13. while loss>Eps and iters <1000 :  
  14.     loss = 0  
  15.     i = random.randint(03)  
  16.     h = theta[0]matrix_A[i][0] + theta[1]*matrix_A[i][1]   
  17.     theta[0] = theta[0] + leraing_rate(Matrix_y[i]-h)matrix_A[i][0]  
  18.     theta[1] = theta[1] + leraing_rate(Matrix_y[i]-h)matrix_A[i][1]  
  19.     Error = 0  
  20.     Error = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1] - Matrix_y[i]  
  21.     Error = Error*Error  
  22.     loss = loss +Error  
  23.     iters = iters +1  
  24. print (‘theta=’,theta)  
  25. print (‘iters=’,iters)  
  26. ”“” 
  27. #梯度下降 
  28. while loss>Eps and iters <1000 : 
  29.     loss = 0 
  30.     for i in range(4): 
  31.         h = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1]  
  32.         theta[0] = theta[0] + leraing_rate(Matrix_y[i]-h)matrix_A[i][0] 
  33.         theta[1] = theta[1] + leraing_rate(Matrix_y[i]-h)matrix_A[i][1] 
  34.     for i in range(4): 
  35.         Error = 0 
  36.         Error = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1] - Matrix_y[i] 
  37.         Error = Error*Error 
  38.         loss = loss +Error 
  39.     iters = iters +1 
  40. print (‘theta=’,theta) 
  41. print (‘iters=’,iters) 
  42. ”“”  
  43. ”“” 
  44. #批量梯度下降 
  45. while loss>Eps and iters <1000 : 
  46.     loss = 0 
  47.     sampleindex =  random.sample([0,1,2,3],2) 
  48.     for i in sampleindex : 
  49.         h = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1]  
  50.         theta[0] = theta[0] + leraing_rate(Matrix_y[i]-h)matrix_A[i][0] 
  51.         theta[1] = theta[1] + leraing_rate(Matrix_y[i]-h)*matrix_A[i][1] 
  52.     for i in sampleindex : 
  53.         Error = 0 
  54.         Error = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1] - Matrix_y[i] 
  55.         Error = Error*Error 
  56.         loss = loss +Error 
  57.     iters = iters +1 
  58. print (‘theta=’,theta) 
  59. print (‘iters=’,iters) 
  60. ”“”  
#Python 3.3.5 
import random
# matrix_A 训练集matrix_A = [[1,4], [2,5], [5,1], [4,2]]Matrix_y = [19,26,19,20]theta = [2,5]#学习速率leraing_rate = 0.005loss = 50iters = 1Eps = 0.0001#随机梯度下降while loss>Eps and iters <1000 : loss = 0 i = random.randint(0, 3) h = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1] theta[0] = theta[0] + leraing_rate*(Matrix_y[i]-h)*matrix_A[i][0] theta[1] = theta[1] + leraing_rate*(Matrix_y[i]-h)*matrix_A[i][1] Error = 0 Error = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1] - Matrix_y[i] Error = Error*Error loss = loss +Error iters = iters +1print ('theta=',theta)print ('iters=',iters)"""#梯度下降while loss>Eps and iters <1000 : loss = 0 for i in range(4): h = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1] theta[0] = theta[0] + leraing_rate*(Matrix_y[i]-h)*matrix_A[i][0] theta[1] = theta[1] + leraing_rate*(Matrix_y[i]-h)*matrix_A[i][1] for i in range(4): Error = 0 Error = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1] - Matrix_y[i] Error = Error*Error loss = loss +Error iters = iters +1print ('theta=',theta)print ('iters=',iters)""""""

批量梯度下降

while loss>Eps and iters <1000 :
loss = 0
sampleindex = random.sample([0,1,2,3],2)
for i in sampleindex :
h = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1]
theta[0] = theta[0] + leraing_rate*(Matrix_y[i]-h)*matrix_A[i][0]
theta[1] = theta[1] + leraing_rate*(Matrix_y[i]-h)*matrix_A[i][1]
for i in sampleindex :
Error = 0
Error = theta[0]*matrix_A[i][0] + theta[1]*matrix_A[i][1] - Matrix_y[i]
Error = Error*Error
loss = loss +Error
iters = iters +1
print (‘theta=’,theta)
print (‘iters=’,iters)
“”“

求解结果:
[python] view plain copy
print?
  1. >>>   
  2. theta= [2.99809592161579454.001522800837675]  
  3. iters= 75  
>>>  
theta= [2.9980959216157945, 4.001522800837675]
iters= 75
但如果对输入数据添加一些噪声

[python] view plain copy
print?
  1. matrix_A = [[1.05,4], [2.1,5], [5,1], [4,2]]  
matrix_A = [[1.05,4], [2.1,5], [5,1], [4,2]]
求解结果为:

[python] view plain copy
print?
  1. >>>   
  2. theta= [3.00959506851977253.944718521027671]  
  3. iters= 1000  
>>>  
theta= [3.0095950685197725, 3.944718521027671]
iters= 1000
可见在有噪声的情况下,要及时调整模型误差精度、迭代次数上限,一期达到我们的需求。
阅读全文
0 0
原创粉丝点击