機器學習(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),當上式收斂時則退出迭代,何為收斂,即前后兩次迭代的值不再發生變化了。一般情況下,會設置一個具體的參數,當前后兩次迭代差值小於該參數時候結束迭代。注意以下幾點:
二. 隨機梯度下降算法
因為每次計算梯度都需要遍歷所有的樣本點。這是因為梯度是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,從而花費的時間遠大於隨機梯度下降。但是隨機梯度下降過早的結束了迭代,使得它獲取的值只是接近局部最優解,而並非像批梯度下降算法那么是局部最優解。
因此我覺得以上的差別才是批梯度下降與隨機梯度下降最本質的差別。