梯度下降算法Python简单试验
使用随机梯度下降算法简单实现了求解函数f(x,y) = x^2 + y^2 最小值
1 from matplotlib import pyplot as plt 2 import numpy as np 3 from mpl_toolkits.mplot3d import Axes3D 4 5 fig = plt.figure() 6 7 def partialDerivative(xy): 8 return np.array([2*xy[0], 2*xy[1]]) 9 10 def f(xy): 11 return xy[0] **2 + xy[1] **2 12 13 xy=np.array([100,200]) 14 z = f(xy) 15 step=0.1 16 delta = 0.0001 17 i=0 18 while(i<1000): 19 fxy = partialDerivative(xy) 20 fxy = fxy * (-step) 21 22 xy = xy + fxy 23 24 zxy = f(xy) 25 if zxy - z < delta and z - zxy < delta: 26 break 27 28 z = zxy 29 print(i,fxy,xy,z) 30 i+=1 31 32 print (xy,z)