gradient descent
python
numpy
machine learning
optimization

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 f(boldsymbolx)f(\\boldsymbol{x}), where boldsymbolx\\boldsymbol{x} is a vector, the gradient, denoted as nablaf(boldsymbolx)\\nabla f(\\boldsymbol{x}), is a vector of partial derivatives:

nablaf(boldsymbolx)=(partialfpartialx1,partialfpartialx2,ldots,partialfpartialxn)\\nabla f(\\boldsymbol{x}) = \left( \frac{\\partial f}{\\partial x_1}, \frac{\\partial f}{\\partial x_2}, \\ldots, \frac{\\partial f}{\\partial x_n} \right) 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 f:mathbbRnmathbbRf: \\mathbb{R}^n \rightarrow \\mathbb{R}, the objective is to find a point boldsymbolx\\boldsymbol{x}^* such that f(boldsymbolx)f(\\boldsymbol{x}^*) is minimized. The gradient descent algorithm iterates to update boldsymbolx\\boldsymbol{x}:

boldsymbolxk+1=boldsymbolxketanablaf(boldsymbolxk)\\boldsymbol{x}_{k+1} = \\boldsymbol{x}_k - \\eta \\nabla f(\\boldsymbol{x}_k) Here, eta\\eta is the learning rate, a hyperparameter that defines the size of the steps taken to reach a minimum.

Types of Gradient Descent

  1. Batch Gradient Descent: Computes the gradient using the entire dataset.
  2. Stochastic Gradient Descent (SGD): Uses a single randomly selected data point to compute the gradient, allowing for faster iterations.
  3. 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 f(x)=x2f(x) = x^2. The goal is to find the minimum value.

Step 1: Compute the Gradient

For f(x)=x2f(x) = x^2, the derivative f(x)=2xf'(x) = 2x.

Step 2: Implement in Python using NumPy

python
1import numpy as np
2
3# Function: f(x) = x^2
4def f(x):
5    return x**2
6
7# Derivative: f'(x) = 2x
8def gradient(x):
9    return 2 * x
10
11# Gradient Descent Algorithm
12def gradient_descent(starting_point, learning_rate, num_iterations):
13    x = starting_point
14    history = []
15    
16    for _ in range(num_iterations):
17        grad = gradient(x)
18        x = x - learning_rate * grad
19        history.append(x)
20        
21    return x, history
22
23# Parameters
24starting_point = 10
25learning_rate = 0.1
26num_iterations = 50
27
28minimum, history = gradient_descent(starting_point, learning_rate, num_iterations)
29
30print(f"The minimum value of the function is approximately at x = {minimum}")

Choosing the Learning Rate

The learning rate (eta\\eta) 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 eta\\eta dynamically.

Convergence

  • Convergence Criteria: Stop the gradient descent when the change in f(x)f(x) is smaller than a threshold.
  • Local Minima: The algorithm can get trapped in local minima for non-convex functions.

Key Points Summary

ConceptExplanation
GradientVector of partial derivatives; direction of steepest ascent.
Learning Rate (eta\\eta)Step size parameter that controls movement along the gradient.
Batch GDUpdates 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 GDUses a subset of data, balancing efficiency and precision.
Adaptive Learning RatesTechniques like Adam adjust eta\\eta based on past gradients.
ConvergenceDetermined by small changes in f(x)f(x) 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.


Course illustration
Course illustration

All Rights Reserved.