1,单变量梯度下降案例
y=f(x)=
梯度下降的公式
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,多变量梯度下降实例
实例函数
函数还是一样,只是原来是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)