使用随机梯度下降算法简单实现了求解函数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] **212 13 xy=np.array([100,200])14 z = f(xy)15 step=0.116 delta = 0.000117 i=018 while(i<1000):19 fxy = partialDerivative(xy)20 fxy = fxy * (-step)21 22 xy = xy + fxy23 24 zxy = f(xy)25 if zxy - z < delta and z - zxy < delta:26 break27 28 z = zxy29 print(i,fxy,xy,z)30 i+=131 32 print (xy,z)