Stochastic gradient descent (SGD)

Stochastic gradient descent is the basic method for training neural networks: it repeatedly nudges a model's parameters to reduce error, estimating the right direction from a small random sample of data each step.

Training a model means finding parameter values that make its errors, measured by a loss function, as small as possible. Gradient descent does this step by step: work out which direction would reduce the loss fastest, take a small step that way and repeat. The catch is that the exact direction depends on every example in the training set, and computing it over billions of examples for every step would be impossibly slow. Stochastic gradient descent estimates the direction from a randomly chosen sample instead.

Strictly, Google's machine learning glossary defines SGD as using a single random example per step, and mini-batch stochastic gradient descent as using a small subset. In practice people say SGD for both, and training on mini-batches is the norm for neural networks. Each step is a rough estimate, so the path towards a good solution is noisy, but steps are so cheap that the model makes far more progress for the same amount of computing. The size of each step is set by the learning rate, one of the most important settings in training.

The method is older than modern AI. It grows out of stochastic approximation methods introduced by Herbert Robbins and Sutton Monro in 1951, and Jack Kiefer and Jacob Wolfowitz published a closely related algorithm in 1952. Frank Rosenblatt used it to train his perceptron later in the 1950s, and in 1986 an influential paper by David Rumelhart, Geoffrey Hinton and Ronald Williams used it with backpropagation to train networks with hidden layers.

Most modern training uses refinements of the same idea. Momentum, which carries part of the previous step into the next one to smooth the path, comes from Boris Polyak's 1964 heavy ball method and was used with backpropagation in 1986. Adam, published in 2014, adapts the step size for each parameter separately and is one of the most widely used optimisers. When a news story says a model was trained for a certain number of steps, each step is typically one of these updates on one mini-batch.

Common questions

What is the difference between gradient descent and stochastic gradient descent?

Plain gradient descent calculates the direction to adjust the parameters using the whole training set before every step. Stochastic gradient descent uses a single random example or a small random batch, which gives a noisier estimate but makes each step far cheaper. For large datasets this lets training make much faster progress.

Why is it called stochastic?

Stochastic means involving randomness. The method picks examples at random for each step, so the estimated direction varies from step to step rather than being the exact average over all the data.

Is Adam better than SGD?

Adam often trains faster with less tuning, because it adjusts the step size for each parameter automatically, and it is very widely used. Plain SGD, often with momentum, remains in use, and which works better depends on the model and how carefully each is tuned.

What is a mini-batch?

A mini-batch is a small random group of training examples used to compute one update of the model's parameters. Using a batch rather than a single example gives a steadier estimate of the right direction and lets hardware such as GPUs process many examples in parallel.

Finding more

Search for "stochastic gradient descent", "mini-batch gradient descent", "learning rate" or "Adam optimiser" for explanations at different levels. Good sources include Google's Machine Learning Crash Course, the documentation of training libraries such as PyTorch, and university lecture notes on optimisation. For claims that a new optimiser beats existing ones, look for comparisons where both were tuned with equal care.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.