liveBook · Manning

Stochastic gradient descent

As explained in Machine Learning Algorithms in Depth

Stochastic gradient descent is an iterative optimization method that updates parameters using gradients estimated from random data subsets.

Stochastic gradient descent (SGD) minimizes a loss by repeatedly estimating its gradient from a randomly selected subset of training examples, then moving the parameters in the opposite direction. Using a mini-batch of size m avoids computing every update from the full dataset. section 10.4

Let θ be the model parameters, L the loss, and εₖ the learning rate at iteration k. For examples (xᵢ, yᵢ) in a mini-batch, estimate gₖ = (1/m)∑ᵢ ∇θ L(f(xᵢ; θₖ), yᵢ), then update θₖ₊₁ = θₖ − εₖgₖ. The gradient gives the direction of increasing loss; the minus sign makes the update a minimization step. A larger εₖ moves farther on each update but can miss an optimum. A smaller one may make training unnecessarily slow. section 10.4

For a training loop, the key choices are:

  • Sample a mini-batch, compute its loss gradient, and update the parameters. Repeat over iterations. section 10.4
  • Choose a learning-rate schedule. One practical starting value is 10⁻³, followed by reductions as training proceeds; the learning rate may change with iteration. section 10.4
  • Consider momentum when successive gradients point in similar directions: it carries an exponentially decaying average of earlier gradients into later updates. Nesterov momentum instead evaluates the gradient after applying the current velocity. section 10.4

Perceptron updates as gradient-based training

For a labeled example, define its margin as zᵢ = yᵢ(θ·xᵢ + θ₀). Hinge loss penalizes insufficient margin. Its piecewise-linear gradient is zero when zᵢ > 1 and gives an update direction when zᵢ ≤ 1; at zᵢ = 1, the max operation is not differentiable, so a subgradient is used. A perceptron implementation samples training examples and updates after a classification error. This connects the classifier’s update rule to stochastic gradient-based loss minimization without making a full-dataset gradient calculation at every step. section 5.2

Figure 10.13 Cross-entropy loss and validation accuracy of LeNet trained on cifar100 using different optimizers
figure

Compare cross-entropy loss and validation accuracy when LeNet is trained on CIFAR-100 with different optimizers; the plot makes optimizer choice a measurable training decision. section 10.4

How Machine Learning Algorithms in Depth covers it

How deep it goes
Derives mini-batch updates and learning-rate effects, then relates perceptron updates to hinge-loss gradients and compares neural-network optimizers.
Examples
Uses an iris perceptron classifier and a LeNet-on-CIFAR-100 comparison of cross-entropy loss and validation accuracy.
What it assumes
Assumes familiarity with gradients, losses, labeled examples, and model parameters while spelling out the mini-batch update.
What you can do after
Build it from scratch for perceptron training and apply it via Keras/TensorFlow to compare neural-network optimizers.
How it connects
Links hinge loss and perceptrons to mini-batches, learning-rate schedules, momentum, and Nesterov momentum.

Also explained in

Compare how 12 books explain stochastic gradient descent →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning Algorithms in Depth. The section links point to the parts of the book it draws on.