一个梯度下降算法的例子

一个梯度下降算法的例子

flyfish
gradient descent

函数是f(x)=x**4-3*x**3+2 python写法

f(x)=x^4-3*(x^3)+2 C++ 写法

导数是 f’(x)=4*x**3-9*x**2

x从-50到50的图像
一个梯度下降算法的例子_第1张图片

x从-5到5的图像
一个梯度下降算法的例子_第2张图片

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(-5, 5, 0.001)
y=x**4-3*x**3+2
plt.plot(x,y)  
plt.show()


old = 0
new= 5 
step = 0.01
precision = 0.00001

def derivative(x):
    return 4*x**3-9*x**2

while abs(new - old) > precision:
    old = new
    new = new - step * derivative(new)

print (new)

结果是2.25

当new=6 开始时
经过迭代结果是2.2499646074278457

0.8032842278686305
0.840624861847519
0.8804622684298664
0.9229296507309586
0.9681455460305634
1.016205130521792
1.067169389942697
1.1210520795330405
1.1778046421472836
1.237299637824332
1.2993137782331108
1.3635123370474889
1.429437442158506
1.4965033788967752
1.5640022802344904
1.6311231270849003
1.6969855549473505
1.7606875094969714
1.821362659674955
1.8782404675959476
1.930700009196379
1.9783089472809865
2.0208417065692057
2.0582751831448975
2.090764845528107
2.118607415721545
2.1421976265432066
2.1619858762300224
2.178441640823547
2.1920251576443675
2.203167862727841
2.2122606942724694
2.2196486877629633
2.2256301304909205
2.2304587076907274
2.234347382687595
2.2374730902946083
2.239981621916576
2.2419923174775156
2.243602351591089
2.2448905184851697
2.2459204946033684
2.2467436015363202
2.2474011148628947
2.2479261740485823
2.2483453500247714
2.2486799240099864
2.2489469258218673
2.2491599737759116
2.2493299520940697
2.249465555993498
2.24957372949745
2.249660016570137
2.2497288424102844
2.24978373858824
2.2498275231061067
2.249862444322635
2.249890295941524
2.2499125088471215
2.24993022442776
2.249944353104799
2.2499556210437
2.2499646074278457

你可能感兴趣的:(深度学习)