A training loop for two numbers
The topic Entropy, cross-entropy and KL showed that minimizing the forward KL is the same as maximizing the likelihood of samples from . Now we run that minimization and look at the answer it gives. The model is the simplest one from Multivariate Gaussians: a single one-dimensional Gaussian.
We write the model as with parameters , where . The optimizer moves anywhere on the real line, and stays positive whatever step it takes.
In a toy problem we choose the target ourselves, so its density is known. Then the KL can be estimated by Monte Carlo from samples :
The samples stay fixed during training. Each step builds from the current and , evaluates at the samples, averages, and differentiates. Then gradient descent moves both parameters against their gradients with a learning rate :
In practice Adam replaces the plain step: it rescales each coordinate using running statistics of its gradients. Autograd computes the derivatives; here we will also find them by hand.