import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
# 设置 matplotlib 参数以支持中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 定义函数和梯度计算
x, y = sp.symbols('x y')
f = (x - 1) ** 2 + (y - 2) ** 2
grad_f = sp.derive_by_array(f, [x, y])
def substitute_values(gradient, values):
"""
对梯度表达式进行代值
:param gradient: 梯度表达式
:param values: 变量及其对应值的字典
:return: 代值后的梯度向量
"""
return [g.subs(values) for g in gradient]
def plot_contour_and_path(func, path, xlim=(-1, 3), ylim=(0, 4)):
"""
绘制等高线图和梯度下降路径
:param func: 目标函数
:param path: 梯度下降路径
:param xlim: x 轴范围
:param ylim: y 轴范围
"""
x_vals = np.linspace(xlim[0], xlim[1], 400)
y_vals = np.linspace(ylim[0], ylim[1], 400)
X, Y = np.meshgrid(x_vals, y_vals)
Z = np.array([[func.subs({x: X[i, j], y: Y[i, j]}) for j in range(X.shape[1])] for i in range(X.shape[0])])
plt.contour(X, Y, Z, 20)
path = np.array(path)
plt.plot(path[:, 0], path[:, 1], 'ro-', label='Gradient Descent Path')
plt.scatter([1], [2], color='blue', label='Global Minimum (1, 2)')
plt.xlim(xlim)
plt.ylim(ylim)
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.title('二元函数梯度下降')
plt.legend()
plt.grid()
plt.show()
# 梯度下降算法
def gradient_descent(grad, start_point, learning_rate, num_iterations):
"""
梯度下降算法
:param grad: 梯度表达式
:param start_point: 起始点
:param learning_rate: 学习率
:param num_iterations: 迭代次数
:return: 梯度下降路径
"""
path = [start_point]
point = start_point
for _ in range(num_iterations):
grad_values = substitute_values(grad, {x: point[0], y: point[1]})
point = [point[0] - learning_rate * grad_values[0],
point[1] - learning_rate * grad_values[1]]
path.append(point)
print(point)
return path
# 可视化结果
def plot_gradient_descent_path(path, xlim=(-1, 3), ylim=(0, 4)):
"""
绘制梯度下降路径
:param path: 梯度下降路径
:param xlim: x 轴范围
:param ylim: y 轴范围
"""
path = np.array(path)
plt.plot(path[:, 0], path[:, 1], 'ro-', label='Gradient Descent Path')
plt.scatter([1], [2], color='blue', label='Global Minimum (1, 2)')
plt.xlim(xlim)
plt.ylim(ylim)
# 标注每个点的迭代次数
for i, (px, py) in enumerate(path):
plt.text(px, py, f'{i}', fontsize=9, verticalalignment='bottom', horizontalalignment='right')
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.title('二元函数梯度下降')
plt.legend()
plt.grid()
plt.show()
# 参数设置
start_point = [2.5, 3.5]
learning_rate = 0.1
num_iterations =100
# 执行梯度下降
path = gradient_descent(grad_f, start_point, learning_rate, num_iterations)
plot_gradient_descent_path(path)
运行结果