梯度下降算法学习
2021/11/14 9:39:39
本文主要是介绍梯度下降算法学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
梯度下降求f(x,y)=x^2 + y^2
先画个图
import numpy as np from matplotlib import interactive, pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import animation as amat "function:f()x,y = x^2 +y^2" def fangcheng(x,y): return np.power(x,2) + np.power(y,2) def show(X,Y,func=fangcheng): fig = plt.figure() ax = Axes3D(fig) X,Y = np.meshgrid(X,Y,sparse=True) Z = func(X,Y) plt.title("test f(x,y)=x^2 + y^2") ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap='rainbow') ax.set_xlabel('x label',color='r') ax.set_ylabel('y label',color='g') ax.set_zlabel('z label',color='b') amat.FuncAnimation(fig,fangcheng,frames=200,interval=20,blit=True) plt.show() if __name__ == '__main__': X = np.arange(-1.5,1.5,0.1) Y = np.arange(-1.5,1.5,0.1) Z = fangcheng(X,Y) show(X,Y,fangcheng)
梯度下降求最小值
import numpy as np from matplotlib import interactive, pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import animation as amat "function:f(x,y) = x^2 +y^2" def fangcheng(x,y): return np.power(x,2) + np.power(y,2) def draw(px,py,pz,X,Y,func=fangcheng): fig = plt.figure() ax = Axes3D(fig) X,Y = np.meshgrid(X,Y,sparse=True) Z = func(X,Y) plt.title("test f(x,y)=x^2 + y^2") ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap='rainbow',) ax.set_xlabel('x label',color='r') ax.set_ylabel('y label',color='g') ax.set_zlabel('z label',color='b') #amat.FuncAnimation(fig,fangcheng,frames=200,interval=20,blit=True) ax.plot(px,py,pz,'r.',color = "blue") plt.show() def tiduxiajiang(X,Y,maxcrcyles=100,learnrate=0.1): new_x = [X] new_y = [Y] gz = [fangcheng(X,Y)] current_x = X current_y = Y for cycle in range(maxcrcyles): current_y -=learnrate * 2 * Y current_x -=learnrate * 2 * X X = current_x Y = current_y new_x.append(X) new_y.append(Y) gz.append(fangcheng(X,Y)) return new_x,new_y,gz if __name__=='__main__': X = np.arange(-1.5,1.5,0.1) Y = np.arange(-1.5,1.5,0.1) x = 1 y = 3 print(x,y) x,y,z = tiduxiajiang(x,y) print(x,y,z) draw(x,y,z,X,Y,fangcheng)
这篇关于梯度下降算法学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现
- 2024-05-30我们小公司,哪像华为一样,用得上IPD(集成产品开发)?
- 2024-05-30java excel上传--poi
- 2024-05-30安装笔记本应用商店的pycharm,再安排pandas等模块,说是没有打包工具?
- 2024-05-29java11新特性
- 2024-05-29哪些无用敏捷指标正在破坏敏捷转型?
- 2024-05-29鸿蒙原生应用再新丁!新华社 入局鸿蒙
- 2024-05-29设计模式 之 迭代器模式(Iterator)