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























