批量梯度下降(Batch Gradient Descent)--------(含有具体数据示例)

批量梯度下降(Batch Gradient Descent)

批量梯度下降是一种优化算法,用于最小化损失函数,通过迭代地调整模型参数来逼近最优解。它在每次更新参数时使用整个训练数据集来计算梯度,因此也称为标准梯度下降(Standard Gradient Descent)。

批量梯度下降的原理

  1. 初始化参数:随机初始化模型参数。
  2. 计算梯度:使用整个训练数据集计算损失函数对模型参数的梯度。
  3. 更新参数:按照梯度的负方向更新参数。
  4. 迭代:重复计算梯度和更新参数,直到损失函数收敛或达到预设的迭代次数。

批量梯度下降的更新公式为:
θ = θ − α ∇ θ J ( θ ) \theta = \theta - \alpha \nabla_{\theta} J(\theta) θ=θαθJ(θ)
其中:
- θ \theta θ是模型参数。
- α \alpha α是学习率,控制每次更新的步长。
- ∇ θ J ( θ ) \nabla_{\theta} J(\theta) θJ(θ)是损失函数 J ( θ ) J(\theta) J(θ)对参数 θ \theta θ的梯度。

批量梯度下降的具体步骤

假设我们有一个简单的二次函数 J ( θ ) = 1 2 θ 2 J(\theta) = \frac{1}{2} \theta^2 J(θ)=21θ2,需要通过批量梯度下降找到函数的最小值。

步骤1:初始化参数

假设我们随机初始化参数 θ = 10 \theta = 10 θ=10

步骤2:计算梯度

函数 J ( θ ) = 1 2 θ 2 J(\theta) = \frac{1}{2} \theta^2 J(θ)=21θ2的梯度为 ∇ θ J ( θ ) = θ \nabla_{\theta} J(\theta) = \theta θJ(θ)=θ。当前 θ = 10 \theta = 10 θ=10,所以梯度 ∇ θ J ( 10 ) = 10 \nabla_{\theta} J(10) = 10 θJ(10)=10

步骤3:更新参数

设定学习率 α = 0.1 \alpha = 0.1 α=0.1,按照梯度下降的公式更新参数:
θ = θ − α ∇ θ J ( θ ) \theta = \theta - \alpha \nabla_{\theta} J(\theta) θ=θαθJ(θ)
θ = 10 − 0.1 × 10 = 10 − 1 = 9 \theta = 10 - 0.1 \times 10 = 10 - 1 = 9 θ=100.1×10=101=9

步骤4:迭代

重复上述步骤,继续迭代:

  • 第2次迭代
    θ = 9 \theta = 9 θ=9
    ∇ θ J ( 9 ) = 9 \nabla_{\theta} J(9) = 9 θJ(9)=9
    θ = 9 − 0.1 × 9 = 9 − 0.9 = 8.1 \theta = 9 - 0.1 \times 9 = 9 - 0.9 = 8.1 θ=90.1×9=90.9=8.1

  • 第3次迭代
    θ = 8.1 \theta = 8.1 θ=8.1
    ∇ θ J ( 8.1 ) = 8.1 \nabla_{\theta} J(8.1) = 8.1 θJ(8.1)=8.1
    θ = 8.1 − 0.1 × 8.1 = 8.1 − 0.81 = 7.29 \theta = 8.1 - 0.1 \times 8.1 = 8.1 - 0.81 = 7.29 θ=8.10.1×8.1=8.10.81=7.29

  • 第4次迭代
    θ = 7.29 \theta = 7.29 θ=7.29
    ∇ θ J ( 7.29 ) = 7.29 \nabla_{\theta} J(7.29) = 7.29 θJ(7.29)=7.29
    θ = 7.29 − 0.1 × 7.29 = 7.29 − 0.729 = 6.561 \theta = 7.29 - 0.1 \times 7.29 = 7.29 - 0.729 = 6.561 θ=7.290.1×7.29=7.290.729=6.561

通过多次迭代, θ \theta θ值逐步减小,最终会收敛到函数的最小值 θ = 0 \theta = 0 θ=0

具体数据示例

假设我们有一个线性回归问题,用批量梯度下降来优化模型参数。给定训练数据集 ( x i , y i ) (x_i, y_i) (xi,yi)如下:

xy
12
23
34
45

我们要拟合的线性模型为 h ( θ ) = θ 0 + θ 1 x h(\theta) = \theta_0 + \theta_1 x h(θ)=θ0+θ1x

初始化参数

假设 θ 0 = 0 \theta_0 = 0 θ0=0 θ 1 = 0 \theta_1 = 0 θ1=0,学习率 α = 0.01 \alpha = 0.01 α=0.01

计算梯度

损失函数 J ( θ ) J(\theta) J(θ)为均方误差:
J ( θ ) = 1 2 m ∑ i = 1 m ( h ( θ ) − y i ) 2 J(\theta) = \frac{1}{2m} \sum_{i=1}^{m} (h(\theta) - y_i)^2 J(θ)=2m1i=1m(h(θ)yi)2
其中 m m m是训练样本的数量,梯度为:
∇ θ 0 J ( θ ) = 1 m ∑ i = 1 m ( h ( θ ) − y i ) \nabla_{\theta_0} J(\theta) = \frac{1}{m} \sum_{i=1}^{m} (h(\theta) - y_i) θ0J(θ)=m1i=1m(h(θ)yi)
∇ θ 1 J ( θ ) = 1 m ∑ i = 1 m ( h ( θ ) − y i ) x i \nabla_{\theta_1} J(\theta) = \frac{1}{m} \sum_{i=1}^{m} (h(\theta) - y_i) x_i θ1J(θ)=m1i=1m(h(θ)yi)xi

对初始参数 θ 0 = 0 \theta_0 = 0 θ0=0 θ 1 = 0 \theta_1 = 0 θ1=0,计算梯度:

- h ( θ ) = 0 h(\theta) = 0 h(θ)=0
- ∇ θ 0 J ( θ ) = 1 4 ∑ i = 1 4 ( 0 − y i ) = − 1 4 ( 2 + 3 + 4 + 5 ) = − 3.5 \nabla_{\theta_0} J(\theta) = \frac{1}{4} \sum_{i=1}^{4} (0 - y_i) = -\frac{1}{4} (2 + 3 + 4 + 5) = -3.5 θ0J(θ)=41i=14(0yi)=41(2+3+4+5)=3.5
- ∇ θ 1 J ( θ ) = 1 4 ∑ i = 1 4 ( 0 − y i ) x i = − 1 4 ( 2 × 1 + 3 × 2 + 4 × 3 + 5 × 4 ) = − 7.5 \nabla_{\theta_1} J(\theta) = \frac{1}{4} \sum_{i=1}^{4} (0 - y_i) x_i = -\frac{1}{4} (2 \times 1 + 3 \times 2 + 4 \times 3 + 5 \times 4) = -7.5 θ1J(θ)=41i=14(0yi)xi=41(2×1+3×2+4×3+5×4)=7.5

更新参数

按照梯度下降公式更新参数:
θ 0 = θ 0 − α ∇ θ 0 J ( θ ) = 0 − 0.01 × ( − 3.5 ) = 0.035 \theta_0 = \theta_0 - \alpha \nabla_{\theta_0} J(\theta) = 0 - 0.01 \times (-3.5) = 0.035 θ0=θ0αθ0J(θ)=00.01×(3.5)=0.035
θ 1 = θ 1 − α ∇ θ 1 J ( θ ) = 0 − 0.01 × ( − 7.5 ) = 0.075 \theta_1 = \theta_1 - \alpha \nabla_{\theta_1} J(\theta) = 0 - 0.01 \times (-7.5) = 0.075 θ1=θ1αθ1J(θ)=00.01×(7.5)=0.075

多次迭代

继续计算新的梯度并更新参数,直至收敛。

总结

批量梯度下降在每次参数更新时使用整个训练数据集来计算梯度,保证了梯度估计的准确性和参数更新的稳定性。通过具体数据的举例说明,可以清楚地看到梯度下降优化如何逐步逼近最优解。在实际应用中,选择合适的学习率和迭代次数,可以有效地优化模型参数,使模型达到最佳性能。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
mini-batch gradient descent(小批量梯度下降)是梯度下降算法的一种变种,它在每个迭代步骤中计算部分训练样本的梯度,并更新模型权重。相比于批量梯度下降,mini-batch gradient descent具有更小的计算和内存需求,并且通常可以更快地找到较好的优化方向。 以下是mini-batch gradient descent的代码实现: 1. 初始化模型的权重和偏置。 2. 定义批量大小(batch_size),即每次更新模型参数时使用的样本数量。 3. 将训练集划分为大小为batch_size的小批量并进行迭代。 4. 对于每个小批量样本,计算其梯度。 5. 对于每个模型参数,使用计算得到的梯度和学习率来更新参数。 6. 重复步骤3-5,直到达到设定的迭代次数或收敛条件。 以下是一个简单的mini-batch gradient descent的Python代码示例: ```python # 计算梯度 def calculate_gradient(X, y, weights): # 根据模型参数计算预测值 y_pred = np.dot(X, weights) # 计算预测误差 error = y_pred - y # 计算梯度 gradient = np.dot(X.T, error) / len(X) return gradient # 更新模型参数 def update_weights(weights, gradient, learning_rate): new_weights = weights - learning_rate * gradient return new_weights # mini-batch gradient descent def mini_batch_gradient_descent(X, y, batch_size, learning_rate, num_iterations): # 初始化模型参数 weights = np.zeros(X.shape[1]) # 迭代更新模型参数 for _ in range(num_iterations): # 划分小批量样本 random_indices = np.random.choice(len(X), size=batch_size, replace=False) X_batch = X[random_indices] y_batch = y[random_indices] # 计算梯度 gradient = calculate_gradient(X_batch, y_batch, weights) # 更新模型参数 weights = update_weights(weights, gradient, learning_rate) return weights # 调用mini-batch gradient descent函数进行模型训练 weights = mini_batch_gradient_descent(X_train, y_train, batch_size=32, learning_rate=0.01, num_iterations=1000) ``` 在上述代码中,`X`和`y`分别表示训练数据集和目标变量,`weights`为模型的参数,`batch_size`为批量大小,`learning_rate`为学习率,`num_iterations`为迭代次数。 在每次迭代中,算法会将训练数据集随机划分为大小为`batch_size`的小批量样本,然后根据这些小批量样本计算梯度,并使用学习率来更新模型参数。重复多次迭代后,算法将得到最优的模型参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值