http://en.wikipedia.org/wiki/Gradient_descent 
http://zh.wikipedia.org/wiki/%E6%9C%80%E9%80%9F%E4%B8%8B%E9%99%8D%E6%B3%95
 Gradient descent is based on the observation that if the multivariable function F(\mathbf{x}) is defined and differentiable in a neighborhood of a point \mathbf{a}, then F(\mathbf{x}) decreases fastest if one goes from \mathbf{a} in the direction of the negative gradient of F at \mathbf{a}-\nabla F(\mathbf{a}) 
為啥步長要變化?Tianyi的解釋很好:如果步長過大,可能使得函數值上升,故要減小步長 (下面這個圖片是在紙上畫好,然后scan的)。
Andrew NG的coursera課程Machine learning的II. Linear Regression with One VariableGradient descent Intuition中的解釋很好,比如在下圖在右側的點,則梯度是正數, -\nabla F(\mathbf{a})是負數,即使當前的a減小
例1:Toward the Optimization of Normalized Graph Laplacian(TNN 2011)的Fig. 1. Normalized graph Laplacian learning algorithm是很好的梯度下降法的例子.只要看Fig1,其他不必看。Fig1陶Shuning老師課件 非線性優化第六頁第四個ppt,對應教材P124,關鍵直線搜索策略,應用 非線性優化第四頁第四個ppt,步長加倍或減倍。只要目標減少就到下一個搜索點,并且步長加倍;否則停留在原點,將步長減倍。
例2: Distance Metric Learning for Large Margin Nearest Neighbor Classification(JLMR),目標函數就是公式14,是矩陣M的二次型,展開后就會發現,關于M是線性的,故是凸的。對M求導的結果,附錄公式18和19之間的公式中沒有M

我自己額外的思考:如果是凸函數,對自變量求偏導為0,然后將自變量求出來不就行了嘛,為啥還要梯度下降?上述例二是不行的,因為對M求導后與M無關了。和tianyi討論,正因為求導為0 沒有解析解采用梯度下降,有解析解就結束了

http://blog.csdn.net/yudingjun0611/article/details/8147046

1. 梯度下降法

梯度下降法的原理可以參考:斯坦福機器學習第一講

我實驗所用的數據是100個二維點。

如果梯度下降算法不能正常運行,考慮使用更小的步長(也就是學習率),這里需要注意兩點:

1)對于足夠小的,  能保證在每一步都減小;
2)但是如果太小,梯度下降算法收斂的會很慢;

總結:
1)如果太小,就會收斂很慢;
2)如果太大,就不能保證每一次迭代都減小,也就不能保證收斂;
如何選擇-經驗的方法:
..., 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1...
約3倍于前一個數。

matlab源碼:

  1. function [theta0,theta1]=Gradient_descent(X,Y);  
  2. theta0=0;  
  3. theta1=0;  
  4. t0=0;  
  5. t1=0;  
  6. while(1)  
  7.     for i=1:1:100 %100個點  
  8.         t0=t0+(theta0+theta1*X(i,1)-Y(i,1))*1;  
  9.         t1=t1+(theta0+theta1*X(i,1)-Y(i,1))*X(i,1);  
  10.     end  
  11.     old_theta0=theta0;  
  12.     old_theta1=theta1;  
  13.     theta0=theta0-0.000001*t0 %0.000001表示學習率  
  14.     theta1=theta1-0.000001*t1  
  15.     t0=0;  
  16.     t1=0;  
  17.     if(sqrt((old_theta0-theta0)^2+(old_theta1-theta1)^2)<0.000001) % 這里是判斷收斂的條件,當然可以有其他方法來做  
  18.         break;  
  19.     end  
  20. end  


2. 隨機梯度下降法

隨機梯度下降法適用于樣本點數量非常龐大的情況,算法使得總體向著梯度下降快的方向下降。

matlab源碼:

  1. function [theta0,theta1]=Gradient_descent_rand(X,Y);  
  2. theta0=0;  
  3. theta1=0;  
  4. t0=theta0;  
  5. t1=theta1;  
  6. for i=1:1:100  
  7.     t0=theta0-0.01*(theta0+theta1*X(i,1)-Y(i,1))*1  
  8.     t1=theta1-0.01*(theta0+theta1*X(i,1)-Y(i,1))*X(i,1)  
  9.     theta0=t0  
  10.     theta1=t1  
  11. end