機器學習(1)之梯度下降(gradient descent)


機器學習(1)之梯度下降(gradient descent)

題記:最近零碎的時間都在學習Andrew Ng的machine learning,因此就有了這些筆記。

 

梯度下降是線性回歸的一種(Linear Regression),首先給出一個關於房屋的經典例子,

面積(feet2) 房間個數 價格(1000$)
2104 3 400
1600 3 330
2400 3 369
1416 2 232
3000 4 540
... ... ..

 

上表中面積和房間個數是輸入參數,價格是所要輸出的解。面積和房間個數分別表示一個特征,用X表示。價格用Y表示。表格的一行表示一個樣本。現在要做的是根據這些樣本來預測其他面積和房間個數對應的價格。可以用以下圖來表示,即給定一個訓練集合,學習函數h,使得h(x)能符合結果Y。

一. 批梯度下降算法 

可以用以下式子表示一個樣本:

 

θ表示X映射成Y的權重,x表示一次特征。假設x0=1,上式就可以寫成:

分別使用x(j),y(j)表示第J個樣本。我們計算的目的是為了讓計算的值無限接近真實值y,即代價函數可以采用LMS算法

要獲取J(θ)最小,即對J(θ)進行求導且為零:

當單個特征值時,上式中j表示系數(權重)的編號,右邊的值賦值給左邊θj從而完成一次迭代。

單個特征的迭代如下:

多個特征的迭代如下:

上式就是批梯度下降算法(batch gradient descent),當上式收斂時則退出迭代,何為收斂,即前后兩次迭代的值不再發生變化了。一般情況下,會設置一個具體的參數,當前后兩次迭代差值小於該參數時候結束迭代。注意以下幾點:

(1) a 即learning rate,決定的下降步伐,如果太小,則找到函數最小值的速度就很慢,如果太大,則可能會出現overshoot the minimum的現象;
 
(2) 初始點不同,獲得的最小值也不同,因此梯度下降求得的只是局部最小值;
 
(3) 越接近最小值時,下降速度越慢;
 
(4) 計算批梯度下降算法時候,計算每一個θ值都需要遍歷計算所有樣本,當數據量的時候這是比較費時的計算。
 
批梯度下降算法的步驟可以歸納為以下幾步:
 
(1)先確定向下一步的步伐大小,我們稱為Learning rate ;
 
(2)任意給定一個初始值:θ向量,一般為0向量
 
(3)確定一個向下的方向,並向下走預先規定的步伐,並更新θ向量
 
(4)當下降的高度小於某個定義的值,則停止下降;
 

 

二. 隨機梯度下降算法

因為每次計算梯度都需要遍歷所有的樣本點。這是因為梯度是J(θ)的導數,而J(θ)是需要考慮所有樣本的誤差和 ,這個方法問題就是,擴展性問題,當樣本點很大的時候,基本就沒法算了。所以接下來又提出了隨機梯度下降算法(stochastic gradient descent )。隨機梯度下降算法,每次迭代只是考慮讓該樣本點的J(θ)趨向最小,而不管其他的樣本點,這樣算法會很快,但是收斂的過程會比較曲折,整體效果上,大多數時候它只能接近局部最優解,而無法真正達到局部最優解。所以適合用於較大訓練集的case。

三.代碼實現

隨機梯度下降算法的python的實現: 

 1 # coding=utf-8
 2 #!/usr/bin/python
 3 
 4 '''
 5 Created on 2014年9月6日
 6  
 7 @author: Ryan C. F.
 8 
 9 '''
10 
11 #Training data set
12 #each element in x represents (x0,x1,x2)
13 x = [(1,0.,3) , (1,1.,3) ,(1,2.,3), (1,3.,2) , (1,4.,4)]
14 #y[i] is the output of y = theta0 * x[0] + theta1 * x[1] +theta2 * x[2]
15 y = [95.364,97.217205,75.195834,60.105519,49.342380]
16 
17 
18 epsilon = 0.0001
19 #learning rate
20 alpha = 0.01
21 diff = [0,0]
22 error1 = 0
23 error0 =0
24 m = len(x)
25 
26 
27 #init the parameters to zero
28 theta0 = 0
29 theta1 = 0
30 theta2 = 0
31 
32 while True:
33 
34     #calculate the parameters
35     for i in range(m):
36     
37         diff[0] = y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] )
38         
39         theta0 = theta0 + alpha * diff[0]* x[i][0]
40         theta1 = theta1 + alpha * diff[0]* x[i][1]
41         theta2 = theta2 + alpha * diff[0]* x[i][2]
42     
43     #calculate the cost function
44     error1 = 0
45     for lp in range(len(x)):
46         error1 += ( y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] ) )**2/2
47     
48     if abs(error1-error0) < epsilon:
49         break
50     else:
51         error0 = error1
52     
53     print ' theta0 : %f, theta1 : %f, theta2 : %f, error1 : %f'%(theta0,theta1,theta2,error1)
54 
55 print 'Done: theta0 : %f, theta1 : %f, theta2 : %f'%(theta0,theta1,theta2)

批梯度下降算法

 1 # coding=utf-8
 2 #!/usr/bin/python
 3 
 4 '''
 5 Created on 2014年9月6日
 6  
 7 @author: Ryan C. F.
 8 
 9 '''
10 
11 #Training data set
12 #each element in x represents (x0,x1,x2)
13 x = [(1,0.,3) , (1,1.,3) ,(1,2.,3), (1,3.,2) , (1,4.,4)]
14 #y[i] is the output of y = theta0 * x[0] + theta1 * x[1] +theta2 * x[2]
15 y = [95.364,97.217205,75.195834,60.105519,49.342380]
16 
17 
18 epsilon = 0.000001
19 #learning rate
20 alpha = 0.001
21 diff = [0,0]
22 error1 = 0
23 error0 =0
24 m = len(x)
25 
26 #init the parameters to zero
27 theta0 = 0
28 theta1 = 0
29 theta2 = 0
30 sum0 = 0
31 sum1 = 0
32 sum2 = 0
33 while True:
34     
35     #calculate the parameters
36     for i in range(m):
37         #begin batch gradient descent
38         diff[0] = y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] )
39         sum0 = sum0 + alpha * diff[0]* x[i][0]
40         sum1 = sum1 + alpha * diff[0]* x[i][1]
41         sum2 = sum2 + alpha * diff[0]* x[i][2]
42         #end  batch gradient descent
43     theta0 = sum0;
44     theta1 = sum1;
45     theta2 = sum2;
46     #calculate the cost function
47     error1 = 0
48     for lp in range(len(x)):
49         error1 += ( y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] ) )**2/2
50     
51     if abs(error1-error0) < epsilon:
52         break
53     else:
54         error0 = error1
55     
56     print ' theta0 : %f, theta1 : %f, theta2 : %f, error1 : %f'%(theta0,theta1,theta2,error1)
57 
58 print 'Done: theta0 : %f, theta1 : %f, theta2 : %f'%(theta0,theta1,theta2)

通過上述批梯度下降和隨機梯度下降算法代碼的對比,不難發現兩者的區別:

1. 隨機梯度下降算法在迭代的時候,每迭代一個新的樣本,就會更新一次所有的theta參數。

35     for i in range(m):
36     
37         diff[0] = y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] )
38         
39         theta0 = theta0 + alpha * diff[0]* x[i][0]
40         theta1 = theta1 + alpha * diff[0]* x[i][1]
41         theta2 = theta2 + alpha * diff[0]* x[i][2]

2. 批梯度下降算法在迭代的時候,是完成所有樣本的迭代后才會去更新一次theta參數

35     #calculate the parameters
36     for i in range(m):
37         #begin batch gradient descent
38         diff[0] = y[i]-( theta0 + theta1 * x[i][1] + theta2 * x[i][2] )
39         sum0 = sum0 + alpha * diff[0]* x[i][0]
40         sum1 = sum1 + alpha * diff[0]* x[i][1]
41         sum2 = sum2 + alpha * diff[0]* x[i][2]
42         #end  batch gradient descent
43     theta0 = sum0;
44     theta1 = sum1;
45     theta2 = sum2;

因此當樣本數量很大時候,批梯度得做完所有樣本的計算才能更新一次theta,從而花費的時間遠大於隨機梯度下降。但是隨機梯度下降過早的結束了迭代,使得它獲取的值只是接近局部最優解,而並非像批梯度下降算法那么是局部最優解。

因此我覺得以上的差別才是批梯度下降與隨機梯度下降最本質的差別。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM