用Python实现梯度下降算法二阶导数,并画出3D图

细节知识和上一篇一样,只不过这个多了一个3D画图,

#梯度下降 == 导数值下降
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
#梯度下降算法是一个方法,是帮助我们找极值点的方法

def targetFunc(x1, x2):
    return (x1+x2)**2
    pass

def gradientFunc(x1, x2):
     a = x1 + (2*x1*x2)**0.5
     return a
     pass
def gradientFunc2(x1,x2):
    b = x2 + (2*x1*x2)**0.5
    return b
    pass
listx = []
#猜测的过程           猜的值    目标函数    梯度函数       步进系数    收敛条件
def gradientCal(initX1, initX2, targetFunc, gradientFunc, rating=0.1, tolent=0.000001, times = 500000):
    result = targetFunc(initX1,initX2)            #计算出initX1,initX2这个点的实际值
    listx.append((initX1, initX2))

    newX1 = initX1 - rating * gradientFunc(initX1,initX2)
    newX2 = initX2 - rating * gradientFunc2(initX1,initX2)
    newResult = targetFunc(newX1, newX2)
    reResult = np.abs(result - newResult)
    t = 0
    while reResult > tolent and t < times:
        t += 1
        initX1 = newX1
        initX2 = newX2
        result = newResult
        listx.append((initX1,initX2))
        newX1 = newX1 - rating*gradientFunc(newX1,newX2)
        newX2 = newX2 - rating*gradientFunc2(newX2,newX1)
        newResult1 = targetFunc(newX1,newX2)
        reResult = np.abs(result - newResult)
        pass
    return initX1, initX2
    pass

if __name__ == '__main__':
    print(gradientCal(10, 10, targetFunc, gradientFunc2))
    xs, ys = np.meshgrid(np.linspace(-50, 50, 1000), np.linspace(-50, 50, 1000))
    print(xs)
    print(xs.shape)
    print(ys.shape)

    z = targetFunc(xs, ys)
    print(z)
    result = []
    for row1, row2 in zip(xs, ys):
        row = []
        for x1, x2 in zip(row1, row2):
            row.append(targetFunc(x1, x2))
            pass
        result.append(row)
        pass
    print(np.array(result))

    # 画图
    ax = plt.figure().add_subplot(111, projection='3d')

    ax.plot_surface(xs, ys, z)

    plt.show()

运行效果图

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值