Gradient Descent Algorithm

Imagine you are blindfolded at some arbitrary point on a mountain, tasked with finding the absolute lowest point in the valley. You cannot see the entire landscape, only the immediate slope beneath your feet. Your best strategy is to take small steps in the direction that feels steepest downhill. This iterative process, guided by local information, is the core intuition behind Gradient Descent.

The Core Idea: Descending the Loss Landscape

In machine learning, we often define a loss function (or cost function) that quantifies how poorly our model performs for a given set of parameters. The goal of training is to find the parameter values that minimize this loss function. Gradient Descent treats this loss function as a multi-dimensional landscape, where the height represents the loss and the horizontal dimensions represent the model's parameters.

Gradient Descent Iteration Flow
Loading diagram...
The iterative process of Gradient Descent, from initialization to convergence.

The Gradient: Direction of Steepest Ascent

The gradient of a function at a particular point is a vector that points in the direction of the function's steepest increase. For a loss function J(w0,w1,…,wn)J(w_0, w_1, \dots, w_n) with multiple parameters, the gradient is composed of the partial derivatives with respect to each parameter. By knowing the direction of steepest increase, we can infer the direction of steepest decrease by simply taking the negative of the gradient.

📐 Gradient Definition

For a function J(w0,w1,…,wn)J(w_0, w_1, \dots, w_n), its gradient ∇J\nabla J is a vector of its partial derivatives:

∇J(w)=(∂J∂w0∂J∂w1⋮∂J∂wn)\nabla J(w) = \begin{pmatrix} \frac{\partial J}{\partial w_0} \\ \frac{\partial J}{\partial w_1} \\ \vdots \\ \frac{\partial J}{\partial w_n} \end{pmatrix}

The Update Rule: Stepping Downhill

To move towards the minimum of the loss function, we adjust our parameters in the direction opposite to the gradient. This adjustment is scaled by a crucial hyperparameter called the learning rate, denoted by α\alpha. The learning rate determines the size of each step we take down the loss landscape. A larger learning rate means bigger steps, while a smaller one means finer adjustments.

📐 Parameter Update Rule

The parameters ww are updated iteratively using the following rule:

wnew=wold−α∇J(wold)w_{new} = w_{old} - \alpha \nabla J(w_{old})

Where:
- wneww_{new} are the updated parameters
- woldw_{old} are the current parameters
- α\alpha is the learning rate
- ∇J(wold)\nabla J(w_{old}) is the gradient of the loss function JJ with respect to ww at the current parameters woldw_{old}

pythonCalculating the Gradient for a Simple Function
Check Your Understanding
If the gradient of a parameter is positive, how should Gradient Descent adjust that parameter to reduce loss?

The Learning Rate (alphaalpha): Step Size Control

The learning rate α\alpha is arguably the most critical hyperparameter in Gradient Descent. If α\alpha is too large, the algorithm might overshoot the minimum, bounce around, or even diverge, never finding a stable solution. Conversely, if α\alpha is too small, convergence will be extremely slow, requiring many more iterations and computational resources. Finding an optimal learning rate often involves experimentation and techniques like learning rate schedules.

Impact of Learning Rate on Convergence
This chart shows the loss value over iterations for a simple quadratic function using three different learning rates: too high, optimal, and too low. An optimal learning rate allows for efficient convergence to the minimum loss.
Loading chart...
Key Insight: An appropriately chosen learning rate is critical for efficient and stable convergence to the minimum loss.
pythonImplementing Batch Gradient Descent for a Quadratic Function
Try It Yourself
Using the gradient_descent function from the previous example, experiment with different learning rates. What happens if you set learning_rate = 0.9 or learning_rate = 0.001? Observe the final_x and final_loss values.
python

Batch, Stochastic, and Mini-Batch Gradient Descent

The term 'Gradient Descent' often refers to Batch Gradient Descent (BGD), where the gradient is calculated using the entire training dataset for each parameter update. While precise, this can be computationally expensive for large datasets. To address this, Stochastic Gradient Descent (SGD) computes the gradient and updates parameters using only one randomly chosen training example at a time. Mini-Batch Gradient Descent (MBGD) strikes a balance, using a small, randomly selected subset (a 'mini-batch') of the training data for each update.

Comparing Gradient Descent Variants
FeatureBatch Gradient Descent (BGD)Stochastic Gradient Descent (SGD)Mini-Batch Gradient Descent (MBGD)
Data per UpdateEntire datasetOne training exampleSmall subset (mini-batch)
Update FrequencyOne update per epochMany updates per epochMultiple updates per epoch
Computational Cost per UpdateVery highVery lowModerate
Convergence PathSmooth, directNoisy, zig-zaggingSmoother than SGD, noisier than BGD
Risk of Local MinimaHigh (can get stuck)Lower (noise can escape)Moderate
Typical Use CaseSmall datasets, convex functionsVery large datasets, online learningMost common for deep learning
Choosing the right Gradient Descent variant depends on dataset size, computational resources, and the nature of the loss landscape.
Check Your Understanding
For a very large dataset, why might Stochastic Gradient Descent (SGD) be preferred over Batch Gradient Descent (BGD)?

Challenges and Practical Considerations

While powerful, Gradient Descent faces challenges. In complex, non-convex loss landscapes, it can get stuck in local minima or saddle points, where the gradient is zero but the function is not at its global minimum. The choice of learning rate is critical, as discussed. Additionally, deciding when to stop the iterative process (convergence criteria) is important; common criteria include a maximum number of iterations, or stopping when the change in loss or parameters falls below a small threshold.

pythonGradient Descent for Simple Linear Regression
Key Takeaways
  • Gradient Descent is an iterative optimization algorithm that minimizes a function by repeatedly moving in the direction of steepest decrease.

  • The gradient is a vector of partial derivatives that points towards the steepest increase of a function; we move in the opposite direction.

  • The parameter update rule is wnew=wold−α∇J(wold)w_{new} = w_{old} - \alpha \nabla J(w_{old}), where ww are parameters, α\alpha is the learning rate, and JJ is the loss function.

  • The learning rate (α\alpha) controls the step size. Too high, and the algorithm may overshoot or diverge; too low, and convergence becomes excessively slow.

  • Batch Gradient Descent uses the entire dataset for each update, Stochastic Gradient Descent uses one example, and Mini-Batch Gradient Descent uses a small subset, balancing speed and stability.

  • Gradient Descent can get stuck in local minima or saddle points in non-convex loss landscapes, which is a common challenge in deep learning.

  • Practical application involves defining a loss function, calculating its gradient, and iteratively updating parameters until a convergence criterion is met.

← All lessons in Calculus: Gradients & Optimization

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free