liveBook · Manning

Stochastic gradient descent

As explained in Deep Learning with R, Third Edition

Mini-batch stochastic gradient descent is an optimization method that updates model parameters using gradients computed from randomly sampled small batches of training data.

Stochastic gradient descent (SGD) trains a model by repeatedly adjusting its parameters to reduce loss. Mini-batch SGD randomly selects a small batch, runs a forward pass, computes loss and gradients during the backward pass, then moves parameters slightly opposite the gradient. The learning rate controls the update size. section 2.4.3 section 2.6

A training update follows this loop:

  • Draw a random batch of samples and targets. section 2.4.3
  • Run the model on the batch to produce predictions. section 2.4.3
  • Measure prediction error with a loss function and compute gradients with backpropagation. section 2.5 section 2.6
  • Update the weights in the direction that reduces the batch loss. section 2.4.3

Training a digit classifier

A Keras model can classify normalized 28×28 MNIST images with two Dense layers. Compiling with Adam, sparse categorical cross-entropy, and accuracy selects the optimizer, feedback signal, and metric; fitting for five epochs with batches of 128 performs mini-batch updates. The same training process can be understood as repeatedly moving the network’s weight tensors toward lower loss. section 2.5

Mini-batch SGD balances two extremes: true SGD uses one sample per update, while batch gradient descent uses all available data. Larger batches can make each update more accurate but more expensive. A learning rate that is too small slows descent; one that is too large can send updates to unhelpful locations. Optimizers can specify variations such as Adam, RMSProp, or SGD with momentum. section 2.4.3 section 2.6

Figure 2.17 SGD down a 1D loss curve (one learnable parameter)
figure

Use the one-parameter loss-curve figure to visualize how gradient-based updates descend toward lower loss. section 2.4.3

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains sampling, forward and backward passes, parameter updates, batch-size tradeoffs, and learning-rate risks without formal derivation.
Examples
Demonstrates MNIST digit classification with Keras Dense layers, Adam, five epochs, and batches of 128 samples.
What it assumes
Assumes basic models, data, and loss while teaching forward passes, gradients, backpropagation, and weight updates from the training loop.
What you can do after
Apply it via Keras by choosing an optimizer, loss, batch size, and training schedule for a neural-network model.
How it connects
Connects loss functions, optimizers, backpropagation, learning rate, batch gradient descent, and neural-network weights.

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