Stochastic Gradient Descent (SGD)

Machine Learning
Introduced by
Robbins, H. & Monro, S.
Year
1951
Type
First-order iterative optimization algorithm

What does it do?

Stochastic Gradient Descent (SGD) is a first-order iterative optimization algorithm that minimizes an objective function by updating model parameters with the gradient computed on one randomly selected training example, or on a small mini-batch, at each step.

Its theoretical basis is the stochastic approximation framework introduced by Robbins and Monro in 1951. It is widely used to train machine learning and deep learning models on datasets too large for full-gradient updates to be practical.

The intuition behind the method

Ordinary gradient descent computes the exact gradient of the loss over all training examples before each parameter update. That is accurate but expensive on large datasets. SGD instead picks one example, or a small batch, at random, computes a noisy estimate of the gradient, and updates the parameters immediately. Each individual step is noisy, but over many steps the noise averages out and the parameters tend toward a minimum of the loss. The noise is often argued to help the optimizer move past shallow local minima and saddle points, although the size of this effect depends on the setting.

History

The stochastic approximation framework was introduced by Herbert Robbins and Sutton Monro in their 1951 paper 'A Stochastic Approximation Method' in The Annals of Mathematical Statistics. Their setting was an unknown, monotone expected response M(x), and the goal was to find the solution of M(x) = alpha by making successive noisy experiments at levels that converge in probability to that solution. Applying this framework to minimize a loss averaged over training examples, and using it to train machine learning models, came later, and the idea is now the basis of most gradient-based training in deep learning.

When to use it

Choose SGD when the training set is large enough that computing the full gradient at every update is too costly, which covers most neural network training and large-scale linear model fitting. It requires an objective that is differentiable, or sub-differentiable, with respect to the parameters. The standard analysis also assumes that samples are drawn independently and that the gradient estimate is unbiased, which holds for uniform random sampling. For convex objectives that are Lipschitz on a bounded domain, the analysis of the averaged iterate gives suboptimality of order O(1/sqrt(T)) after T steps. For non-convex objectives such as deep networks, SGD is used in practice to reach stationary points, but no global optimality guarantee applies. Mini-batch SGD (batch sizes of 32 to 512) is usually preferred over single-example updates because it lowers gradient variance and uses parallel hardware efficiently. For small, well-conditioned problems where full-batch gradients are affordable, second-order methods such as L-BFGS may converge in fewer iterations.

Limitations and considerations

  • Stochastic gradients make the training loss fluctuate between steps rather than decrease monotonically, so convergence is harder to judge from a single loss curve.
  • Performance depends strongly on the learning rate and its schedule: a rate that is too large can diverge, and one that is too small slows training to a crawl.
  • For non-convex objectives such as deep networks, convergence to a global minimum is not guaranteed; the method reaches stationary points.
  • The update uses one global step size for all parameters, so it handles directions of very different curvature poorly, which motivates per-parameter adaptive variants.
  • The classical convergence guarantees require step sizes that decay in a specific way, and the decay schedules used in practice do not carry those guarantees.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • The cost of each update is set by the batch size rather than the dataset size, so memory and compute per step stay constant as the dataset grows.
  • The method needs only a gradient, so it applies to any differentiable loss, such as cross-entropy, squared error or hinge loss, without requiring a closed-form solution.
  • Gradient noise is often argued to help the iterates leave shallow local minima and saddle points in non-convex landscapes; deterministic full-gradient descent has no comparable stochastic component.
  • Many widely used optimizers, including momentum methods and adaptive-rate variants such as Adam, keep the same basic update structure and modify the step rule, so understanding the basic scheme is needed to understand them.
  • Mini-batch updates map directly onto parallel hardware, so the same algorithm can be spread across multiple processors.

Applications

  • Training deep neural networks for image classification, speech recognition, and natural language processing.
  • Fitting large-scale logistic regression and linear SVM models in text classification and recommendation systems.
  • Online learning settings where examples arrive sequentially and the model must be updated in real time.
  • Reinforcement learning policy gradient methods, where the expected return is optimized using sampled trajectories.
  • Matrix factorization for collaborative filtering in recommender systems.

How it works

How it works

1

The training objective is the average of the per-example losses over all n examples. For tasks such as classification with cross-entropy or regression with squared error, n can be very large, so evaluating the full objective and its gradient at every iteration is costly.

Formula
L(\theta) = \frac{1}{n} \sum_{i=1}^{n} \ell(f(x_i; \theta),\, y_i)
2

At step t, an example (or mini-batch) is drawn uniformly at random from the training set. When sampling is uniform, the gradient on that sample is an unbiased estimate of the gradient of the full objective, so on average the step points in a descent direction even though any single step is noisy. Robbins and Monro's 1951 paper gives the general stochastic approximation scheme, which concerns successive noisy observations of a monotone regression function; the loss-gradient form used here is the machine-learning specialization of that idea.

Formula
\hat{g}_t = \nabla_\theta \ell(f(x_{i_t}; \theta_t),\, y_{i_t})
3

The parameters are moved opposite to the gradient estimate, scaled by the learning rate eta_t. A smaller rate makes each update cautious but slows progress, while a larger rate speeds progress but can overshoot or diverge. This update is the basis of most gradient-based optimizers used in deep learning.

Formula
\theta_{t+1} = \theta_t - \eta_t \, \hat{g}_t
4

The conditions require the step sizes to shrink over time: their sum diverges, while the sum of their squares converges. These are the classical Robbins-Monro requirements under which convergence is established for convex objectives. Practical schedules such as step decay, cosine annealing and warm restarts are heuristics that do not necessarily satisfy these conditions, so the theory guides the choice of schedule rather than dictating it.

Formula
\sum_{t=1}^{\infty} \eta_t = \infty, \quad \sum_{t=1}^{\infty} \eta_t^2 < \infty

Common pitfalls

  • Keeping a constant learning rate for the whole run, so the iterates keep fluctuating around the minimum instead of settling; this is why decay schedules matter.
  • Choosing a very large batch size because it gives a smoother loss curve, without accounting for the fact that it reduces the gradient noise the method relies on. The batch size changes the optimization behaviour, so it should be tuned for the task rather than maximised for smoothness.
  • Not reshuffling the training data between epochs. The library treatment notes that SGD cycles through the points in some random order and can give very poor results with certain orderings of training points. If examples are ordered by class or time, the mini-batches are systematically biased and the unbiasedness of the gradient estimate no longer holds.
  • Setting the learning rate once from a short run and not revisiting it while watching the training curve, which can hide divergence or an early plateau.
  • Switching to an adaptive optimizer when convergence is slow without first checking the learning rate, the schedule and the batch size, which are common causes of slow SGD training.

Frequently asked questions

What is the difference between SGD, mini-batch SGD, and full-batch gradient descent?

Full-batch gradient descent computes the exact gradient over all n training examples per update, which is accurate but costs O(n) per step. Pure SGD uses exactly one randomly chosen example per update, which costs O(1) per step but is very noisy. Mini-batch SGD uses a small random subset, typically 32 to 512 examples, which reduces noise while keeping the cost per step manageable and making use of parallel hardware. In modern usage, 'SGD' almost always means mini-batch SGD.

How should I set the learning rate?

There is no universal value; the appropriate rate depends on the scale of the loss, the batch size and the model. A practical approach is to choose a starting rate, watch the training loss curve for divergence or stalling, and apply a decay schedule. The Robbins-Monro conditions require step sizes to decay over time, but a schedule that decays too quickly can stall training, so the decay must be chosen with care.

How does SGD compare with Adam on test performance?

Whether SGD generalizes better than Adam depends on the task and on tuning, and no general theorem settles the question. Adaptive methods such as Adam often reach low training loss quickly. A related pattern is documented for SGD relative to full-batch gradient descent: SGD may perform less well on the training data yet comparably, and sometimes better, on test data, which is commonly attributed to its indirect regularizing effect. Comparisons between SGD and Adam should therefore be checked on validation data for the task at hand.

Does SGD guarantee finding the global minimum?

Not in general. For convex objectives that are Lipschitz on a bounded domain, SGD with iterate averaging achieves suboptimality of order O(1/sqrt(T)) after T steps, with a step size chosen according to the number of steps T. For non-convex objectives such as deep networks, the usual guarantees concern stationary points, where the gradient is near zero, and do not establish a global minimum. In practice, the solutions SGD finds are often adequate for the task, but this is an empirical observation rather than a guarantee.

Related methods

Sources

  1. Robbins, H. & Monro, S. (1951). A Stochastic Approximation Method. The Annals of Mathematical Statistics, 22(3), 400–407.

    Open DOI reference
  2. Goodfellow, I., Bengio, Y. & Courville, A. (2016). Deep Learning (Ch. 8). MIT Press.

    ISBN: 978-0-262-03561-3
  3. understanding-machine-learning-theory-algorithms, Section 14.3, p. 191

  4. Neural Networks and Deep Learning (2018), Chapter 3, Eqs. 3.31–3.32