gradient descent
convergence criteria
optimization algorithms
machine learning
iterative methods

Gradient descent convergence How to decide convergence?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Gradient descent is a fundamental optimization algorithm used extensively in machine learning and deep learning to minimize loss functions. The primary goal of gradient descent is to iteratively adjust the model parameters to reduce the error of predictions. Understanding when gradient descent has converged, meaning the algorithm has effectively minimized the loss function, is crucial for the efficiency and effectiveness of training machine learning models.

Key Concepts in Gradient Descent

Gradient descent operates by iteratively moving in the direction of the steepest decrease of the loss function, calculated by its gradient. The three main variants include:

  1. Batch Gradient Descent: Uses the entire dataset to compute the gradient at each step.
  2. Stochastic Gradient Descent (SGD): Uses a single example to compute the gradient and update the parameters.
  3. Mini-batch Gradient Descent: Uses a subset of the dataset (mini-batch) to compute the gradient.

Mathematical Formulation

Given a loss function J(θ)J(\theta) where θ\theta are the parameters, the gradient descent update rule is:

θ=θαJ(θ)\theta = \theta - \alpha \nabla J(\theta)

where: • α\alpha is the learning rate, • J(θ)\nabla J(\theta) is the gradient of the loss function with respect to the parameters.

Convergence in Gradient Descent

Deciding when gradient descent has converged is a critical aspect of training machine learning models. Convergence indicates that further iterations will not significantly decrease the loss function. Several criteria can be used to determine convergence:

1. Gradient Norm

One frequently used criterion involves monitoring the norm of the gradient J(θ)\| \nabla J(\theta) \|. When this norm falls below a certain threshold ϵ\epsilon, the algorithm is assumed to have converged:

J(θ)\<ϵ\| \nabla J(\theta) | \< \epsilon

A small gradient norm indicates that the changes in parameter values have become negligible.

2. Change in Loss

Function

Another practical approach is to track the change in the value of the loss function J(θ)J(\theta) between iterations:

J(θ(t))J(θ(t1))\<δ|J(\theta^{(t)}) - J(\theta^{(t-1)})| \< \delta

where δ\delta is a predefined small positive constant. If the change falls below δ\delta, the algorithm is considered converged.

3. Maximum Iterations

Often, a maximum number of iterations is set as a safeguard against endlessly running processes, especially in scenarios where computational cost is a concern.

4. Learning Rate

The choice of learning rate α\alpha influences convergence. A learning rate that's too large may cause the algorithm to oscillate or diverge, while one that's too small can result in slow convergence. Adaptive techniques like AdaGrad, RMSProp, and Adam provide mechanisms to adjust the learning rate dynamically for efficient convergence.

5. Validation Set Performance

Another practical approach is to monitor the model's performance on a validation set. If there is no significant improvement in performance metrics (e.g., accuracy, F1-score) over several iterations, this can be a signal to stop the training.

Example

Consider training a linear regression model using gradient descent. Here's a pseudocode example for mini-batch gradient descent checking convergence with change in loss:

Momentum Techniques: Implement momentum to speed up convergence in cases of high curvature or noisy gradients. • Adaptive Methods: Use methods like Adam, RMSProp to automatically adjust learning rates. • Learning Rate Schedules: Some problems benefit from gradually reducing the learning rate as the algorithm approaches convergence.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.