liveBook · Manning

Stochastic gradient descent

As explained in Bayesian Optimization in Action

Mini-batch gradient descent is an optimization technique that updates model parameters using loss and gradient estimates computed from small subsets of the training data.

Stochastic gradient descent, commonly implemented as mini-batch gradient descent, updates model parameters using the loss and gradients estimated from small subsets of training data. It makes optimization practical when computing the full loss is expensive, as in Gaussian-process models whose exact training cost grows cubically with dataset size. section 12.0 section 12.2.2

Apply mini-batch gradient descent by repeating this training loop:

  • Split the training set into small batches, such as ten batches of 100 points for a dataset of 1,000 points. section 12.2.2
  • Compute the model output and loss for one batch rather than the entire dataset. section 12.2.2
  • Backpropagate the batch loss, update the parameters, and repeat for every batch across many iterations. section 12.2.2 section 12.2.3
  • Expect individual updates to follow imperfect directions; aggregated updates can still reduce the true loss effectively. section 12.2.2 section 12.5
for epoch in range(2):                        #1
 
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        inputs, labels = data                 #2
 
        optimizer.zero_grad()                 #3
 
        outputs = net(inputs)                 #4
        loss = criterion(outputs, labels)     #4
        loss.backward()                       #4
        optimizer.step()                      #4

A compact PyTorch loop shows the essential sequence: clear gradients, compute outputs and loss, backpropagate, and step the optimizer. section 12.2.2

Training a variational Gaussian process

A variational Gaussian process uses mini-batch gradient descent to minimize an ELBO based on each batch. In GPyTorch, create a DataLoader, compute loss as -mll(output, y_batch), call loss.backward(), and update with optimizer.step(). The resulting predictions can roughly match those of an exact GP while training on medium-to-large datasets. section 12.2.3 section 12.5

Housing-price exercise

Compare exact and variational Gaussian processes on a housing-price dataset. Train the exact model and a variational model with 100 inducing points, use batches of 100 for mini-batch gradient descent, and compare training times. section 12.4

How Bayesian Optimization in Action covers it

How deep it goes
Defines batch approximation, explains its noisy descent behavior, and demonstrates the PyTorch training loop without deriving optimizer internals.
Examples
Uses loss-valley illustrations, PyTorch loops, variational Gaussian processes, and a housing-price timing exercise.
What it assumes
Assumes familiarity with loss functions and gradient descent while teaching batch construction and approximate-loss updates.
What you can do after
Apply it via PyTorch or GPyTorch to train models with repeated batch-level loss and gradient updates.
How it connects
Connects gradient descent with variational Gaussian processes, ELBO optimization, and natural gradient descent.

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 Bayesian Optimization in Action. The section links point to the parts of the book it draws on.