梯度下降实例

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np


def Fun(x, y):
    return x - y + 2 * x ** 2 + 2 * x * y + y ** 2


def PxFun(x, y):
    return 1 + 4 * x + 2 * y


def PyFun(x, y):
    return -1 + 2 * x + 2 * y


fig = plt.figure()
ax = Axes3D(fig)
X, Y = np.mgrid[-2:2:40j, -2:2:40j]
Z = Fun(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap="rainbow")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
step = 0.0008
x = 0
y = 0
tag_x = [x]
tag_y = [y]
tag_z = [Fun(x, y)]
new_x = x
new_y = y
Over = False
while not Over:
    new_x -= step * PxFun(x, y)
    new_y -= step * PyFun(x, y)
    if Fun(x, y) - Fun(new_x, new_y) < 7e-9:
        Over = True
    x = new_x
    y = new_y
    tag_x.append(x)
    tag_y.append(y)
    tag_z.append(Fun(x, y))
ax.plot(tag_x, tag_y, tag_z, 'r')
plt.title('(x,y)~(' + str(x) + "," + str(y) + ')')
plt.show()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值