@cleardusk
2015-11-13T17:03:21.000000Z
字数 587
阅读 1370
GjzCVCode
算法部分
# coding:utf-8
'''Gradient desent algo, take F(x) = x^4-3x^3+2 for example'''
def F(x):
return x**4 - 3 * x**3 + 2
def f_derivative(x):
return 4 * x**3 - 9 * x**2
pass
def gd(x_new=None):
'''
x_new is the guess number
'''
# parameter
gamma = 0.01 # step size
precision = 0.00001
x_old = 0
# iteration
while abs(x_new - x_old) > precision:
x_old = x_new
x_new = x_old - gamma * f_derivative(x_old)
return x_new
pass
if __name__ == '__main__':
x = gd(2)
print 'Local minum occurs at', x
测试部分
from gd import *
def test():
x_guess = 6
x = gd(x_guess)
print 'guess x:', x_guess
print 'x:', x
print 'function value:', F(x)
print 'derivative value:', f_derivative(x)
if __name__ == '__main__':
test()