梯度下降

1,单变量梯度下降案例

y=f(x)=x^2

梯度下降的公式 

\Theta^1=\Theta^0-\bigtriangledown j'(\Theta^0)

import numpy as np
import matplotlib.pyplot as plt


def f(x):
    return x ** 2


def h(x):
    return 2 * x


xarray = []
yarray = []

x = 2
step = 0.8
f_change = f(x)
f_current = f(x)

xarray.append(x)
yarray.append(f_current)

while f_change > 1e-5:
    x = x - step * h(x)  # 梯度下降计算
    tmp = f(x)
    f_change = np.abs(f_current - tmp)
    f_current = tmp
    xarray.append(x)
    yarray.append(f_current)
print('最终结果:', (x, f_current))

fig = plt.figure()
x2 = np.arange(-2.1, 2.14, 0.05)
y2 = x2 ** 2
plt.plot(x2, y2, '-', color='#666666', linewidth=2)
plt.plot(xarray, yarray, 'bo--')
plt.title(u'$y=x^2$函数求解最小值,最终解为:x=%.2f,y=%.2f' % (x, f_current))
plt.show()

最终结果: (0.0015672832819200039, 2.4563768857859384e-06)

2,多变量梯度下降实例

实例函数

z=f(x)=x^2+y^2

\Theta^1=\Theta^0-\bigtriangledown j'(\Theta^0) 函数还是一样,只是原来是x,现在变成x,y

import numpy as np
import matplotlib.pyplot as plt


def f(x, y):
    return x ** 2 + y ** 2


def h(x):
    return 2 * x


xarray = []
yarray = []
zarray = []

x = 2
y = 2
step = 0.1
f_change = x ** 2 + y ** 2
f_current = f(x, y)

xarray.append(x)
yarray.append(y)
zarray.append(f_current)

while f_change > 1e-3:
    x = x - step * h(x)
    y = y - step * h(x)
    tmp = f(x, y)
    f_change = f_current - f(x, y)
    f_current = tmp
    xarray.append(x)
    yarray.append(y)
    zarray.append(f_current)
print('最终结果:', (x, y))

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)
x2 = np.arange(-2, 2, 0.2)
y2 = np.arange(-2, 2, 0.2)
x2, y2 = np.meshgrid(x2, y2)
z2 = x2 ** 2 + y2 ** 2

ax.plot_surface(x2, y2, z2, rstride=1, cstride=1, cmap='rainbow')
ax.plot(xarray, yarray, zarray, 'ro--')
# plt.title(u'$y=x^2$函数求解最小值,最终解为:x=%.2f,y=%.2f'%(x,f_current))
plt.savefig('2.png')
plt.show()

最终结果: (0.004835703278458518, 0.40386856262276655)

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值