liveBook · Manning

Stochastic gradient descent

As explained in Deep Learning with Python, Third Edition

A gradient-descent training method that computes updates from small batches of training examples rather than the full dataset at once.

Training a model means adjusting its weights to reduce a loss. Stochastic gradient descent (SGD) does this using randomly drawn training examples rather than calculating every update from the full dataset. In practice, mini-batch SGD is the useful compromise: one example per update is true SGD, while using the entire dataset makes each update more expensive. section 2.4.3 section 2.6

For each mini-batch:

  • Draw inputs and targets, then run the model to get predictions—a forward pass. section 2.4.3
  • Measure the loss and compute its gradient with respect to the weights—a backward pass. section 2.4.3
  • Move the weights a little against the gradient: W -= learning_rate * gradient. section 2.4.3

The learning rate controls the size of each move. Too small can mean many iterations; too large can send updates to unhelpful locations. The optimizer specifies the exact update rule—SGD with momentum, for example, uses more than the current gradient. section 2.4.3 section 2.6

Handwritten-digit training

A two-layer Keras classifier trained with fit(..., epochs=5, batch_size=128) processes mini-batches of 128 images. For each batch, backpropagation computes a loss gradient and the optimizer updates the weights. Here, the configured optimizer is Adam: mini-batch training does not require choosing an optimizer named SGD. section 2.5

def training_step(inputs, targets):
    predictions = model(inputs)
    loss = mean_squared_error(targets, predictions)
    loss.backward()
    optimizer.step()
    model.zero_grad()
    return loss

This PyTorch training step makes the order concrete: predict, calculate loss, call backward(), update parameters with optimizer.step(), then clear gradients with model.zero_grad(). Clearing matters because gradients otherwise accumulate across steps. section 3.4.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through forward pass, loss, backward pass, and weight update; compares batch sizes and learning-rate choices without a dense derivation.
Examples
Uses a one-parameter loss curve, handwritten-digit training in Keras, and a PyTorch linear-classifier training step.
What it assumes
Builds on model predictions, loss, and weights while introducing the gradient-based update sequence without a formal derivation.
What you can do after
Apply it via Keras fit or torch.optim.SGD, or perform parameter updates and gradient resets in a PyTorch training step.
How it connects
Connects loss functions, backpropagation, learning rates, optimizers, and mini-batches in the training loop.

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 Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.