Gradient

梯度下降(Gradient Descent)是一种优化算法,用于通过最小化成本函数(Loss Function)来找到模型的最优参数。它广泛应用于各种机器学习模型,包括线性回归、逻辑回归和神经网络。

Gradient descent uses calculus to estimate how changing each parameter changes the cost. For example, increasing a parameter might be predicted to reduce the cost.

Gradient descent is named as such because it calculates the gradient (slope) of the relationship between each model parameter and the cost. The parameters are then altered to move down this slope.

This algorithm is simple and powerful, yet it isn’t guaranteed to find the optimal model parameters that minimize the cost. The two main sources of error are local minima and instability.

Local minima

Our previous example looked to do a good job, assuming that cost would have kept increasing when the parameter was smaller than 0 or greater than 10:

Plot of cost versus model parameter, with a minima for cost when the model parameter is five.

This job wouldn’t have been so great if parameters smaller than zero or larger than 10 would have resulted in lower costs, like in this image:

Plot of cost versus model parameter, with a local minima for cost when the model parameter is five but a lower cost when the model parameter is at negative six.

In the preceding graph, a parameter value of negative seven would have been a better solution than five, because it has a lower cost. Gradient descent doesn’t know the full relationship between each parameter and the cost—which is represented by the dotted line—in advance. Therefore, it’s prone to finding local minima: parameter estimates that aren’t the best solution, but the gradient is zero.

Instability

A related issue is that gradient descent sometimes shows instability. This instability usually occurs when the step size or learning rate—the amount that each parameter is adjusted by each iteration—is too large. The parameters are then adjusted too far on each step, and the model actually gets worse with each iteration:

Plot of cost versus model parameter, which shows cost moving in large steps with minimal decrease in cost.

Having a slower learning rate can solve this problem, but might also introduce issues. First, slower learning rates can mean training takes a long time, because more steps are required. Second, taking smaller steps makes it more likely that training settles on a local minima:

Plot of cost versus model parameter, showing small movements in cost.

By contrast, a faster learning rate can make it easier to avoid hitting local minima, because larger steps can skip over local maxima:

Plot of cost versus model parameter, with regular movements in cost until a minima is reached.

方法

梯度下降的基本思想是沿着成本函数的梯度(斜率)方向更新模型参数,以找到成本函数的最小值。梯度下降的步骤如下:

  1. 初始化参数:随机初始化模型参数(例如,权重和偏差)。
  2. 计算梯度:计算成本函数对每个参数的偏导数,即梯度。
  3. 更新参数:根据梯度的负方向调整参数。更新公式为: 其中:
    • 是模型参数。
    • 是学习率,决定了每次更新的步长。
    • 是成本函数。
  4. 重复步骤2和3:直到成本函数收敛到一个最小值,即参数调整不再显著降低成本函数值。

代码示例

以下是使用 Python 实现线性回归的梯度下降算法示例:

import numpy as np
 
# 生成样本数据
np.random.seed(0)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
 
# 梯度下降参数
learning_rate = 0.1
n_iterations = 1000
m = 100
 
# 初始化参数
theta = np.random.randn(2, 1)
 
# 增加 x0 = 1,以便同时计算截距和斜率
X_b = np.c_[np.ones((100, 1)), X]
 
# 梯度下降
for iteration in range(n_iterations):
    gradients = 2/m * X_b.T.dot(X_b.dot(theta) - y)
    theta -= learning_rate * gradients
 
print(f"模型参数: {theta}")

应用

梯度下降算法广泛应用于各种机器学习模型中,包括但不限于:

  • 线性回归:用于最小化均方误差(MSE)。
  • 逻辑回归:用于最小化对数损失(Log Loss)。
  • 神经网络:用于最小化预测误差,调整网络中的权重和偏差。

优缺点

优点

  • 简单易用:实现和理解都相对简单。
  • 适应性强:可以应用于各种不同类型的模型。

缺点

  • 局部极小值:可能会停留在局部极小值,而非全局最小值。
  • 不稳定性:学习率设置不当可能导致参数更新过大或过小,影响收敛效果。
  • 收敛速度:学习率过大可能导致震荡,学习率过小可能导致收敛速度缓慢。

类似算法

除了梯度下降,还有其他一些优化算法可以解决类似的问题:

动量梯度下降(Momentum Gradient Descent)

通过加入动量项来加速梯度下降的收敛过程,防止模型陷入局部极小值。

AdaGrad

自适应学习率算法,能够根据参数的变化自动调整学习率,适用于稀疏数据集。

RMSProp

改进的自适应学习率算法,通过指数加权移动平均来调整学习率,适用于非平稳目标。

Adam(Adaptive Moment Estimation)

结合了动量梯度下降和RMSProp的优点,既能够加速收敛,又能处理非平稳目标,广泛应用于深度学习中。


GridSearchCV

概念

GridSearchCV 是 Scikit-Learn 库中的一个模块,用于执行网格搜索(Grid Search)以优化模型参数。它遍历给定参数网格的所有可能组合,并使用交叉验证(Cross-Validation)来评估每个组合的性能,最终选择表现最好的参数组合。

方法

  1. 定义参数网格:指定待调参数及其候选值。
  2. 穷举搜索:遍历参数网格的所有可能组合。
  3. 交叉验证:对每个参数组合进行交叉验证,计算模型性能。
  4. 选择最佳参数:选择具有最佳交叉验证性能的参数组合。

代码示例

以下是使用 Python 实现 GridSearchCV 的示例:

from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
 
# 定义模型
model = RandomForestClassifier()
 
# 定义参数网格
param_grid = {
    'n_estimators': [50, 100, 200],
    'max_depth': [None, 10, 20, 30],
    'min_samples_split': [2, 5, 10]
}
 
# 执行网格搜索
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5, scoring='accuracy')
grid_search.fit(X_train, y_train)
 
# 输出最佳参数组合
print(f"最佳参数组合: {grid_search.best_params_}")

应用

GridSearchCV 广泛应用于各种机器学习模型的超参数调优。例如:

  • 随机森林:优化树的数量、树的最大深度等参数。
  • 支持向量机:优化核函数类型、正则化参数等。
  • K 近邻算法:优化邻居数量、距离度量方式等。

优缺点

优点

  • 系统全面:遍历所有可能的参数组合,确保找到全局最优解。
  • 易于理解和实现:通过简单的 API 调用即可完成复杂的参数搜索。

缺点

  • 计算复杂度高:参数组合数量多时,计算开销大,耗时长。
  • 不适合大数据集:在大数据集上执行时,可能导致内存不足或处理时间过长。

类似算法

除了 GridSearchCV,还有其他一些超参数优化算法:

RandomizedSearchCV

随机搜索方法,通过随机选择参数组合进行评估,减少计算复杂度。

Bayesian Optimization

贝叶斯优化,通过构建代理模型来估计和优化目标函数,以找到最优参数组合。

Hyperopt

基于树结构的贝叶斯优化算法,适用于高维参数空间的优化。