gradient descent using python and numpy
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 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.
Related reading
- Gradient Descent with constraints lagrange multipliers
- Graph disconnected cannot obtain value for tensor Tensor
- Graph optimizations on a tensorflow serveable created using tf.Estimator
- GridSearch for an estimator inside a OneVsRestClassifier
- Gram Schmidt with R
- Graph auto-layout algorithm
- Gradient Descent vs Adagrad vs Momentum in TensorFlow
- Graph algorithm simplify graph by replacing chains of nodes with single node

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.