梯度下降优化:核心算法详解
梯度下降优化:核心算法详解
引言
想象您被蒙住眼睛站在山上,迫切地寻找最低点。您只能感受到脚下地面的斜率。这基本上就是梯度下降为机器学习算法所做的。它是一个强大的优化算法,通过迭代地"下坡"移动来帮助我们找到模型的最佳参数,从而最小化成本函数。本文将揭开这个核心算法背后的魔法,使其对初学者和经验丰富的机器学习爱好者都易于理解。
理解地形:成本函数和梯度
在我们开始下坡之旅之前,我们需要理解地形。在机器学习中,我们的"山"由成本函数表示,也称为损失函数或目标函数。这个函数量化了我们的模型表现如何;较低的值表示更好的性能。我们的目标是找到最小化这个成本函数的参数(如神经网络中的权重和偏置)。
梯度是我们的指南针。它是一个向量,指向给定点处成本函数最陡峭上升的方向。直观地说,它告诉我们成本最大增加的方向。为了下降,我们只需沿梯度的相反方向移动。
在数学上,梯度是成本函数相对于每个参数的偏导数向量。对于简单的成本函数 J(θ),其中 θ 表示我们的参数,梯度表示为 ∇J(θ)。这个向量的每个元素表示当我们稍微调整相应参数时成本变化多少。
下降:梯度下降算法
梯度下降算法是一个迭代过程。我们从参数的初始猜测开始,并基于梯度重复更新它们,直到达到最小值(或对其的满意近似)。以下是简化的分解:
初始化参数: 从参数的随机或预定义值开始。
计算梯度: 使用微积分(或数值近似)计算 ∇J(θ)。
更新参数: 沿梯度的相反方向调整参数:
θ = θ - α ∇J(θ)
其中 α 是学习率,一个控制步长的超参数。较小的 α 导致较小的步长,可能收敛较慢但精度更高,而较大的 α 导致较大的步长,可能收敛较快但有过冲最小值的风险。
重复步骤2和3: 继续迭代,直到满足停止标准(例如,梯度足够小,成本函数停止显著减少,或达到最大迭代次数)。
以下是Python伪代码表示:
- # 初始化参数 theta
- theta = initialize_parameters()
- # 设置学习率 alpha
- alpha = 0.01
- # 迭代直到收敛
- while not converged:
- # 计算梯度
- gradient = calculate_gradient(theta)
- # 更新参数
- theta = theta - alpha * gradient
- # 检查收敛
- if convergence_criteria_met(theta):
- break
梯度下降的类型
梯度下降有几种变体,每种都有其优缺点:
批量梯度下降: 在每次迭代中使用整个数据集计算梯度。这导致准确的梯度估计,但对于大型数据集可能在计算上很昂贵。
随机梯度下降(SGD): 在每次迭代中仅使用单个数据点(或一小批数据点)计算梯度。这要快得多,但在梯度估计中引入噪声,导致更不稳定的下降。
小批量梯度下降: 批量GD和SGD之间的折衷,在每次迭代中使用数据的小随机子集(小批量)来计算梯度。这平衡了计算效率和梯度准确性。
实际应用和意义
梯度下降是许多机器学习模型背后的主力。它对训练以下模型至关重要:
神经网络: 用于调整权重和偏置以最小化预测误差。
线性回归: 通过最小化平方误差之和找到最佳拟合线。
逻辑回归: 用于优化模型参数以最大化正确分类数据点的可能性。
支持向量机(SVM): 某些SVM训练算法利用梯度下降来优化模型参数。
挑战和局限性
虽然非常强大,但梯度下降并非没有挑战:
局部最小值: 算法可能陷入局部最小值,这是其附近最低但不是全局最小值(绝对最低点)的点。
学习率选择: 选择正确的学习率至关重要。太小,收敛缓慢;太大,算法可能过冲最小值并无法收敛。
鞍点: 在高维空间中,算法可能陷入鞍点,其中梯度为零,但它不是最小值或最大值。
梯度下降的未来
梯度下降仍然是机器学习中的基础算法。正在进行的研究专注于提高其效率和稳健性,包括:
自适应学习率: 像Adam和RMSprop这样的算法动态调整每个参数的学习率,提高收敛速度和稳定性。
基于动量的方法: 这些技术为下降添加惯性,帮助逃离局部最小值并加速收敛。
二阶优化方法: 这些方法使用成本函数曲率信息(Hessian矩阵)来更有效地指导下降,但它们通常在计算上更昂贵。
实际代码示例
让我们通过Python代码来深入理解梯度下降算法:
- import numpy as np
- import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- # 1. 基本梯度下降实现
- def basic_gradient_descent():
- """基本梯度下降算法"""
- def cost_function(x):
- """成本函数: f(x) = x² + 2x + 1"""
- return x**2 + 2*x + 1
- def gradient_function(x):
- """梯度函数: f'(x) = 2x + 2"""
- return 2*x + 2
- def gradient_descent(cost_func, grad_func, x0, learning_rate=0.1, max_iterations=100):
- """梯度下降算法"""
- x = x0
- history = [x]
- costs = [cost_func(x)]
- for i in range(max_iterations):
- grad = grad_func(x)
- x = x - learning_rate * grad
- history.append(x)
- costs.append(cost_func(x))
- # 检查收敛
- if abs(grad) < 1e-6:
- break
- return x, history, costs
- # 运行梯度下降
- x0 = 5.0
- optimal_x, history, costs = gradient_descent(cost_function, gradient_function, x0)
- print(f"初始值: x = {x0}")
- print(f"最优值: x = {optimal_x:.6f}")
- print(f"成本值: f(x) = {cost_function(optimal_x):.6f}")
- print(f"迭代次数: {len(history)}")
- # 可视化
- x_plot = np.linspace(-3, 7, 100)
- y_plot = cost_function(x_plot)
- plt.figure(figsize=(15, 5))
- # 函数和优化路径
- plt.subplot(1, 3, 1)
- plt.plot(x_plot, y_plot, 'b-', label='f(x) = x² + 2x + 1')
- plt.plot(history, costs, 'ro-', label='优化路径')
- plt.plot(optimal_x, cost_function(optimal_x), 'go', markersize=10, label='最优解')
- plt.xlabel('x')
- plt.ylabel('f(x)')
- plt.title('梯度下降优化')
- plt.legend()
- plt.grid(True, alpha=0.3)
- # 成本函数收敛
- plt.subplot(1, 3, 2)
- plt.plot(costs, 'b-')
- plt.xlabel('迭代次数')
- plt.ylabel('成本值')
- plt.title('成本函数收敛')
- plt.grid(True, alpha=0.3)
- # 梯度收敛
- plt.subplot(1, 3, 3)
- gradients = [gradient_function(x) for x in history]
- plt.plot(gradients, 'r-')
- plt.axhline(y=0, color='k', linestyle='--', alpha=0.5)
- plt.xlabel('迭代次数')
- plt.ylabel('梯度值')
- plt.title('梯度收敛')
- plt.grid(True, alpha=0.3)
- plt.tight_layout()
- plt.show()
- return optimal_x, history, costs
- # 2. 学习率影响分析
- def learning_rate_analysis():
- """学习率对梯度下降的影响"""
- def cost_function(x):
- return x**2 + 2*x + 1
- def gradient_function(x):
- return 2*x + 2
- def gradient_descent_with_lr(cost_func, grad_func, x0, learning_rate, max_iterations=100):
- """带学习率的梯度下降"""
- x = x0
- history = [x]
- for i in range(max_iterations):
- grad = grad_func(x)
- x = x - learning_rate * grad
- history.append(x)
- if abs(grad) < 1e-6:
- break
- return x, history
- # 测试不同学习率
- learning_rates = [0.01, 0.1, 0.5, 1.0, 1.5]
- x0 = 5.0
- results = []
- for lr in learning_rates:
- optimal_x, history = gradient_descent_with_lr(cost_function, gradient_function, x0, lr)
- results.append({
- 'learning_rate': lr,
- 'optimal_x': optimal_x,
- 'iterations': len(history),
- 'history': history
- })
- print(f"学习率 {lr}: 最优值 = {optimal_x:.6f}, 迭代次数 = {len(history)}")
- # 可视化不同学习率的效果
- plt.figure(figsize=(15, 5))
- # 优化路径
- plt.subplot(1, 3, 1)
- x_plot = np.linspace(-3, 7, 100)
- y_plot = cost_function(x_plot)
- plt.plot(x_plot, y_plot, 'k-', alpha=0.3, label='成本函数')
- colors = ['red', 'blue', 'green', 'orange', 'purple']
- for i, result in enumerate(results):
- history = result['history']
- costs = [cost_function(x) for x in history]
- plt.plot(history, costs, f'{colors[i]}o-',
- label=f'LR={result["learning_rate"]}')
- plt.xlabel('x')
- plt.ylabel('f(x)')
- plt.title('不同学习率的优化路径')
- plt.legend()
- plt.grid(True, alpha=0.3)
- # 收敛速度
- plt.subplot(1, 3, 2)
- for i, result in enumerate(results):
- history = result['history']
- costs = [cost_function(x) for x in history]
- plt.plot(costs, f'{colors[i]}-', label=f'LR={result["learning_rate"]}')
- plt.xlabel('迭代次数')
- plt.ylabel('成本值')
- plt.title('收敛速度比较')
- plt.legend()
- plt.grid(True, alpha=0.3)
- # 迭代次数
- plt.subplot(1, 3, 3)
- lrs = [r['learning_rate'] for r in results]
- iterations = [r['iterations'] for r in results]
- plt.bar(range(len(lrs)), iterations, color=colors)
- plt.xlabel('学习率')
- plt.ylabel('迭代次数')
- plt.title('收敛所需迭代次数')
- plt.xticks(range(len(lrs)), lrs)
- plt.grid(True, alpha=0.3)
- plt.tight_layout()
- plt.show()
- return results
- # 3. 多维梯度下降
- def multidimensional_gradient_descent():
- """多维梯度下降"""
- def cost_function_2d(x, y):
- """二维成本函数: f(x,y) = x² + y²"""
- return x**2 + y**2
- def gradient_2d(x, y):
- """二维梯度: [∂f/∂x, ∂f/∂y] = [2x, 2y]"""
- return np.array([2*x, 2*y])
- def gradient_descent_2d(cost_func, grad_func, x0, learning_rate=0.1, max_iterations=100):
- """二维梯度下降"""
- x = np.array(x0, dtype=float)
- history = [x.copy()]
- costs = [cost_func(x[0], x[1])]
- for i in range(max_iterations):
- grad = grad_func(x[0], x[1])
- x = x - learning_rate * grad
- history.append(x.copy())
- costs.append(cost_func(x[0], x[1]))
- if np.linalg.norm(grad) < 1e-6:
- break
- return x, history, costs
- # 从不同起点运行
- starting_points = [np.array([3.0, 4.0]), np.array([-2.0, 1.0]), np.array([0.0, 5.0])]
- results = []
- for i, x0 in enumerate(starting_points):
- optimal_point, history, costs = gradient_descent_2d(cost_function_2d, gradient_2d, x0)
- results.append({
- 'start': x0,
- 'optimal': optimal_point,
- 'history': history,
- 'costs': costs
- })
- print(f"起点 {i+1}: {x0} -> 最优点: {optimal_point}, 成本: {cost_function_2d(optimal_point[0], optimal_point[1]):.6f}")
- # 可视化
- x = np.linspace(-4, 4, 100)
- y = np.linspace(-4, 4, 100)
- X, Y = np.meshgrid(x, y)
- Z = cost_function_2d(X, Y)
- plt.figure(figsize=(12, 8))
- # 等高线图
- plt.contour(X, Y, Z, levels=20, alpha=0.6)
- plt.colorbar(label='f(x, y)')
- # 优化路径
- colors = ['red', 'blue', 'green']
- for i, result in enumerate(results):
- history = np.array(result['history'])
- plt.plot(history[:, 0], history[:, 1], f'{colors[i]}o-',
- label=f'路径 {i+1}: {result["start"]} -> {result["optimal"]:.2f}')
- plt.plot(result['start'][0], result['start'][1], f'{colors[i]}s', markersize=10)
- plt.plot(result['optimal'][0], result['optimal'][1], f'{colors[i]}*', markersize=15)
- plt.xlabel('x')
- plt.ylabel('y')
- plt.title('多维梯度下降优化路径')
- plt.legend()
- plt.grid(True, alpha=0.3)
- plt.axis('equal')
- plt.show()
- return results
- # 4. 不同类型的梯度下降
- def gradient_descent_variants():
- """梯度下降变体比较"""
- # 生成数据
- np.random.seed(42)
- X = np.random.randn(100, 2)
- y = 2 * X[:, 0] + 3 * X[:, 1] + 1 + 0.1 * np.random.randn(100)
- def linear_model(X, w, b):
- """线性模型"""
- return np.dot(X, w) + b
- def mse_loss(y_true, y_pred):
- """均方误差损失"""
- return np.mean((y_true - y_pred) ** 2)
- def compute_gradient(X, y, w, b):
- """计算梯度"""
- y_pred = linear_model(X, w, b)
- dw = -2 * np.mean(X.T * (y - y_pred), axis=1)
- db = -2 * np.mean(y - y_pred)
- return dw, db
- # 批量梯度下降
- def batch_gradient_descent(X, y, learning_rate=0.01, max_iterations=1000):
- """批量梯度下降"""
- w = np.zeros(2)
- b = 0.0
- history = []
- for i in range(max_iterations):
- dw, db = compute_gradient(X, y, w, b)
- w = w - learning_rate * dw
- b = b - learning_rate * db
- y_pred = linear_model(X, w, b)
- loss = mse_loss(y, y_pred)
- history.append(loss)
- if i % 100 == 0:
- print(f"批量GD - Epoch {i}: 损失 = {loss:.6f}")
- return w, b, history
- # 随机梯度下降
- def stochastic_gradient_descent(X, y, learning_rate=0.01, max_iterations=1000):
- """随机梯度下降"""
- w = np.zeros(2)
- b = 0.0
- history = []
- for i in range(max_iterations):
- # 随机选择一个样本
- idx = np.random.randint(0, len(X))
- X_sample = X[idx:idx+1]
- y_sample = y[idx:idx+1]
- dw, db = compute_gradient(X_sample, y_sample, w, b)
- w = w - learning_rate * dw
- b = b - learning_rate * db
- if i % 100 == 0:
- y_pred = linear_model(X, w, b)
- loss = mse_loss(y, y_pred)
- history.append(loss)
- print(f"随机GD - Epoch {i}: 损失 = {loss:.6f}")
- return w, b, history
- # 小批量梯度下降
- def mini_batch_gradient_descent(X, y, batch_size=32, learning_rate=0.01, max_iterations=1000):
- """小批量梯度下降"""
- w = np.zeros(2)
- b = 0.0
- history = []
- for i in range(max_iterations):
- # 随机选择小批量
- indices = np.random.choice(len(X), batch_size, replace=False)
- X_batch = X[indices]
- y_batch = y[indices]
- dw, db = compute_gradient(X_batch, y_batch, w, b)
- w = w - learning_rate * dw
- b = b - learning_rate * db
- if i % 100 == 0:
- y_pred = linear_model(X, w, b)
- loss = mse_loss(y, y_pred)
- history.append(loss)
- print(f"小批量GD - Epoch {i}: 损失 = {loss:.6f}")
- return w, b, history
- # 运行比较
- print("=== 批量梯度下降 ===")
- w_batch, b_batch, history_batch = batch_gradient_descent(X, y)
- print("\n=== 随机梯度下降 ===")
- w_sgd, b_sgd, history_sgd = stochastic_gradient_descent(X, y)
- print("\n=== 小批量梯度下降 ===")
- w_mb, b_mb, history_mb = mini_batch_gradient_descent(X, y)
- # 结果比较
- print(f"\n批量GD: w = {w_batch}, b = {b_batch:.4f}")
- print(f"随机GD: w = {w_sgd}, b = {b_sgd:.4f}")
- print(f"小批量GD: w = {w_mb}, b = {b_mb:.4f}")
- print(f"真实值: w = [2, 3], b = 1")
- # 可视化比较
- plt.figure(figsize=(15, 5))
- # 损失收敛
- plt.subplot(1, 3, 1)
- epochs = range(0, len(history_batch) * 100, 100)
- plt.plot(epochs, history_batch, 'b-', label='批量GD')
- plt.plot(epochs, history_sgd, 'r-', label='随机GD')
- plt.plot(epochs, history_mb, 'g-', label='小批量GD')
- plt.xlabel('Epoch')
- plt.ylabel('损失')
- plt.title('损失收敛比较')
- plt.legend()
- plt.grid(True, alpha=0.3)
- # 参数收敛
- plt.subplot(1, 3, 2)
- methods = ['批量GD', '随机GD', '小批量GD']
- w1_values = [w_batch[0], w_sgd[0], w_mb[0]]
- w2_values = [w_batch[1], w_sgd[1], w_mb[1]]
- x = np.arange(len(methods))
- width = 0.35
- plt.bar(x - width/2, w1_values, width, label='w1', alpha=0.8)
- plt.bar(x + width/2, w2_values, width, label='w2', alpha=0.8)
- plt.axhline(y=2, color='red', linestyle='--', alpha=0.7, label='真实w1')
- plt.axhline(y=3, color='orange', linestyle='--', alpha=0.7, label='真实w2')
- plt.xlabel('方法')
- plt.ylabel('权重值')
- plt.title('参数收敛比较')
- plt.xticks(x, methods)
- plt.legend()
- plt.grid(True, alpha=0.3)
- # 预测质量
- plt.subplot(1, 3, 3)
- y_pred_batch = linear_model(X, w_batch, b_batch)
- y_pred_sgd = linear_model(X, w_sgd, b_sgd)
- y_pred_mb = linear_model(X, w_mb, b_mb)
- plt.scatter(y, y_pred_batch, alpha=0.6, label='批量GD')
- plt.scatter(y, y_pred_sgd, alpha=0.6, label='随机GD')
- plt.scatter(y, y_pred_mb, alpha=0.6, label='小批量GD')
- plt.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', alpha=0.5)
- plt.xlabel('真实值')
- plt.ylabel('预测值')
- plt.title('预测质量比较')
- plt.legend()
- plt.grid(True, alpha=0.3)
- plt.tight_layout()
- plt.show()
- return w_batch, w_sgd, w_mb
- # 5. 局部最小值和鞍点问题
- def local_minima_and_saddle_points():
- """局部最小值和鞍点问题"""
- def complex_function(x, y):
- """具有多个局部最小值的函数"""
- return np.sin(x) + 0.5 * x**2 + np.cos(y) + 0.3 * y**2
- def complex_gradient(x, y):
- """复杂函数的梯度"""
- dx = np.cos(x) + x
- dy = -np.sin(y) + 0.6 * y
- return np.array([dx, dy])
- def gradient_descent_with_momentum(cost_func, grad_func, x0, learning_rate=0.1, momentum=0.9, max_iterations=1000):
- """带动量的梯度下降"""
- x = np.array(x0, dtype=float)
- velocity = np.zeros_like(x)
- history = [x.copy()]
- for i in range(max_iterations):
- grad = grad_func(x[0], x[1])
- velocity = momentum * velocity - learning_rate * grad
- x = x + velocity
- history.append(x.copy())
- if np.linalg.norm(grad) < 1e-6:
- break
- return x, history
- # 从不同起点运行
- starting_points = [np.array([-3.0, 2.0]), np.array([1.0, -2.0]), np.array([0.0, 0.0])]
- results = []
- for i, x0 in enumerate(starting_points):
- optimal_point, history = gradient_descent_with_momentum(complex_function, complex_gradient, x0)
- results.append({
- 'start': x0,
- 'optimal': optimal_point,
- 'history': history,
- 'value': complex_function(optimal_point[0], optimal_point[1])
- })
- print(f"起点 {i+1}: {x0} -> 最优点: {optimal_point}, 函数值: {complex_function(optimal_point[0], optimal_point[1]):.6f}")
- # 可视化
- x = np.linspace(-4, 4, 100)
- y = np.linspace(-4, 4, 100)
- X, Y = np.meshgrid(x, y)
- Z = complex_function(X, Y)
- plt.figure(figsize=(15, 5))
- # 3D表面图
- ax1 = plt.subplot(1, 3, 1, projection='3d')
- surf = ax1.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8)
- ax1.set_title('复杂函数表面')
- ax1.set_xlabel('x')
- ax1.set_ylabel('y')
- ax1.set_zlabel('f(x,y)')
- # 等高线图
- ax2 = plt.subplot(1, 3, 2)
- contour = ax2.contour(X, Y, Z, levels=20)
- ax2.clabel(contour, inline=True, fontsize=8)
- colors = ['red', 'blue', 'green']
- for i, result in enumerate(results):
- history = np.array(result['history'])
- ax2.plot(history[:, 0], history[:, 1], f'{colors[i]}o-',
- label=f'路径 {i+1}: {result["start"]} -> {result["optimal"]:.2f}')
- ax2.plot(result['start'][0], result['start'][1], f'{colors[i]}s', markersize=10)
- ax2.plot(result['optimal'][0], result['optimal'][1], f'{colors[i]}*', markersize=15)
- ax2.set_title('优化路径')
- ax2.set_xlabel('x')
- ax2.set_ylabel('y')
- ax2.legend()
- ax2.grid(True, alpha=0.3)
- # 函数值收敛
- ax3 = plt.subplot(1, 3, 3)
- for i, result in enumerate(results):
- history = np.array(result['history'])
- values = [complex_function(x[0], x[1]) for x in history]
- ax3.plot(values, f'{colors[i]}-', label=f'路径 {i+1}')
- ax3.set_xlabel('迭代次数')
- ax3.set_ylabel('函数值')
- ax3.set_title('函数值收敛')
- ax3.legend()
- ax3.grid(True, alpha=0.3)
- plt.tight_layout()
- plt.show()
- return results
- # 6. 自适应学习率算法
- def adaptive_learning_rate_algorithms():
- """自适应学习率算法"""
- def rosenbrock_function(x):
- """Rosenbrock函数"""
- return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2
- def rosenbrock_gradient(x):
- """Rosenbrock函数梯度"""
- dx1 = -2 * (1 - x[0]) - 400 * x[0] * (x[1] - x[0]**2)
- dx2 = 200 * (x[1] - x[0]**2)
- return np.array([dx1, dx2])
- # AdaGrad
- def adagrad(cost_func, grad_func, x0, learning_rate=0.1, max_iterations=1000):
- """AdaGrad算法"""
- x = np.array(x0, dtype=float)
- G = np.zeros_like(x) # 梯度平方累积
- history = [x.copy()]
- for i in range(max_iterations):
- grad = grad_func(x)
- G += grad**2
- x = x - learning_rate * grad / (np.sqrt(G) + 1e-8)
- history.append(x.copy())
- if np.linalg.norm(grad) < 1e-6:
- break
- return x, history
- # RMSprop
- def rmsprop(cost_func, grad_func, x0, learning_rate=0.01, beta=0.9, max_iterations=1000):
- """RMSprop算法"""
- x = np.array(x0, dtype=float)
- v = np.zeros_like(x) # 移动平均
- history = [x.copy()]
- for i in range(max_iterations):
- grad = grad_func(x)
- v = beta * v + (1 - beta) * grad**2
- x = x - learning_rate * grad / (np.sqrt(v) + 1e-8)
- history.append(x.copy())
- if np.linalg.norm(grad) < 1e-6:
- break
- return x, history
- # Adam
- def adam(cost_func, grad_func, x0, learning_rate=0.01, beta1=0.9, beta2=0.999, max_iterations=1000):
- """Adam算法"""
- x = np.array(x0, dtype=float)
- m = np.zeros_like(x) # 一阶矩估计
- v = np.zeros_like(x) # 二阶矩估计
- history = [x.copy()]
- for i in range(max_iterations):
- grad = grad_func(x)
- m = beta1 * m + (1 - beta1) * grad
- v = beta2 * v + (1 - beta2) * grad**2
- # 偏差修正
- m_hat = m / (1 - beta1**(i+1))
- v_hat = v / (1 - beta2**(i+1))
- x = x - learning_rate * m_hat / (np.sqrt(v_hat) + 1e-8)
- history.append(x.copy())
- if np.linalg.norm(grad) < 1e-6:
- break
- return x, history
- # 比较算法
- x0 = np.array([-1.0, 1.0])
- algorithms = [
- ('AdaGrad', adagrad),
- ('RMSprop', rmsprop),
- ('Adam', adam)
- ]
- results = []
- for name, algorithm in algorithms:
- optimal_point, history = algorithm(rosenbrock_function, rosenbrock_gradient, x0)
- results.append({
- 'name': name,
- 'optimal': optimal_point,
- 'history': history,
- 'value': rosenbrock_function(optimal_point)
- })
- print(f"{name}: 最优点 = {optimal_point}, 函数值 = {rosenbrock_function(optimal_point):.6f}")
- # 可视化比较
- plt.figure(figsize=(15, 5))
- # 优化路径
- plt.subplot(1, 3, 1)
- x = np.linspace(-2, 2, 100)
- y = np.linspace(-1, 3, 100)
- X, Y = np.meshgrid(x, y)
- Z = np.zeros_like(X)
- for i in range(X.shape[0]):
- for j in range(X.shape[1]):
- Z[i, j] = rosenbrock_function([X[i, j], Y[i, j]])
- plt.contour(X, Y, Z, levels=20, alpha=0.6)
- colors = ['red', 'blue', 'green']
- for i, result in enumerate(results):
- history = np.array(result['history'])
- plt.plot(history[:, 0], history[:, 1], f'{colors[i]}o-',
- label=f'{result["name"]}: {result["optimal"]:.3f}')
- plt.plot(1, 1, 'k*', markersize=15, label='全局最优 (1,1)')
- plt.xlabel('x1')
- plt.ylabel('x2')
- plt.title('优化路径比较')
- plt.legend()
- plt.grid(True, alpha=0.3)
- # 函数值收敛
- plt.subplot(1, 3, 2)
- for i, result in enumerate(results):
- history = np.array(result['history'])
- values = [rosenbrock_function(x) for x in history]
- plt.plot(values, f'{colors[i]}-', label=result['name'])
- plt.xlabel('迭代次数')
- plt.ylabel('函数值')
- plt.title('收敛速度比较')
- plt.legend()
- plt.grid(True, alpha=0.3)
- plt.yscale('log')
- # 梯度范数
- plt.subplot(1, 3, 3)
- for i, result in enumerate(results):
- history = np.array(result['history'])
- gradients = [np.linalg.norm(rosenbrock_gradient(x)) for x in history]
- plt.plot(gradients, f'{colors[i]}-', label=result['name'])
- plt.xlabel('迭代次数')
- plt.ylabel('梯度范数')
- plt.title('梯度收敛比较')
- plt.legend()
- plt.grid(True, alpha=0.3)
- plt.yscale('log')
- plt.tight_layout()
- plt.show()
- return results
- # 运行所有示例
- if __name__ == "__main__":
- print("=== 基本梯度下降 ===")
- optimal_x, history, costs = basic_gradient_descent()
- print("\n=== 学习率分析 ===")
- lr_results = learning_rate_analysis()
- print("\n=== 多维梯度下降 ===")
- md_results = multidimensional_gradient_descent()
- print("\n=== 梯度下降变体 ===")
- variant_results = gradient_descent_variants()
- print("\n=== 局部最小值和鞍点 ===")
- local_min_results = local_minima_and_saddle_points()
- print("\n=== 自适应学习率算法 ===")
- adaptive_results = adaptive_learning_rate_algorithms()
总结
梯度下降是现代机器学习的基石。其直观的概念,广泛的适用性和持续的改进确保了其在塑造人工智能未来方面的持续重要性。理解其机制和局限性对于任何寻求掌握机器学习领域的人来说都是必不可少的。
关键要点:
基本原理:沿梯度相反方向迭代更新参数
学习率选择:平衡收敛速度和稳定性
算法变体:批量、随机、小批量梯度下降
挑战:局部最小值、鞍点、学习率调优
改进方法:动量、自适应学习率、二阶方法
实际应用:
神经网络训练:权重和偏置优化
线性模型:参数估计
深度学习:大规模模型训练
强化学习:策略优化
梯度下降算法不仅是理论工具,更是现代机器学习系统实现的核心技术。随着自动微分和高级优化算法的发展,梯度下降继续在人工智能领域发挥关键作用,推动着机器学习的快速发展。
