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.
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.
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.
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.
| Question | Batch gradient descent | SGD (one example) | Mini-batch SGD |
|---|---|---|---|
| Data per update | Whole training set | One example | Small group of examples |
| Cost per update | Highest | Lowest | Moderate |
| Update noise | Lowest | Highest | Moderate |
| Typical use | Small datasets | Online and linear models | Neural network training |
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.
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.
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.
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.
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.
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.
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.
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.
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.