梯度下降法

来源:互联网 发布:中国方言软件 编辑:程序博客网 时间:2024/06/05 20:06

转载地址:http://blog.csdn.net/nupt123456789/article/details/8281923

梯度下降法是一个一阶迭代优化算法,通常也称为最速下降法。我之前也没有关注过这类算法。近,听斯坦福大学的机器学习课程时,碰到了用梯度下降算法求解线性回归问题,于是看了看这类算法的思想。今天只写了一些入门级的知识。




我们知道,函数的曲线如下:


编程实现:c++ code

[cpp] view plain copy
  1. /* 
  2.  * @author:郑海波 
  3.  * blog.csdn.net/nuptboyzhb/ 
  4.  * 2012-12-11 
  5.  */  
  6. #include <iostream>  
  7. #include <math.h>  
  8. using namespace std;  
  9. int main()  
  10. {  
  11.     double e=0.00001;//定义迭代精度  
  12.     double alpha=0.5;//定义迭代步长  
  13.     double x=0;//初始化x  
  14.     double y0=x*x-3*x+2;//与初始化x对应的y值  
  15.     double y1=0;//定义变量,用于保存当前值  
  16.     while (true)  
  17.     {  
  18.         x=x-alpha*(2.0*x-3.0);  
  19.         y1=x*x-3*x+2;  
  20.         if (abs(y1-y0)<e)//如果2次迭代的结果变化很小,结束迭代  
  21.         {  
  22.             break;  
  23.         }  
  24.         y0=y1;//更新迭代的结果  
  25.     }  
  26.     cout<<"Min(f(x))="<<y0<<endl;  
  27.     cout<<"minx="<<x<<endl;  
  28.     return 0;  
  29. }  
  30. //运行结果  
  31. //Min(f(x))=-0.25  
  32. //minx=1.5  
  33. //Press any key to continue  

问题:

迭代步长alpha为什么要选择0.5??选择其他的值可以吗?它的取值与迭代的次数、收敛性及结果的准确性有何关系?如何选择alpha的值?

*******************

  除了作者所说的alpha的取值,初值x0的取值应该怎么取?对于有多个峰值的优化函数,取得不好的话,应该会陷入局部极小值的。如何判断优化函数是单峰还是多峰,还有什么牛顿法,LM法又是什么意思??

*******************

WIKI给出的解释:

Gradient descent is a first-order iterative optimization algorithm. To find a local minimum of a function using gradient descent, one takes steps proportional to the negative of the gradient (or of the approximate gradient) of the function at the current point. If instead one takes steps proportional to the positive of the gradient, one approaches a local maximum of that function; the procedure is then known as gradient ascent.




从Wiki的解释可以看出:

  1 对于单峰(或者说是凸函数)来说,梯度下降法是全局最优解,其他情况下是局部最优;

   2 步长的选择可以通过线性搜索的方法获得


对于步长的选择可以用 backtracking line search(转载地址:http://www.cnblogs.com/fstang/p/4192735.html)

PS:下面的变量 t就是F在x处的更新公式中的步长,函数 f  就是我们优化的目标函数 ,即F。









0 0
原创粉丝点击