XenonStack Glossary: AI, Data and Cloud Terms Explained

Gradient Descent: How the Algorithm Works

Written by Dr. Jagreet Kaur | 17 Mar 2023

Gradient descent is a first-order iterative optimisation algorithm that finds the parameters that minimise a function, which in machine learning is usually the cost or loss function. Each step moves the parameters a small amount in the direction that reduces loss, and the process repeats until loss stops falling.

This page is for practitioners and technical leaders who want to understand how models learn. It covers the steps, the learning rate, batch and stochastic variants, how it differs from related methods, and the practical checks that matter.

What is gradient descent?

The Google Machine Learning Crash Course describes gradient descent as a technique that iteratively finds the weights and bias that produce the model with the lowest loss. It starts with values near zero, computes the loss, determines which direction lowers it, and moves the weight and bias a small amount in that direction. The process repeats until the model cannot reduce the loss any further.

How does gradient descent work?

  1. Compute the loss with the current parameters.
  2. Work out the direction that lowers the loss, given by the gradient.
  3. Move the parameters a small amount in that direction.
  4. Repeat until the loss stops falling.

The size of each move is the learning rate, a setting chosen by the practitioner. The Google course notes that a model has converged when additional iterations no longer reduce loss meaningfully. Too large a step can overshoot, and too small a step makes training slow, so the rate is tuned and loss is watched during training.

What are the types of gradient descent?

Variants are distinguished by how much data each step uses. Full batch gradient descent uses the whole dataset at once to compute the gradient, whereas stochastic gradient descent takes a sample while computing it. The scikit-learn stochastic gradient descent guide describes stochastic gradient descent as approximating the true gradient by considering a single training example at a time, and calls it an efficient way to fit linear classifiers and regressors under convex loss functions. It is only a way to train a model, not a model family.

VariantData used per stepTrade-off
Full batchThe whole training setSmooth steps, but costly on large data
StochasticOne example or a small sampleCheap steps, but noisier progress
Mini-batchA small group of examplesA common middle ground between the two

The scikit-learn guide lists efficiency and ease of implementation as advantages. Its listed drawbacks are the need for hyperparameters such as the regularisation strength and number of iterations, and sensitivity to feature scaling. It recommends standardising features, for example with a scaling step in a pipeline fitted on training data only.

How does gradient descent differ from first and second order methods?

MethodUsesTypical consequence
First order (gradient descent)The gradient onlyCheap per step, needs a tuned learning rate
Second orderThe gradient and curvature informationCostlier per step, can take fewer steps

Gradient descent is a first order method because it relies on the first derivative alone. Second order approaches add curvature information, which can reduce the number of steps but raises the cost of each one.

How does gradient descent relate to AI agents?

AI agents call machine learning models to reason and act, so teams that build and run agents benefit from knowing what a model's training run optimised for and how it was evaluated. Platforms such as ElixirClaw, XenonStack's AI agents computing platform, sit around the models that agents rely on.

What should you do first?

  1. Define the loss that matches the decision the model supports.
  2. Standardise features before training with gradient based methods.
  3. Start with a modest learning rate and watch the loss curve.
  4. Use a validation set or cross-validation to choose the number of iterations.
  5. Stop when loss stops improving, and record the settings used.

To map one decision your agents make, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.

Frequently asked questions

What is gradient descent in simple terms?

It is a way of improving a model step by step. At each step the parameters move slightly in the direction that lowers the loss, until the loss stops falling.

What is the learning rate?

The learning rate sets how far the parameters move at each step. Too large a value can overshoot the minimum, and too small a value makes training slow.

What is the difference between batch and stochastic gradient descent?

Batch gradient descent uses the whole dataset to compute each gradient. Stochastic gradient descent uses a single example or a small sample, which makes each step cheaper but noisier.

Why does feature scaling matter for gradient descent?

Stochastic gradient descent is sensitive to feature scaling, so features should be standardised before training, with the scaler fitted on training data only.

How do I know gradient descent has converged?

Convergence is reached when further iterations no longer reduce the loss meaningfully. Train until the loss stabilises to confirm it.