Java教程

梯度下降算法学习

本文主要是介绍梯度下降算法学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

梯度下降求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)

I6Z5KU.png

梯度下降求最小值

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)


I6ZW80.png

这篇关于梯度下降算法学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!