Dictionary of Applied Machine Learning: 114 machine-learning terms, each with a definition, a figure, citations and a Python demo · All terms · Demos · Start here
Updated on 2026-11-03
Readiness 100/100
See also gradient descent gradient empirical risk minimization
Stochastic gradient descent (SGD) is a variant of gradient descent (GD) in which the gradient of the objective function is replaced by a computationally cheaper stochastic approximation. Its main application in machine learning (ML) is empirical risk minimization (ERM) on a training set that is large or stored in a distributed database: the gradient of the empirical risk requires an average over the entire training set. SGD approximates this average by the average over a randomly drawn batch of data points. The batch size trades the computational cost of a single update against the accuracy of the gradient approximation.

stochGD.py, which recomputes what the entry states: the script block by block · open in ColabB-weatherConsider the training set of Fig. 1: twelve weather stations, each contributing one data point with the minimum temperature of a day as feature and its maximum temperature as label, stored on two servers, one of GeoSphere Austria and one of the Finnish Meteorological Institute (FMI). The training set holds $m$ data points. Linear regression learns a linear hypothesis from it, so that its predictions match the labels. That amounts to finding the minimum of a differentiable convex objective function, the empirical risk \[ f({\mathbf w}) := \frac{1}{m} \sum_{r=1}^{m} L\left({\mathbf z}^{(r)},{\mathbf w} \right) \text{.} \] The training set is $\mathcal{D}^{(\rm train)} = \big\{ {\mathbf z}^{(1)}, \ldots, {\mathbf z}^{(m)} \big\}$, and each data point ${\mathbf z}^{(r)}$ is a feature vector together with its label. Here, $L\left({\mathbf z}^{(r)},{\mathbf w} \right)$ is the loss that the hypothesis with model parameters ${\mathbf w}$ incurs on ${\mathbf z}^{(r)}$.
B-approxGradient descent (GD) searches for a
minimum of a differentiable convex function by
repeating the gradient step
\[
{\mathbf w}^{(t+1)} = {\mathbf w}^{(t)}
- \eta^{(t)} \nabla f({\mathbf w}^{(t)})
\text{,}
\]
with a step size $\eta^{(t)} > 0$ that is small
enough for each step to decrease $f$ (see GD). The
gradient of the above objective function is
$\nabla f({\mathbf w}) = \frac{1}{m}
\sum_{r=1}^{m} \nabla_{{\mathbf w}}
L\left({\mathbf z}^{(r)},{\mathbf w} \right)$, an average
with one term per data point. Its exact evaluation therefore
requires a pass over the entire training set, which is
computationally expensive when $m$ is large. SGD instead
uses the cheaper estimate
\[
g({\mathbf w}) := \frac{1}{B}
\sum_{r \in \mathcal{B}} \nabla_{{\mathbf w}}
L\left({\mathbf z}^{(r)},{\mathbf w} \right) \text{,}
\]
the average of the same terms over a batch
$\mathcal{B} \subseteq \{1, \ldots, m\}$ of
$B = |\mathcal{B}|$ indices chosen uniformly at random
(see Fig. 1).
Geosphere.at (filled circles) and FMI.fi
(filled squares). The dotted vertical segments are the
prediction errors of a linear hypothesis map
$\hypothesis$ (solid curve). A GD update of the
model parameters $\weights$ uses the prediction errors of
all $\samplesize$ data points; an SGD update uses only
those of a randomly chosen batch $\batch$ (inside red rings).
Data generated by pythondemos/stochGD.py, which
downloads the observations from the open data services of
GeoSphere Austria and the FMI
When the training set is spread over several servers, as in Fig. 1, a batch can be drawn from one server at a time, so no server needs the whole training set.
See also: gradient descent, gradient-based method, gradient, empirical risk minimization, batch, step size, objective function, zero-gradient condition, online gradient descent.
@misc{dictml_stochGD,
author = {Jung, Alexander},
editor = {Olioumtsevits, Konstantina and Schnoor, Ekkehard},
title = {stochastic gradient descent (SGD)},
howpublished = {Dictionary of Applied Machine Learning (course edition)},
year = {2026},
doi = {10.5281/zenodo.21569296},
note = {ISBN 978-952-64-3013-3, CC BY 4.0, retrieved 2026-11-04},
url = {https://dictionaryofml.org/terms/stochGD.html}
}