gradient descent
linear regression
optimization algorithms
machine learning
computational issues

Gradient Descent for Linear Regression Exploding

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

Introduction

Gradient Descent is a cornerstone of many machine learning algorithms, including linear regression. It offers a way to minimize the loss function by iteratively adjusting parameters. However, issues can arise, leading to scenarios like "gradient descent exploding." This article aims to explore what this phenomenon is, why it occurs, and how it can be mitigated, especially in the context of linear regression.

Understanding Gradient Descent in Linear Regression

Linear regression aims to find the best-fitting line by minimizing the Mean Squared Error (MSE) between the predicted and actual values. The MSE can be represented as:

MSE(θ)=12ni=1n(yi(mxi+b))2\text{MSE}(\theta) = \frac{1}{2n} \sum_{i=1}^{n} (y_i - (mx_i + b))^2

Gradient Descent seeks to find the optimal parameters θ=[m,b]\theta = [m, b] by updating them iteratively:

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

where:

  • α\alpha is the learning rate.
  • J(θ)\nabla J(\theta) is the gradient of the loss function.

Component of "Exploding" in Gradient Descent

"Gradient Descent Exploding" refers to scenarios where the algorithm diverges rather than converging to a minimum. This situation typically occurs due to the following reasons:

Learning Rate Issues

  • Too Large Learning Rate: A large learning rate can cause overshooting the minimum, leading to divergence. This could cause the model parameters to take excessively large steps, exponentially increasing the MSE.

Poor Feature Scaling

  • Unscaled Features: Features on different scales can cause gradients to explode. Features with large ranges can disproportionately affect the gradient, causing erratic updates.

Ill-Conditioned Problem

  • Condition Number: The condition number of the feature matrix (ratio of the largest to smallest singular values) can lead to issues. A high condition number implies the matrix is close to singular, causing numerical instability.

Example of Exploding Gradient Descent

Consider a simple linear regression model with two features:

Dataset:

  • Learning Rate Decay: Gradually reduce the learning rate after each iteration to stabilize updates.
  • Adaptive Learning Rate: Algorithms like AdaGrad, RMSprop, or Adam adjust the learning rate dynamically based on gradient history.
  • Standardization: Scale features to have zero mean and unit variance.
  • Normalization: Rescale features to a specific range, like [0, 1].
  • Regularization: Add regularization terms (L1 or L2) to penalize large coefficients and improve stability.
  • Condition Number Reduction: Apply singular value decomposition (SVD) techniques or preconditioning for better matrix conditioning.

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.