美文网首页
一维梯度下降算法

一维梯度下降算法

作者: 光头披风侠 | 来源:发表于2019-08-20 21:17 被阅读0次

一维梯度下降算法

import sympy as sy
import matplotlib.pyplot as plt
import numpy as np

def Gradient_descent(x0):
    w = 1e-8
    delta = 0.01
    x = sy.symbols('x')
    f = x**4 + 13 * x**3 + 35 * x**2 - 85 * x
y_index = np.arange(-8, 3, 0.01)
y = []
for i in y_index:
    y.append(f.subs(x, i))
plt.plot(y_index, y)
plt.show()
delta_f = sy.diff(f, x, 1)
f_start = f.subs(x, x0)
t = x0
while True:
    f_end = f_start
    t += -delta * delta_f.subs(x, t)
    f_start = f.subs(x, t)
    if(abs(f_end - f_start) < w):
        break
print(f_start, f_end, t)
if __name__ == '__main__':
    Gradient_descent(-6)

相关文章

网友评论

      本文标题:一维梯度下降算法

      本文链接:https://www.haomeiwen.com/subject/phbmsctx.html