首页 > 代码库 > 梯度下降法

梯度下降法

梯度下降法是求解无约束最优化问题的一种常用方法。实现简单,代码如下。

 1 # -*- coding: utf8 -*-
 2 
 3 delta = 0.00001
 4 
 5 # f = x^2 + y^2
 6 my_function = lambda point : point[0] ** 2 + point[1] ** 2
 7 
 8 # 计算梯度
 9 def gradient(f, point):
10     global delta
11     grad = [0.0] * len(point)
12     for i in xrange(len(point)):
13         point_ = list(point)
14         point_[i] += delta
15         grad[i] = (f(point_) - f(point)) / delta
16     return grad
17 
18 # 梯度下降法
19 def graddesc(f, initpoint, step, epsilon):
20     curpoint = initpoint
21     while True:
22         ng = map(lambda x : -x, gradient(f, curpoint))          # 负梯度
23         nextpoint = map(lambda x,g : x+step*g, curpoint, ng)    # 下一个试探点
24         if abs(f(nextpoint) - f(curpoint)) < epsilon:           # 迭代终止
25             break
26         curpoint = nextpoint
27     return curpoint
28 
29 print my_function(graddesc(my_function, [-5, 4], 0.1, 0.000001))