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.
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:
- Batch Gradient Descent: Uses the entire dataset to compute the gradient at each step.
- Stochastic Gradient Descent (SGD): Uses a single example to compute the gradient and update the parameters.
- Mini-batch Gradient Descent: Uses a subset of the dataset (mini-batch) to compute the gradient.
Mathematical Formulation
Given a loss function where are the parameters, the gradient descent update rule is:
where: • is the learning rate, • 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 . When this norm falls below a certain threshold , the algorithm is assumed to have converged:
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 between iterations:
where is a predefined small positive constant. If the change falls below , 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 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
- Gradient Descent for Linear Regression Exploding
- Gradient Descent in Matlab
- Gradient Descent Linear Regression in Java
- gradient descent seems to fail
- Gradient Descent vs Adagrad vs Momentum in TensorFlow
- Gram Schmidt with R
- gradient descent using python and numpy
- Gradient Descent with constraints lagrange multipliers

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 courseTrack 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.