A Bayesian trains a classifier
Suppose we are trying to learn the parameters of a classifier using a dataset . The typical machine learning way to do this is to minimize the cross-entropy loss using (stochastic) gradient descent, where is an optional regularization term. The value of at the end of this training procedure, which we will denote , is interpreted as the parameters of the “best” classifier. There’s a second, perhaps more principled way to solve this problem: Bayesian inference. The Bayesian way to learn a classifier is to specify a prior distribution on the parameters and then construct the posterior . Rather than a single, final classifier, this procedure gives a distribution over , and hence a distribution over classifiers, weighted by how likely that classifier is given the data.
So how are the machine learning approach and the Bayesian approach related?
The first answer is that if the cross-entropy loss is minimized, then the resulting parameters are the parameters with the highest probability under the posterior distribution , and hence is a maximum a posteriori estimate: The second, more subtle answer is that stochastic gradient descent on the cross entropy loss mimics Markov chain Monte Carlo sampling from the posterior, and hence is a sample from the posterior:
Cross-entropy minimization as MAP estimation
Finding the parameter that maximizes the posterior distribution is known as maximum a posteriori estimation, or MAP estimation. An expression for the posterior distribution can be found using Bayes’ rule: where the likelihood of the data under our model is Because we are only interested in the classifier and aren’t trying to model , we will say that is actually a constant with respect to . Therefore, the log of the posterior is From this, we see that maximizing the posterior distribution with respect to is equivalent to minimizing the cross-entropy loss with respect to , where the regularization term corresponds to the prior term . Therefore, if we assume that gradient descent on the loss finds the minimizer of the loss, the estimate is a MAP estimate. As a short demonstration of the regularization term, suppose we impose a Gaussian prior centered around on the parameters, that is, . Then the prior becomes and we recover L2 regularization on the parameters, .
Gradient descent for posterior sampling
It’s nice that optimization of the cross-entropy loss gives the MAP estimate if gradient descent converges to a minimum. However, it’s also slightly unsatisfactory, because in Bayesian inference, knowledge of the entire posterior is preferred to a single point estimate of the posterior. It turns out that by slightly modifying gradient descent, we can sample from the posterior , effectively allowing the entire posterior to be characterized after sufficiently many samples.
In particular, gradient descent can be viewed as an instance of Markov chain Monte Carlo. Markov chain Monte Carlo is a technique to sample from a distribution: we first construct a Markov chain whose stationary distribution is the distribution of interest; then we can sample from the distribution by simulating the Markov chain for enough steps. The procedure can in fact be generalized to continuous-time analogues of Markov chains, known as Markov processes, a setting where powerful tools are available for analysis.
Just as a Markov chain can be specified by its transition probabilities, a Markov process can be specified by a stochastic differential equation (SDE). For example, consider the following SDE
where is the Wiener process, which describes Brownian motion. It is perhaps most intuitively understood as the limit of the discrete process (Markov chain) described by as , where the noise is sampled independently for every step. This discretization is known as the Euler–Maruyama scheme applied to (8). The stationary distribution of a Markov process like the one given can be computed by solving a partial differential equation known as the Fokker–Planck equation. It turns out that the stationary distribution of under the evolution of this SDE is where . Now, notice that the discretization (9) can be interpreted as gradient descent with added Gaussian noise at every iteration. With our choice of the cross-entropy loss, , so the stationary distribution of this Markov process is exactly the posterior . Therefore, if noise of the appropriate variance is injected into the update for gradient descent, each iteration of gradient descent may be seen approximately giving the next iterate in a Markov process, and if enough steps are executed, the iterates are approximately sampled from the posterior distribution. Under this procedure, then, when we stop training, can be thought of as an approximate sample from the posterior distribution. For more reading on this perspective, see Pavliotis (2014), especially chapter 4.8.
Stochastic gradient descent for posterior sampling
The above analysis applies to full-batch gradient descent, but this can be costly if the dataset is large. Can we instead use stochastic gradient descent for posterior sampling? Intuitively, it seems to be possible, as stochastic gradient descent can be thought of as gradient descent with a noisy gradient, which is exactly the dynamics described by (9).
Stochastic gradient descent operates by taking gradient steps where is an estimate of computed using minibatches of size : where is sampled uniformly from . Let’s try to write in terms of and some noise. Define so that and in particular, By the central limit theorem, if is large enough, is distributed as a Gaussian with mean . Denote the covariance of this Gaussian . After making the approximation we can write the SGD update as Unfortunately, this differs from the dynamics in (9), as the variance of the noise in (9) is proportional to , but here it is proportional to . Therefore, the iterates under normal SGD do not approach a stationary distribution given by the posterior. Chaudhari and Soatto (2017) give the SDE that SGD actually corresponds to and characterize its actual stationary distribution; see also Li et al. (2017) and Mandt et al. (2016) for further discussion. To correct this difference in dynamics, Welling and Teh (2011) suggest an alternative update to the SGD update, which they call stochastic gradient Langevin dynamics (SGLD): With this modification, the dynamics becomes This is still not of the form in (9); however, if the step size is taken to as training progresses, then the stochastic gradient noise will eventually be dominated by the injected noise . This scheme thereby converges as training progresses to the dynamics (9) and hence to the SDE (8). Thus, the iterates may eventually be viewed as samples from the posterior . SGLD can be thought of as the Bayesian version of SGD, one that allows sampling from the posterior rather than simply finding a point estimate. Chen et al. (2014) extend this idea even further by proposing stochastic gradient Hamiltonian Monte Carlo (SGHMC), which can be thought of as a Bayesian version of SGD with momentum. These techniques can be used when a fully Bayesian treatment is desired.
Originally written November 16, 2018.