liveBook · Manning

Gradient descent

As explained in Grokking Bayes

Gradient descent is an iterative optimization method that updates parameters in the direction of decreasing objective value according to the gradient.

Gradient descent improves a model step by step. At each iteration, it measures the gradient of an objective and moves the parameters toward a lower value. Repeating these informed steps eventually reaches a point where further movement no longer finds a better value. section 6.5

In variational inference, the parameters are the mean and standard deviation of an approximating distribution. The gradient of the ELBO indicates how to adjust them so the approximation fits the unknown posterior more closely. Although maximizing the ELBO technically uses gradient ascent, flipping the objective’s sign lets the process be described as gradient descent. section 6.5 section 6.6

A typical optimization loop proceeds as follows:

  • Evaluate the objective at the current parameter values. section 6.5
  • Compute the gradient with respect to those parameters. section 6.5
  • Adjust the parameters in the direction that decreases the objective, or increases the ELBO when using its maximization form. section 6.5
  • Repeat until the objective value flattens and optimization appears to have converged. section 6.10

Fitting a variational inference model

With PyMC, pm.fit(n=200_000) runs 200,000 optimization steps, after which the fitted approximation can produce posterior samples. For large datasets, minibatches can make gradient-based optimization practical. Compare the resulting approximation with MCMC to assess how closely the distributions agree. section 6.9

import matplotlib.pyplot as plt
 
plt.plot(vi_approx.hist)
plt.show()

Plot the optimization history to check whether the objective has flattened and the run has likely converged. section 6.10

How Grokking Bayes covers it

How deep it goes
Defines iterative gradient-based updates and convergence checks, but defers detailed technical treatment of the algorithm.
Examples
Uses Normal-to-Beta approximation updates, ELBO progress plots, and PyMC Bayesian linear regression with 200,000 fitting steps.
What it assumes
Assumes objective functions, parameters, gradients, and variational inference terminology while explaining their roles through iterative fitting.
What you can do after
Operate/configure PyMC variational inference, choose optimization length, use minibatches, and diagnose convergence from objective-history plots.
How it connects
Connects variational inference, ELBO, gradient ascent, reparameterization trick, automatic differentiation, minibatch optimization, and MCMC.

Also explained in

Compare how 24 books explain gradient descent →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Bayes. The section links point to the parts of the book it draws on.