Gradient Descent for Optimization

How does a machine learning model, given millions of data points, figure out the best set of parameters to make accurate predictions? Gradient Descent is an iterative optimization algorithm that helps models find the parameter values that minimize a defined loss function.

Minimizing Model Loss

Every machine learning model aims to make predictions as accurately as possible. The discrepancy between a model's predictions and the actual values is quantified by a loss function (or cost function). The goal of training a model is to find the specific set of internal parameters (like weights and biases) that minimizes this loss function, thereby improving the model's predictive power. This search for the lowest point in the loss landscape is a core optimization problem.

Descending a 1D Loss Landscape
This chart illustrates how an optimization algorithm might navigate a simple, U-shaped loss function to find its minimum. Starting from a high loss value, each step moves closer to the lowest point, representing the optimal parameter value.
Loading chart...
Key Insight: Optimization involves iteratively moving down the loss function until a minimum is reached, much like walking downhill.

The Gradient: Guiding the Descent

To descend a loss function, a model needs to know which direction is 'downhill'. This is where the gradient comes in. The gradient of a function at a specific point is a vector that points in the direction of the steepest increase of that function. Consequently, the negative of the gradient points in the direction of the steepest decrease. This provides the precise direction to adjust parameters to reduce the loss.

📐 Gradient Vector

The gradient of the loss function LL with respect to the parameter vector θ=[θ0,θ1,…,θn]\theta = [\theta_0, \theta_1, \dots, \theta_n] is given by:

∇L(θ)=(∂L∂θ0∂L∂θ1⋮∂L∂θn)\nabla L(\theta) = \begin{pmatrix} \frac{\partial L}{\partial \theta_0} \\ \frac{\partial L}{\partial \theta_1} \\ \vdots \\ \frac{\partial L}{\partial \theta_n} \end{pmatrix}

The Parameter Update Rule

Once the gradient is calculated, it's used to update the model's parameters. The core idea is to move the parameters in the direction opposite to the gradient, scaled by a small amount. This iterative adjustment ensures that with each step, the model's parameters get closer to the values that minimize the loss function. This process continues until the loss function stops decreasing significantly, indicating that a minimum has been reached.

📐 Parameter Update Formula

The iterative update rule for parameters is:

θnew=θold−α⋅∇L(θold)\theta_{\text{new}} = \theta_{\text{old}} - \alpha \cdot \nabla L(\theta_{\text{old}})

Where:
- θnew\theta_{\text{new}} are the updated parameters.
- θold\theta_{\text{old}} are the current parameters.
- α\alpha (alpha) is the learning rate, a scalar that controls the step size.
- ∇L(θold)\nabla L(\theta_{\text{old}}) is the gradient of the loss function at the current parameters.

Check Your Understanding
If the gradient of the loss function with respect to a parameter is positive, what should happen to that parameter to decrease the loss?

The Learning Rate: Controlling Step Size

The learning rate (α\alpha) is a hyperparameter that dictates the size of the steps taken during each parameter update. It's a critical factor in the performance of Gradient Descent. A carefully chosen learning rate allows the model to converge efficiently to the minimum of the loss function. If the learning rate is too small, convergence will be very slow; if it's too large, the algorithm might overshoot the minimum, oscillate, or even diverge entirely.

Impact of Learning Rate on Loss Convergence
This chart shows how different learning rates affect the training loss over iterations. A small learning rate leads to slow convergence, an optimal rate converges quickly and smoothly, while a large rate can cause oscillations or divergence.
Loading chart...
Key Insight: The learning rate is a critical hyperparameter; too small, and training is slow; too large, and the model may fail to converge.

The Gradient Descent Algorithm

Steps of Gradient Descent
1
Initialize Parameters
Start with an initial guess for the model's parameters (weights and biases). These are often random values or small constants.
2
Calculate the Gradient
Compute the gradient of the loss function with respect to each parameter. This tells us the direction of steepest ascent for the loss.
3
Update Parameters
Adjust the parameters by subtracting the learning rate times the gradient. This moves the parameters in the direction that reduces the loss.
4
Repeat Until Convergence
Continue steps 2 and 3 for a fixed number of iterations, or until the change in loss between iterations becomes very small, indicating convergence to a minimum.
pythonPseudocode for Gradient Descent
Try It Yourself
Consider the gradient_descent function. What would happen if learning_rate was set to a very small value like 0.00001? What if it was set to a very large value like 10?
python
Key Takeaways
  • Gradient Descent is an iterative optimization algorithm that helps machine learning models find optimal parameters.

  • It minimizes a loss function by repeatedly adjusting parameters in the direction opposite to the gradient.

  • The gradient indicates the direction of the steepest increase in the loss, so its negative points towards the steepest decrease.

  • The learning rate (α\alpha) controls the step size taken in each parameter update, significantly impacting convergence speed and stability.

  • Through this iterative process, models effectively 'learn' the best set of parameters to make accurate predictions.

← All lessons in Logistic Regression

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