XenonStack Glossary: AI, Data and Cloud Terms Explained

Stochastic Gradient Descent (SGD): How It Works

Written by Dr. Jagreet Kaur | 17 Mar 2023

Stochastic gradient descent (SGD) is an optimisation method that trains a model by updating its parameters using the gradient of the loss computed on a single example or a small batch, instead of the whole training set. Each update is cheap, so training can start improving a model quickly even on large datasets.

This page is for data scientists and technical leaders who train or review machine learning models. It covers how SGD works, how it differs from batch gradient descent, which settings matter, and what to do first.

What is stochastic gradient descent?

For neural networks, the weights are normally found with an iterative optimisation procedure such as stochastic gradient descent, because there is no closed-form solution. The scikit-learn SGD guide makes a useful point: strictly speaking, SGD is merely an optimisation technique and does not correspond to a specific family of models. It is a way to train a model, such as a linear classifier or a neural network.

Why is SGD widely used?

The same guide lists efficiency and ease of implementation as its main advantages, and notes that the cost grows roughly linearly with the number of training examples. Its stated drawbacks are that it needs several hyperparameters, such as the regularisation parameter and the number of iterations, and that it is sensitive to feature scaling.

How does SGD differ from batch gradient descent?

Batch gradient descent computes the gradient over the full dataset for every update. SGD uses one example, and mini-batch SGD uses a small subset, so updates are cheaper and more frequent but noisier. Mini-batch SGD is the form most often used to train neural networks because it balances update noise against hardware efficiency.

QuestionBatch gradient descentSGD (one example)Mini-batch SGD
Data per updateWhole training setOne exampleSmall group of examples
Cost per updateHighestLowestModerate
Update noiseLowestHighestModerate
Typical useSmall datasetsOnline and linear modelsNeural network training

What settings control SGD?

The learning rate sets the size of each step. Too high and training can diverge or oscillate; too low and it is slow. The scikit-learn guide offers several schedules: optimal, inverse scaling, constant and adaptive, where the rate is divided by 5 when the stopping criterion is reached. The PyTorch SGD documentation adds momentum, dampening, weight decay (an L2 penalty) and a Nesterov option that applies only when momentum is non-zero. Momentum adds a fraction of the previous update to the current one, which smooths gradient noise.

How does SGD relate to Adam and other optimisers?

Adam is an adaptive variant that keeps running estimates of gradient moments to scale each parameter's step. Gradient boosting is a different method that adds models in sequence.

How does SGD relate to AI agents?

Agents that rely on trained models inherit the choices made in training. A model tuned with an unrecorded learning rate or unscaled features is hard to reproduce or explain later. ElixirClaw is XenonStack's AI agents computing platform.

What should you do first?

  1. Shuffle the training data and standardise the features, using the same scaling on test data.
  2. Start with a simple learning rate schedule and watch the loss curve.
  3. Add momentum only after the baseline is stable.
  4. Record the learning rate, batch size and iteration count with each model.

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

Frequently asked questions

What is stochastic gradient descent?

Stochastic gradient descent (SGD) is an optimisation method that updates a model's parameters using the gradient of the loss computed on a single example or a small batch, instead of the whole training set.

How is SGD different from batch gradient descent?

Batch gradient descent computes the gradient over the full dataset for every update. SGD uses one example, and mini-batch SGD uses a small subset, so updates are cheaper and more frequent but noisier.

What does the learning rate do in SGD?

The learning rate sets the size of each update step. If it is too high, training can diverge or oscillate; if it is too low, training is slow. Many setups lower it during training using a schedule.

Why add momentum to SGD?

Momentum adds a fraction of the previous update to the current one. This smooths the noise in the gradients and can speed up progress along consistent directions.

Why does feature scaling matter for SGD?

The scikit-learn guide says SGD is sensitive to feature scaling, so features should ideally be standardised and the same scaling applied to test data.