Gradient Descent: Types and Applications Explained

Gradient Descent is a fundamental optimization algorithm used to minimize the loss function of a machine learning model. It works by iteratively adjusting the model’s parameters in the direction of the negative gradient of the loss function—this direction corresponds to the steepest decrease in the loss value.

Gradient descent is the backbone of training for nearly all deep learning models (e.g., neural networks, Transformers, CNNs) and many traditional machine learning models (e.g., linear regression, logistic regression). Its core idea is inspired by calculus: the gradient of a function at a point tells you the direction of the steepest increase, so moving in the opposite direction minimizes the function.


I. Core Concepts

1. Loss Function and Gradient

Let the model’s parameters be represented by a vector \(\theta = [\theta_1, \theta_2, …, \theta_n]\) (e.g., weights and biases of a neural network). The loss function \(L(\theta)\) quantifies how well the model’s predictions match the true labels (e.g., MSE for regression, cross-entropy for classification).

The gradient of the loss function with respect to \(\theta\) is a vector of partial derivatives:

\(\nabla L(\theta) = \left[ \frac{\partial L(\theta)}{\partial \theta_1}, \frac{\partial L(\theta)}{\partial \theta_2}, …, \frac{\partial L(\theta)}{\partial \theta_n} \right]^T\)

The gradient points in the direction of the steepest increase in the loss function. To minimize \(L(\theta)\), we update the parameters in the negative gradient direction.

2. Parameter Update Rule

The core update rule for gradient descent is:

\(\theta_{t+1} = \theta_t – \alpha \cdot \nabla L(\theta_t)\)

Where:

  • \(\theta_t\): Parameters at iteration t.
  • \(\alpha\): Learning rate (a hyperparameter that controls the step size of each update).
  • \(\nabla L(\theta_t)\): Gradient of the loss function at \(\theta_t\).

3. Learning Rate (\(\alpha\)): A Critical Hyperparameter

The learning rate determines how large each step is:

  • Too small \(\alpha\): The algorithm converges very slowly, requiring many iterations to reach the minimum.
  • Too large \(\alpha\): The algorithm may overshoot the minimum, oscillate around it, or even diverge (loss increases over time).
  • Optimal \(\alpha\): Balances convergence speed and stability, reaching the minimum in a reasonable number of iterations.

II. Types of Gradient Descent

Gradient descent is categorized based on how much data is used to compute the gradient in each iteration. The three main variants are:

1. Batch Gradient Descent (BGD)

Batch Gradient Descent computes the gradient using the entire training dataset in each iteration.

Update Rule

\(\theta_{t+1} = \theta_t – \alpha \cdot \frac{1}{m} \sum_{i=1}^m \nabla L(\theta_t; x_i, y_i)\)

Where m is the total number of training samples.

Key Properties

  • Advantages:
    • Computes the true gradient of the loss function (no noise).
    • Converges to the global minimum (for convex loss functions) or a local minimum (for non-convex functions).
  • Disadvantages:
    • Computationally expensive: Requires processing the entire dataset in each iteration—impractical for large datasets (e.g., millions of samples).
    • Memory-intensive: Needs to load the entire dataset into memory at once.

Use Case

Small datasets where computational efficiency is not a constraint (e.g., linear regression on a dataset with 1,000 samples).

2. Stochastic Gradient Descent (SGD)

Stochastic Gradient Descent computes the gradient using a single random training sample in each iteration. The term “stochastic” refers to the random selection of the sample.

Update Rule

\(\theta_{t+1} = \theta_t – \alpha \cdot \nabla L(\theta_t; x_i, y_i)\)

Where \(x_i, y_i\) is a single randomly selected sample from the training set.

Key Properties

  • Advantages:
    • Fast updates: Only one sample is processed per iteration—much faster than BGD for large datasets.
    • Escape local minima: The noise in the gradient can help the model escape shallow local minima and find better minima (useful for non-convex loss functions).
  • Disadvantages:
    • Noisy gradients: The gradient computed from a single sample is an approximation of the true gradient, leading to oscillations around the minimum.
    • Requires learning rate scheduling: A fixed learning rate may cause oscillations; a decaying learning rate (e.g., \(\alpha = \alpha_0 / \sqrt{t}\)) helps stabilize convergence.

Use Case

Large datasets (e.g., image classification with ImageNet) where BGD is infeasible.

3. Mini-Batch Gradient Descent

Mini-Batch Gradient Descent is a compromise between BGD and SGD—it computes the gradient using a small subset of the training data (called a mini-batch) in each iteration. This is the most widely used variant in deep learning.

Update Rule

\(\theta_{t+1} = \theta_t – \alpha \cdot \frac{1}{b} \sum_{i=1}^b \nabla L(\theta_t; x_i, y_i)\)

Where b is the mini-batch size (typically 16, 32, 64, or 128).

Key Properties

  • Advantages:
    • Balances speed and stability: Faster than BGD (processes small batches) and less noisy than SGD (averages gradients over multiple samples).
    • GPU-friendly: Mini-batches align with the parallel computing capabilities of GPUs, significantly accelerating training.
  • Disadvantages:
    • Requires tuning the mini-batch size (a hyperparameter). A batch size that is too large reduces speed; a batch size that is too small introduces noise.

Use Case

Nearly all deep learning applications (CNNs, Transformers, RL models) due to its balance of speed and stability.


III. Gradient Descent Optimizers (Variants with Momentum)

Basic gradient descent can be slow to converge, especially when the loss function has elongated valleys (common in deep learning). To address this, optimized variants of gradient descent add momentum or adaptive learning rates. Here are the most common ones:

1. Momentum

Momentum adds a “velocity” term to the parameter update, which accumulates the gradient over previous iterations. This helps the algorithm accelerate in the direction of consistent gradients and dampen oscillations.

Update Rule

\(v_{t+1} = \gamma v_t + \nabla L(\theta_t)\)

\(\theta_{t+1} = \theta_t – \alpha v_{t+1}\)

Where \(\gamma\) is the momentum coefficient (typically 0.9). Momentum mimics physical inertia—once the algorithm starts moving in a direction, it continues with that momentum.

2. RMSprop

RMSprop (Root Mean Square Propagation) adapts the learning rate for each parameter by dividing the gradient by the root mean square (RMS) of past gradients. This reduces the learning rate for parameters with large gradients (preventing overshooting) and increases it for parameters with small gradients (speeding up convergence).

Update Rule

\(E[g^2]_t = \gamma E[g^2]_{t-1} + (1-\gamma) (\nabla L(\theta_t))^2\)

\(\theta_{t+1} = \theta_t – \alpha \cdot \frac{\nabla L(\theta_t)}{\sqrt{E[g^2]_t + \epsilon}}\)

Where \(\epsilon\) is a small constant (e.g., \(10^{-8}\)) to avoid division by zero.

3. Adam (Adaptive Moment Estimation)

Adam is the most popular optimizer in deep learning—it combines the advantages of Momentum (accumulates past gradients) and RMSprop (adaptive learning rates). Adam maintains two moving averages:

  1. A moving average of the gradients (momentum term).
  2. A moving average of the squared gradients (adaptive learning rate term).

Update Rule

\(m_t = \beta_1 m_{t-1} + (1-\beta_1) \nabla L(\theta_t) \quad (\text{first moment})\)

\(v_t = \beta_2 v_{t-1} + (1-\beta_2) (\nabla L(\theta_t))^2 \quad (\text{second moment})\)

\(\hat{m}_t = \frac{m_t}{1-\beta_1^t} \quad (\text{bias correction})\)

\(\hat{v}_t = \frac{v_t}{1-\beta_2^t} \quad (\text{bias correction})\)

\(\theta_{t+1} = \theta_t – \alpha \cdot \frac{\hat{m}_t}{\sqrt{\hat{v}_t} + \epsilon}\)

Default hyperparameters: \(\beta_1=0.9\), \(\beta_2=0.999\), \(\epsilon=10^{-8}\). Adam is robust to hyperparameter choices and works well for most deep learning tasks.


IV. Implementation (Python with PyTorch)

Below is a simple implementation of mini-batch gradient descent and Adam for training a linear regression model.

1. Linear Regression Model

python

运行

import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt

# Generate synthetic data: y = 2x + 1 + noise
np.random.seed(42)
x = np.random.rand(100, 1)
y = 2 * x + 1 + 0.1 * np.random.randn(100, 1)

# Convert to PyTorch tensors
x_tensor = torch.from_numpy(x).float()
y_tensor = torch.from_numpy(y).float()

# Define linear regression model (y = wx + b)
class LinearRegression(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(1, 1)  # 1 input feature, 1 output feature

    def forward(self, x):
        return self.linear(x)

2. Train with Mini-Batch Gradient Descent

python

运行

# Initialize model, loss, and optimizer (SGD with momentum)
model = LinearRegression()
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

# Training loop (mini-batch gradient descent)
batch_size = 10
num_epochs = 100
loss_history = []

for epoch in range(num_epochs):
    # Shuffle data (critical for mini-batch SGD)
    permutation = torch.randperm(x_tensor.size(0))
    
    epoch_loss = 0.0
    for i in range(0, x_tensor.size(0), batch_size):
        # Get mini-batch
        indices = permutation[i:i+batch_size]
        x_batch = x_tensor[indices]
        y_batch = y_tensor[indices]
        
        # Forward pass
        y_pred = model(x_batch)
        loss = criterion(y_pred, y_batch)
        
        # Backward pass and optimize (compute gradient + update parameters)
        optimizer.zero_grad()  # Reset gradients to zero
        loss.backward()  # Compute gradient of loss w.r.t. parameters
        optimizer.step()  # Update parameters using gradient descent
        
        epoch_loss += loss.item() * x_batch.size(0)
    
    # Average loss per epoch
    avg_loss = epoch_loss / x_tensor.size(0)
    loss_history.append(avg_loss)
    
    if (epoch+1) % 10 == 0:
        print(f'Epoch {epoch+1}/{num_epochs}, Loss: {avg_loss:.4f}')

# Plot loss history
plt.plot(loss_history)
plt.xlabel('Epoch')
plt.ylabel('MSE Loss')
plt.title('Mini-Batch Gradient Descent Loss History')
plt.show()

# Print learned parameters (should be close to w=2, b=1)
print(f'Learned w: {model.linear.weight.item():.2f}, Learned b: {model.linear.bias.item():.2f}')

3. Train with Adam Optimizer

Replace the optimizer with Adam to see faster convergence:

python

运行

optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

Adam will typically converge to the minimum in fewer epochs than basic SGD.


V. Key Challenges and Solutions

1. Vanishing/Exploding Gradients

  • Vanishing Gradients: Gradients become extremely small as they propagate backward through deep networks, leading to slow convergence.
    • Solutions: Use activation functions like ReLU (avoids saturation), batch normalization, residual connections (e.g., ResNet).
  • Exploding Gradients: Gradients become extremely large, causing parameter updates to diverge.
    • Solutions: Gradient clipping (clip gradients to a maximum norm), weight initialization (e.g., Xavier/Glorot initialization).

2. Local Minima and Saddle Points

Non-convex loss functions (common in deep learning) have local minima and saddle points (points where the gradient is zero but are not minima).

  • Solutions: Use momentum or Adam to escape shallow local minima; add noise to the gradients; use a larger learning rate initially.

VI. Summary

Challenges: Vanishing/exploding gradients, local minima—mitigated with activation functions, batch normalization, and advanced optimizers.

Gradient Descent is an optimization algorithm that minimizes the loss function by updating parameters in the negative gradient direction.

Key Variants:

BGD: Uses the entire dataset per iteration (stable but slow).

SGD: Uses one sample per iteration (fast but noisy).

Mini-Batch GD: Uses small batches (best balance—used in most deep learning tasks).

Optimized Optimizers: Adam, RMSprop, and Momentum improve convergence speed and stability by adding momentum or adaptive learning rates.

Critical Hyperparameters: Learning rate (\(\alpha\)) and mini-batch size—must be tuned for optimal performance.



了解 Ruigu Electronic 的更多信息

订阅后即可通过电子邮件收到最新文章。

Posted in

Leave a comment