Ctrl + K
AI20 min read

Gradient Descent Explained

Understand gradient descent, one of the fundamental optimization algorithms used to train machine learning and neural network models.

Published: 2026-09-14

Gradient descent is one of the fundamental optimization algorithms in machine learning. It is used to adjust model parameters so that the model's predictions become closer to the desired outputs. Many machine learning algorithms rely on optimization, and gradient descent is one of the most important ways to perform that optimization.

The basic idea is simple: measure how the model's error changes when its parameters change, then move the parameters in the direction that reduces the error. By repeating this process many times, the algorithm can find parameter values that produce a lower loss.

Gradient descent is especially important for neural networks. During training, backpropagation calculates how much each parameter contributes to the error, while gradient descent uses those gradients to update the parameters.

What Is Gradient Descent?

Gradient descent is an iterative optimization algorithm that minimizes a loss function by repeatedly moving model parameters in the direction of the steepest decrease in loss.

A machine learning model has parameters such as weights and biases. These parameters determine how the model transforms its inputs into predictions. During training, the goal is to find parameter values that minimize a loss function.

The loss function measures how different the model's predictions are from the expected results. If the loss is high, the model is performing poorly according to that particular objective. If the loss is low, the predictions are closer to the desired outputs.

Gradient descent provides a systematic way to change the parameters so that the loss generally moves downward.

The Basic Intuition

Imagine standing somewhere on a large mountain landscape, but instead of trying to reach the highest point, your goal is to reach the lowest point in the terrain. You cannot see the entire landscape, but you can determine which direction the ground slopes.

If the ground slopes downward toward the left, you move left. If it slopes downward toward the right, you move right. You repeat the process, taking steps in the direction that leads downward until you reach a low point.

Gradient descent works similarly, except that the landscape represents the loss function and the coordinates represent model parameters. The algorithm calculates the gradient of the loss and moves the parameters in the opposite direction.

💡 The word gradient describes the direction of the greatest increase. Gradient descent moves in the opposite direction because the goal is to decrease the loss.

What Is a Loss Function?

Before understanding gradient descent, it helps to understand the quantity it is trying to minimize: the loss function.

A loss function takes the model's predictions and the expected outputs and produces a numerical measure of error. Different machine learning problems use different loss functions.

Loss functionCommon use
Mean squared errorRegression
Mean absolute errorRegression
Binary cross-entropyBinary classification
Categorical cross-entropyMulticlass classification
Sparse categorical cross-entropyMulticlass classification with integer labels

Gradient descent does not determine which loss function should be used. Instead, it uses the gradients of the selected loss function to determine how the model parameters should change.

The Gradient Descent Formula

The basic parameter update rule is:

θ_new = θ_old - η × ∇J(θ)

Here, θ represents the model parameters, η is the learning rate, J(θ) is the loss function, and ∇J(θ) is the gradient of the loss with respect to the parameters.

The minus sign is important. The gradient points toward increasing loss, so subtracting the gradient moves the parameters toward decreasing loss.

What Is the Gradient?

The gradient describes how the loss changes with respect to the model's parameters. For a model with a single parameter, the gradient is simply a derivative. For a model with many parameters, the gradient is a vector containing the partial derivative with respect to each parameter.

For example, if a model has parameters w1, w2, and w3, the gradient can be represented as:

∇J = [∂J/∂w1, ∂J/∂w2, ∂J/∂w3]

Each component tells the optimizer how the loss changes when the corresponding parameter changes. A large positive gradient means increasing that parameter tends to increase the loss, while a negative gradient means increasing the parameter tends to decrease the loss locally.

A Simple One-Parameter Example

Consider a very simple loss function:

J(w) = (w - 5)²

The minimum occurs at w = 5 because that makes the loss equal to zero. Suppose the current parameter is w = 1.

The derivative of the loss function is:

dJ/dw = 2(w - 5)

At w = 1, the gradient is -8. A negative gradient means that increasing w will reduce the loss in this region. Gradient descent therefore moves w in the positive direction.

w_new = w_old - η × gradient

With a learning rate of 0.1, the update becomes:

w_new = 1 - 0.1 × (-8)
w_new = 1.8

The parameter moves from 1 to 1.8, closer to the optimal value of 5. Repeating the update gradually moves the parameter toward the minimum.

How Gradient Descent Trains a Model

Training a model with gradient descent usually follows an iterative process. The exact implementation differs between algorithms and frameworks, but the general pattern remains similar.

  • Initialize the model parameters.
  • Run the model on training data to produce predictions.
  • Calculate the loss between predictions and expected outputs.
  • Calculate the gradient of the loss with respect to the parameters.
  • Update the parameters using the gradient and learning rate.
  • Repeat the process for many iterations or epochs.
  • Stop when the optimization reaches a suitable point or another stopping condition is met.

Each complete pass through the training data is commonly called an epoch. Depending on the optimization method, one parameter update may use the entire dataset, a single example, or a smaller batch of examples.

The Role of the Learning Rate

The learning rate determines how large each parameter update is. It is usually represented by η and is one of the most important hyperparameters in gradient-based optimization.

Learning rateTypical behavior
Too smallTraining can be extremely slow
ReasonableThe loss generally decreases efficiently
Too largeUpdates can overshoot the minimum or become unstable

If the learning rate is very small, the optimizer takes tiny steps. The model may eventually reach a good solution, but training can require a very large number of updates.

If the learning rate is too large, the optimizer can repeatedly jump over a minimum. The loss may oscillate, increase, or fail to converge.

💡 The learning rate controls the size of the step, while the gradient determines the direction of the step. These two ideas should not be confused.

Batch Gradient Descent

Batch gradient descent calculates the gradient using the entire training dataset before making an update.

For a dataset containing thousands or millions of examples, this means the optimizer processes all available training examples before changing the parameters.

AdvantageDisadvantage
Stable gradient estimatesCan require substantial memory and computation
Predictable updatesUpdates can be slow on large datasets
Uses the full dataset for each stepOne update requires processing every training example

Stochastic Gradient Descent

Stochastic gradient descent, usually called SGD, updates the parameters using one training example at a time.

Instead of calculating one gradient from the entire dataset, SGD estimates the gradient from a single example and immediately performs an update.

Because each example can produce a different gradient, the optimization path is much noisier than full-batch gradient descent. However, the frequent updates can make SGD useful for large datasets.

MethodData used per updateBehavior
Batch gradient descentEntire datasetStable but potentially expensive
Stochastic gradient descentOne exampleFast and noisy updates
Mini-batch gradient descentSmall batchBalances efficiency and stability

Mini-Batch Gradient Descent

Mini-batch gradient descent uses a small group of training examples for each update. It is the most common approach for training neural networks.

Instead of processing an entire dataset or a single example, the training data is divided into batches such as 16, 32, 64, 128, or another suitable size.

Mini-batches provide a practical compromise. They can take advantage of modern hardware acceleration while producing less noisy gradient estimates than pure SGD.

💡 When people casually say that a neural network is trained with gradient descent, they often mean a mini-batch gradient-based optimizer rather than full-batch gradient descent.

Gradient Descent and Neural Networks

Neural networks can contain millions or even billions of trainable parameters. Training requires changing these parameters so that the network produces useful predictions.

For each training batch, the network performs a forward pass to calculate predictions and a loss function measures the error. Backpropagation then calculates gradients for the parameters. An optimizer such as SGD or Adam uses those gradients to update the parameters.

This means backpropagation and gradient descent have different roles. Backpropagation is the method used to efficiently calculate gradients through the network, while gradient-based optimization uses those gradients to change the parameters.

Gradient Descent vs Backpropagation

ConceptPurpose
Forward passProduces predictions
Loss functionMeasures prediction error
BackpropagationCalculates gradients
Gradient descent / optimizerUpdates model parameters

These steps work together during neural network training. Backpropagation alone does not train the model because calculating gradients does not change the parameters. The optimizer uses those gradients to perform the actual updates.

What Does the Loss Landscape Look Like?

The loss landscape describes how the loss changes as model parameters change. With one parameter, it can be visualized as a curve. With two parameters, it can be represented as a surface. Real neural networks have vastly more parameters, so their loss landscapes exist in very high-dimensional spaces.

Gradient descent attempts to navigate this landscape toward regions with lower loss. The shape of the landscape can strongly affect how quickly and reliably optimization progresses.

Local Minima and Global Minima

A global minimum is the lowest point of the entire loss landscape. A local minimum is a point that is lower than its immediate surroundings but may not be the lowest point overall.

In simple optimization problems, getting stuck in a local minimum can be an important concern. Neural network loss landscapes are much more complicated, however, and their high-dimensional structure means that concepts such as saddle points and flat regions can also be important.

⚠️ Gradient descent does not guarantee that every optimization problem will reach the global minimum. The behavior depends on the loss function, model, initialization, learning rate, optimizer, and other factors.

Saddle Points

A saddle point is a location where the gradient can be close to zero even though the point is not a local minimum. The surface curves upward in some directions and downward in others.

Saddle points can slow optimization because gradient-based methods may have very small updates when the gradient is close to zero. In high-dimensional neural network optimization, saddle points can be more relevant than the simple idea of getting trapped in a local minimum.

Vanishing and Exploding Gradients

Deep neural networks can encounter problems where gradients become extremely small or extremely large during backpropagation.

When gradients become very small, parameter updates can become negligible. This is known as the vanishing gradient problem. When gradients become extremely large, updates can become unstable, which is associated with exploding gradients.

These problems can make optimization difficult, especially in deep or recurrent neural networks. Techniques such as appropriate initialization, activation functions, normalization, residual connections, gradient clipping, and carefully designed architectures can help address them.

Momentum

Basic gradient descent uses the current gradient to determine the next update. Momentum extends this idea by incorporating information from previous updates.

The optimizer maintains a moving direction that can help accelerate movement in consistent directions while reducing some of the oscillation that can occur in narrow regions of the loss landscape.

Momentum is particularly useful when the optimization landscape has directions with very different curvature, causing basic gradient descent to move inefficiently.

Popular Gradient-Based Optimizers

Modern machine learning often uses optimizers that build on the basic gradient descent idea rather than applying the simplest update rule directly.

OptimizerMain idea
SGDUses gradient-based parameter updates
SGD with momentumAdds accumulated update direction
AdaGradAdapts learning rates based on historical gradients
RMSPropUses a moving average of squared gradients
AdamCombines momentum-like estimates with adaptive learning rates
AdamWAdam-style optimization with decoupled weight decay

Adam is widely used because it often provides effective optimization with relatively little manual tuning. However, SGD with momentum can still perform very well and may be preferred for certain training tasks.

Learning Rate Schedules

The learning rate does not always need to remain constant throughout training. A learning rate schedule changes it according to a predefined strategy or training progress.

  • Step decay reduces the learning rate at selected points during training.
  • Exponential decay gradually decreases the learning rate.
  • Cosine decay follows a cosine-shaped schedule.
  • Warmup starts with a smaller learning rate and increases it during an initial training period.
  • Adaptive optimizers can adjust effective parameter-specific learning rates.

A common motivation is to use larger updates early in training and smaller updates later, when the optimizer is closer to a useful solution.

Why Gradient Descent Can Be Slow

Several factors can make optimization slow. The learning rate may be too small, gradients may become very small, the loss landscape may be poorly conditioned, or the data may not be appropriately scaled.

Feature scaling can be particularly important for some traditional machine learning algorithms. If input features have dramatically different scales, the optimization landscape can become difficult to navigate efficiently.

Modern neural network training also depends heavily on architecture, initialization, normalization, optimizer choice, batch size, learning rate, and the quality of the training data.

Common Gradient Descent Problems

ProblemPossible causePossible response
Loss barely decreasesLearning rate too small or optimization difficultyAdjust learning rate or optimizer
Loss oscillatesLearning rate too largeReduce learning rate
Loss becomes NaNNumerical instability or exploding gradientsReduce learning rate, stabilize computation, or clip gradients
Training is very slowSmall updates or inefficient optimizationTune learning rate, batch size, or optimizer
Training loss improves but validation loss worsensOverfittingUse regularization, more data, or early stopping

Gradient Descent Is Not the Same as Training

Gradient descent is an optimization technique, not a complete machine learning training strategy. Successful model training also depends on the model architecture, data, objective function, preprocessing, regularization, hyperparameters, evaluation process, and other components.

For example, a model can have an excellent optimizer but still perform poorly if the training data contains incorrect labels or does not represent the deployment environment.

Gradient Descent in Linear Regression

Gradient descent can be used to train simple models such as linear regression. Suppose the model predicts a value using:

ŷ = wx + b

Here, w is the weight and b is the bias. A loss function such as mean squared error can measure the difference between the predicted and actual values.

Gradient descent calculates how changing w and b affects the loss and then updates both parameters. Repeating the process can move the model toward parameter values that minimize the training loss.

Gradient Descent in Classification

Gradient-based optimization is also widely used for classification models. For example, logistic regression uses a differentiable loss function and can be trained using gradient-based methods.

Neural networks extend this idea by having many layers and parameters. The underlying optimization principle remains similar: calculate gradients of the loss and use them to update the parameters.

A Typical Neural Network Training Loop

A simplified training loop for a neural network looks like this:

for each epoch:
    for each batch:
        predictions = model(batch_inputs)
        loss = loss_function(predictions, targets)
        gradients = backpropagate(loss)
        optimizer.update(parameters, gradients)

In a real framework, the optimizer manages details such as gradient accumulation, parameter updates, momentum, adaptive learning rates, and clearing old gradients. The exact API differs between libraries, but the conceptual process is similar.

How to Choose a Learning Rate

There is no single learning rate that works for every model. The appropriate value depends on the model architecture, optimizer, batch size, data, loss function, and scale of the gradients.

  • Start with a reasonable value recommended for the selected optimizer or model architecture.
  • Monitor training and validation loss.
  • Reduce the learning rate if training becomes unstable.
  • Consider a learning rate schedule for longer training runs.
  • Use learning-rate experiments or automated tuning when appropriate.
  • Evaluate the effect on both convergence speed and final model quality.
💡 Do not judge a learning rate only by how quickly the training loss decreases at the beginning. A very aggressive learning rate can produce rapid initial progress but poor or unstable final results.

Gradient Descent and Overfitting

Gradient descent minimizes the chosen training objective, but minimizing training loss does not guarantee good performance on unseen data.

A sufficiently powerful model can continue improving its training loss while becoming worse at generalizing to new examples. This is the problem of overfitting.

Techniques such as regularization, dropout, data augmentation, early stopping, and appropriate model selection can help improve generalization. These methods address a different problem from the optimization process itself.

Advantages of Gradient Descent

  • Works with many differentiable loss functions
  • Scales to models with very large numbers of parameters
  • Provides a general method for minimizing model loss
  • Forms the basis of many neural network optimizers
  • Can be adapted to different dataset sizes using batch strategies
  • Works well with modern hardware and automatic differentiation

Limitations of Gradient Descent

  • Requires gradients or suitable gradient estimates
  • Can be sensitive to learning-rate selection
  • Optimization can be slow for poorly conditioned problems
  • Can encounter vanishing or exploding gradients
  • May converge to different solutions depending on initialization and optimization settings
  • Minimizing training loss does not guarantee good generalization

Gradient Descent vs Other Optimization Methods

Gradient descent belongs to a broader family of optimization methods. Some algorithms use first-order information such as gradients, while others can use second-order information such as curvature.

Second-order methods can sometimes converge rapidly, but calculating and storing curvature information can become prohibitively expensive for models with millions or billions of parameters. Gradient-based methods are attractive for large machine learning models because they provide useful optimization information without requiring a full Hessian matrix.

Common Mistakes When Learning Gradient Descent

  • Thinking the gradient points toward the minimum rather than toward increasing loss
  • Confusing the gradient with the learning rate
  • Assuming a larger learning rate is always better
  • Assuming gradient descent always finds the global minimum
  • Confusing backpropagation with the parameter update itself
  • Ignoring the effect of batch size on gradient estimates
  • Assuming low training loss automatically means good generalization

Frequently Asked Questions

What is gradient descent in simple terms?

Gradient descent is an algorithm that repeatedly changes model parameters in the direction that reduces the loss. It uses the gradient to determine the direction and the learning rate to determine the size of each update.

Why is the gradient subtracted?

The gradient points in the direction of the steepest local increase in the loss. Subtracting it moves the parameters in the opposite direction, toward a lower loss.

What is the learning rate in gradient descent?

The learning rate controls how large each parameter update is. A rate that is too small can make training slow, while a rate that is too large can cause unstable updates or prevent convergence.

What is the difference between gradient descent and backpropagation?

Backpropagation calculates gradients of the loss with respect to neural network parameters. Gradient-based optimization uses those gradients to update the parameters. They are complementary parts of the training process.

Is Adam gradient descent?

Adam is a gradient-based optimization algorithm that builds on the basic idea of gradient descent. It uses estimates of gradient moments and adaptive parameter-specific learning rates rather than applying the simplest fixed-step gradient descent update.

Conclusion

Gradient descent is a fundamental optimization technique used to reduce the loss of machine learning models. It repeatedly calculates how the loss changes with respect to model parameters and updates those parameters in the direction that decreases the loss.

The basic algorithm is simple, but effective optimization depends on many factors, including the learning rate, batch size, loss function, model architecture, initialization, and optimizer. Variants such as SGD with momentum and Adam extend the basic approach to make training more efficient and practical.

For neural networks, gradient descent is closely connected to backpropagation. Backpropagation calculates the gradients, while the optimizer uses them to update the network's parameters. Together, these mechanisms form the core of how many modern neural networks learn from data.

Found an issue?

Found an error, outdated information, or something missing from this article? Let me know through the Contact page.

Your feedback helps improve our articles and keep them accurate and useful.