Skip to content
AI & MLintermediate

What is gradient descent?

Updated Nov 30, 20263 min readBased on official documentation
What is gradient descent?

In short

Gradient descent is an optimization method that adjusts a model's parameters step by step to reduce its error. At each step it measures the slope, called the gradient, of the loss and moves the parameters a small distance in the opposite direction. Repeating this many times helps the model learn.

Gradient descent is the most common way to train machine learning models. It is a loop: measure how wrong the model is, find the direction that makes it less wrong, take a small step that way, and repeat.

The core idea

A model has parameters, which are numbers such as weights. Training means finding values for those numbers that give good predictions. To judge a set of values, we use a loss function. It returns a single number that is small when predictions are close to the correct answers and large when they are far off.

The loss depends on the parameters, so it forms a kind of landscape. Gradient descent looks at the slope of that landscape at the current point. This slope is the gradient. The gradient points in the direction in which the loss grows fastest. To reduce the loss, we move the opposite way, downhill.

The method is named after this downhill walk. You never see the whole landscape at once. You only check the slope where you stand, take a step, and check again.

How one step works

Each step has three parts:

  • Compute the loss for the current parameters.
  • Compute the gradient, which tells you how the loss changes when each parameter changes a little.
  • Update each parameter by subtracting a small multiple of its gradient.

The small multiple is the learning rate. It controls the step size. A learning rate that is too large can make the loss jump back and forth or grow. A learning rate that is too small makes progress very slow. Choosing it is one of the most important practical decisions in training.

In most real projects you do not compute gradients by hand. Frameworks such as TensorFlow and PyTorch calculate them automatically, a technique called automatic differentiation. Your job is to define the model and the loss, then choose an optimizer.

A simple example

Suppose we want to find the value of w that makes the loss (w - 3) squared as small as possible. The gradient is 2 times (w - 3). The lowest point is at w = 3. Here is a plain Python version:

w = 0.0 learning_rate = 0.1 for step in range(25): gradient = 2 * (w - 3) w = w - learning_rate * gradient print(w)

After 25 steps, w is very close to 3. Each step moves w a little toward the bottom of the curve. Real models have millions of parameters, but the update rule is the same idea.

In practice, most training uses a variant called stochastic gradient descent, which estimates the gradient from a small batch of examples rather than the whole dataset. This is faster and adds some noise that can help the search.

Common mistakes

Beginners often run into the same problems:

  • Using a learning rate that is far too large, so the loss never settles.
  • Stopping too early without checking whether the loss is still falling.
  • Forgetting to scale input features, which can make the landscape stretched and hard to walk down.
  • Judging progress only by training loss and ignoring performance on data the model has not seen.

When training misbehaves, first print the loss at each step or epoch and look at its shape. A loss that rises or oscillates usually points to the learning rate.

Variants you will meet

Plain gradient descent has many relatives. The Google Machine Learning Glossary describes AdaGrad as a gradient descent algorithm that rescales the gradient of each parameter, which gives each parameter its own effective learning rate. Other popular optimizers adjust step sizes or add momentum, which carries past gradients forward to smooth the path.

These variants do not change the core loop. They change how each step is calculated. If you understand the basic update rule, the others become easier to read. For the official definitions of the terms used in machine learning, see Google's Machine Learning Glossary.

FAQ

Is gradient descent the same as training a model?

Not exactly. Training is the whole process of fitting a model to data. Gradient descent is the optimization method that most training runs use to update the model's parameters during that process.

Why is it called a gradient?

The gradient is the vector of slopes of the loss with respect to each parameter. It shows which direction increases the loss fastest, so moving against it reduces the loss.

Does gradient descent always find the best answer?

No. On complex loss surfaces it can settle in a local minimum or slow down near flat regions. Good learning rates, sensible starting values and monitoring help, but there is no guarantee of the global best.

What is a good learning rate to start with?

There is no universal value. Many people start with a small number and adjust it by watching the loss curve. If the loss grows or bounces, lower the rate. If it falls very slowly, try a larger one.

Sources

This page is based on the official documentation below. Read it for full detail.

Gradient descent — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.