gradient descent using python and numpy
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Gradient Descent is a cornerstone optimization algorithm in machine learning and computational methods. It minimizes a function by iteratively moving in the direction of steepest descent as defined by the negative of the gradient. In this article, we'll delve into the mechanism of gradient descent, explore its variations, and demonstrate implementation using Python and NumPy.
Introduction to Gradient Descent
Gradient Descent is an iterative optimization algorithm, commonly used for finding the minimum of a function. The essence of the algorithm is grounded in calculus, specifically leveraging the gradient, which provides the direction and rate of fastest increase of a function.
The Gradient Vector
For a function , where is a vector, the gradient, denoted as , is a vector of partial derivatives:
The gradient points in the direction of the steepest increase of the function. Thus, for minimization, we take steps proportional to the negative of the gradient.
Gradient Descent Algorithm
Given a function , the objective is to find a point such that is minimized. The gradient descent algorithm iterates to update :
Here, is the learning rate, a hyperparameter that defines the size of the steps taken to reach a minimum.
Types of Gradient Descent
- Batch Gradient Descent: Computes the gradient using the entire dataset.
- Stochastic Gradient Descent (SGD): Uses a single randomly selected data point to compute the gradient, allowing for faster iterations.
- Mini-Batch Gradient Descent: A compromise between Batch and Stochastic, using a subset of the dataset.
Example: Minimizing a Quadratic Function
Consider a simple quadratic function . The goal is to find the minimum value.
Step 1: Compute the Gradient
For , the derivative .
Step 2: Implement in Python using NumPy
Choosing the Learning Rate
The learning rate () is crucial:
- Too large: Can overshoot the minimum and potentially diverge.
- Too small: Convergence is too slow and computationally expensive.
A common practice is to use learning rate schedules or adaptive learning rates (e.g., Adagrad, RMSprop, Adam) to adjust dynamically.
Convergence
- Convergence Criteria: Stop the gradient descent when the change in is smaller than a threshold.
- Local Minima: The algorithm can get trapped in local minima for non-convex functions.
Key Points Summary
| Concept | Explanation |
| Gradient | Vector of partial derivatives; direction of steepest ascent. |
| Learning Rate () | Step size parameter that controls movement along the gradient. |
| Batch GD | Updates using the entire dataset for each step. |
| Stochastic GD (SGD) | Updates using a single data sample per step, thus faster but with more variance. |
| Mini-Batch GD | Uses a subset of data, balancing efficiency and precision. |
| Adaptive Learning Rates | Techniques like Adam adjust based on past gradients. |
| Convergence | Determined by small changes in or dropping below a threshold. |
Extensions and Advanced Topics
- Momentum Optimization: Incorporates rolling averages of past gradients to accelerate convergence.
- Nesterov Accelerated Gradient (NAG): A variant of momentum that looks ahead to calculate the gradient.
- Second-Order Methods: Incorporate curvature information of the function (e.g., Newton's method).
Gradient Descent remains a fundamental algorithm central to machine learning and optimization problems. Mastery of its variants and implementation in NumPy is crucial for developing efficient models.

