import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import math from mpl_toolkits.mplot3d import Axes3D
# 解决中文显示问题 mpl.rcParams['font.sans-serif'] = [u'SimHei'] mpl.rcParams['axes.unicode_minus'] = False
1. 二维空间的梯度下降求解及可视化
1.1 二维空间梯度求解
# 定义示例函数 def f1(x): return 0.5 * (x - 0.25) ** 2 # 导函数 def h1(x): return 0.5 * 2 * (x - 0.25) # 使用梯度下降法求解 GD_X = [] GD_Y = [] x = 4 alpha = 0.5 f_change = f1(x) # 变化量 f_current = f_change GD_X.append(x) GD_Y.append(f_current) iter_num = 0 while f_change > 1e-10 and iter_num < 100: iter_num += 1 x = x - alpha * h1(x) tmp = f1(x) f_change = np.abs(f_current - tmp) f_current = tmp GD_X.append(x) GD_Y.append(f_current) print(u"最终结果为:(%.5f, %.5f)" % (x, f_current)) print(u"迭代过程中X的取值,迭代次数:%d" % iter_num) print(GD_X)
最终结果为:(0.25001, 0.00000) 迭代过程中X的取值,迭代次数:19 [4, 2.125, 1.1875, 0.71875, 0.484375, 0.3671875, 0.30859375, 0.279296875, 0.2646484375, 0.25732421875, 0.253662109375, 0.2518310546875, 0.25091552734375, 0.250457763671875, 0.2502288818359375, 0.25011444091796875, 0.2500572204589844, 0.2500286102294922, 0.2500143051147461, 0.25000715255737305]
1.2二维空间梯度可视化
# 构建数据 X = np.arange(-4, 4.5, 0.05) Y = np.array(list(map(lambda t: f1(t), X))) # 画图 plt.figure(facecolor='w') plt.plot(X, Y, 'r-', linewidth=2) plt.plot(GD_X, GD_Y, 'bo--', linewidth=2) plt.title(u'函数$y=0.5 * (θ - 0.25)^2$; \n学习率:%.3f; 最终解:(%.3f, %.3f);迭代次数:%d' % (alpha, x, f_current, iter_num)) plt.show()