CS58700 - Foundations of Deep Learning¶

Spring 2026¶

Instructor and Copyright: Bruno Ribeiro¶

Bayesian Deep Learning: Markov Chain Monte Carlo & Hamiltonian Monte Carlo¶

Reading:¶

The reading for this lecture:

Reading:

  • (Neal, 2011) Neal, Radford M. MCMC using Hamiltonian dynamics. Handbook of Markov Chain Monte Carlo 2, no. 11 (2011).
  • Chib, Siddhartha, and Edward Greenberg. Understanding the Metropolis-Hastings Algorithm. The American Statistician 49, no. 4 (1995): 327-335

Suggested reading:

  • Geyer, Charles J. Markov Chain Monte Carlo Lecture Notes (1998). This is a complete graduate-level course covering Monte Carlo foundations, Metropolis-Hastings, and MCMC theory.

Bayesian Deep Learning¶

Suppose we have a neural network classifier $p({\bf y} | {\bf x} ; {\bf W})$ and a training dataset $\mathcal{D} = \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N$.

  • The likelihood of the data given the model is $$\mathcal{L}({\bf W},\mathcal{D}) = \prod_{i=1}^N p({\bf y}_i | {\bf x}_i ; {\bf W}).$$
  • The maximum likelihood estimator (MLE) obtains $$ {\bf W}^\star = \arg\!\max_{\bf W} \prod_{i=1}^N p({\bf y}_i | {\bf x}_i ; {\bf W}).$$
    • But ${\bf W}^\star$ is just one model. There may be weight matrices ${\bf W}' \neq {\bf W}^\star$ (models) that achieve the same or nearly the same likelihood.
    • If these other alternative models give the same (different) softmax outputs, this may allow us to be more (less) confident in our predictions.
  • Bayesian models: In Bayesian modeling, we want to be able to sample from the posterior of models given the data: $${\bf W}_\text{r.v.} \sim p({\bf W} | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N ).$$
    • Each sampled ${\bf W}_\text{r.v.}$ is a model. Then, the label prediction can be described as $$p_\text{Bayesian model}({\bf y} | {\bf x}; \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N) = \int_{\bf W} p({\bf y} | {\bf x} ; {\bf W}) p({\bf W} | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N ) d{\bf W},$$ which is clearly intractable.
    • In practice, we will sample a few models ${\bf W}^{(1)}_\text{r.v.},\ldots,{\bf W}^{(K)}_\text{r.v.}$ i.i.d. from ${\bf W}^{(k)}_\text{r.v.} \sim p({\bf W} | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N )$, $k=1,\ldots,K$. The following model output is an unbiased estimate of $p_\text{Bayesian model}({\bf y} | {\bf x})$: $$\hat{p}_\text{Bayesian model}({\bf y} | {\bf x}; \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N) = \frac{1}{K}\sum_{k=1}^K p({\bf y} | {\bf x} ; {\bf W}^{(k)}_\text{r.v.})$$

Bayes Theorem and Model Posterior¶

Q1: How do we obtain $p({\bf W} | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N )$?

A1: We can apply Bayes theorem.

  • Let $P({\bf W})$ be the prior probability of ${\bf W}$. For instance, ${\bf W} \sim \text{Normal}({\bf 0},{\bf I})$.
  • Bayes theorem: $$p({\bf W} | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N ) = \frac{\prod_{i=1}^N p({\bf y}_i | {\bf x}_i ; {\bf W}) P({\bf W})}{p( \{{\bf y}_i\}_{i=1}^N | \{{\bf x}_i\}_{i=1}^N)},$$ where $$p(\{{\bf y}_i\}_{i=1}^N | \{{\bf x}_i\}_{i=1}^N) = \int_{\bf W} \prod_{i=1}^N p({\bf y}_i | {\bf x}_i ; {\bf W}) P({\bf W}).$$

Rejection sampling (detour)¶

  • In rejection sampling, we want to sample $X$ from $p(x)$
    • First we need a proposal distribution $q(x)$ from which we will sample examples
      • Unlike $p(x)$, we know how to sample from $q(x)$.
      • E.g., $q(x)$ is a multivariate Normal distribution
    • First need to know a value $M$ such that $M \geq \sup_x \frac{p(x)}{q(x)}$.
    • We will then generate samples according to an acceptance-rejection sampling algorithm.

Acceptance-rejection sampling algorithm:

  1. sample $x \sim q(x)$ from the proposal distribution $q(x)$
  2. sample $u \sim \text{Uniform}(0,1)$ independently of $X$
  3. while $u > \frac{p(x)}{M q(x)}$, reject $x$ and GOTO 1:
  4. Accept (output) $x$ as a sample from $p(x)$.

Lemma: Let $X'$ be sampled using the acceptance-rejection algorithm. Then $X' \sim p(x)$.

Proof: Let $X' \sim \text{acc-rej algo}$ and $X \sim q(x)$, then $$ p(X' = x) = p(X = x| \text{Accept}) = \frac{p(X = x, \text{Accept})}{p(\text{Accept})} = p(x) $$ because $$ \frac{P(X = x, \text{Accept})}{P(\text{Accept})} = \frac{P(\text{Accept}|X = x) p(X = x)}{P(\text{Accept})} = \frac{\frac{p(x)}{M q(x)} q(x)}{P(\text{Accept})} = \frac{\frac{p(x)}{M}}{P(\text{Accept})} = p(x) $$ as $$ P(\text{Accept}) = \int P(\text{Accept}|X = x)p(X = x) dx = \int \frac{p(x)}{M q(x)} q(x) dx = \frac{1}{M} \int p(x) dx = \frac{1}{M} $$

Bayesian modeling: Posterior sampling via rejection sampling (impractical)¶

Acceptance-rejection sampling algorithm for posterior sampling.

Let $M \geq \frac{p({\bf W} | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N )}{P({\bf W})}, \forall {\bf W}$ be a constant:

  1. sample ${\bf W}' \sim q({\bf W}) = P({\bf W})$ from the proposal distribution $P({\bf W})$, which is our prior over the neuron weights
  1. sample $u \sim \text{Uniform}(0,1)$ independently of ${\bf W}'$
  2. while $u > \frac{p({\bf W}' | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N )}{M P({\bf W}')}$, reject ${\bf W}'$ and GOTO 1:
  1. Accept (output) ${\bf W}'$ as a sample from $p({\bf W} | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N )$.
  • Assumption:
    • $\text{supp}(p({\bf W} | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N )) \subseteq \text{supp}(q({\bf W}))$, i.e., the posterior support is contained in the support of the proposal distribution.
    • This assumption will be very important when we study GANs and diffusion models

The rejection/acceptance procedure in $$u > \frac{p({\bf W}' | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N )}{M P({\bf W}')} = \text{acceptance prob}$$ is not a complex as it seems.

Since $$ p({\bf W} | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N ) = \frac{\prod_{i=1}^N p({\bf y}_i | {\bf x}_i ; {\bf W}) P({\bf W})}{p( \{{\bf y}_i\}_{i=1}^N | \{{\bf x}_i\}_{i=1}^N)}, $$ we can simplify $$ \text{acceptance prob} = \frac{p({\bf W}' | \{({\bf x}_i,{\bf y}_i)\}_{i=1}^N )}{M P({\bf W}')}$$ as $$ \text{acceptance prob} = \frac{\prod_{i=1}^N p({\bf y}_i | {\bf x}_i ; {\bf W}') P({\bf W}')}{M' P({\bf W}')} = \frac{\prod_{i=1}^N p({\bf y}_i | {\bf x}_i ; {\bf W}')}{M' } = \frac{\mathcal{L}({\bf W}',\mathcal{D})}{M'}, $$ where

  • $M' \geq M p( \{{\bf y}_i\}_{i=1}^N | \{{\bf x}_i\}_{i=1}^N)$ and
  • $\mathcal{L}({\bf W},\mathcal{D})$ is the likelihood of the proposed model ${\bf W}'$ under our training dataset $\mathcal{D}$.

Defining $M'$¶

First, we can easily find $M'$ since, fundamentally, $M'$ is just making sure $\text{acceptance prob} \in (0,1]$, hence $$M' = \prod_{i=1}^N p({\bf y}_i | {\bf x}_i ; {\bf W}^\star),$$ where $$ {\bf W}^\star = \arg\!\max_{\bf W} \prod_{i=1}^N p({\bf y}_i | {\bf x}_i ; {\bf W}).$$

The above rejection sampling procedure is impractical¶

But if we understand the principles behind the above rejection sampling procedure, we will understand all current and future techniques for Bayesian deep learning.

As before:

  • ${\bf W}^\star$ is the MLE solution
  • $P({\bf W})$ is the prior probability

Note:

  • Note that ${\bf W}^\star$ is a very good sample, since it is accepted with probability one.
    • Likely, $({\bf W}^\star + \epsilon)$ for some small $\epsilon$ is also a model that will be accepted.
    • What if our proposal distribution knew that ${\bf W}^\star$ was a good proposal and tried to propose similar models?
      • Define the proposal now as a 1st order Markov chain: $q({\bf W}_{t+1} | {\bf W}_t)$ and let ${\bf W}_0 = {\bf W}^\star$.
      • Can we sample from $P({\bf W})$ by sampling from some well-designed Markov chain $q({\bf W}_{t+1} | {\bf W}_t)$?
        • But sampling from $P({\bf W})$ still causes too many rejections... can we improve it?
  • Can we think of 1st Markov chains $q({\bf W}_{t+1} | {\bf W}_t)$ that would give us better steady state proposals than $P({\bf W})$?
    • This is probably a complex function that must consider the likelihood $\mathcal{L}({\bf W},\mathcal{D})$ in the proposals

Markov Chain Monte Carlo¶

Markov Chain Monte Carlo (MCMC) gives us a way to sample from a complex high-dimensional distribution $q({\bf x})$ by sampling from intermediate, simpler (lower-dimensional) distributions, which generally gives us dependent samples $x_1,\ldots,x_n$.

The idea behind MCMC is simple.

  • Suppose the domain $\mathbb{D}$ is finite, to simplify our exposition
    • A more complex but similar proof works on continuous state spaces like ${\bf W}$.
  • Since we have a finite domain $\mathbb{D}$, and enumerate the elements of $\mathbb{D}$ as ${\bf d}_1,{\bf d}_2,\ldots,|\mathbb{D}|$.
  • We cannot sample directly from $q({\bf x})$ but suppose we knew how to construct a Markov Chain with transition matrix ${\bf P}$ whose unique stationary distribution was $q({\bf x})$.
    • That is, if we define $$ {\bf P}_{{\bf x}_{t},{\bf x}_{t+1}} = q({\bf x}_{t+1} | {\bf x}_t)$$ and if $${\bf x}_t \sim q({\bf x}),$$ then $$x_{t+1} \sim q({\bf x} | {\bf x}_t)$$ is also sampled from $q({\bf x})$.
  • In the matrix ${\bf P}$, the $i$-th row represents a conditional probability distribution $(q(d_j | d_i))_{j=1}^{|\mathbb{D}|}$.
    • ${\bf P}$ is denoted the stochastic matrix.

To undestand why the above is true, let's describe the distribution $q({\bf x})$ as a column vector ${\bf q} = (q(d_1),q(d_2),\ldots)$.

For some arbitrary starting distribution ${\bf q}^{(0)}$, consider the recursion $$ {\bf q}^{(t+1)} = {\bf q}^{(t)}{\bf P} . $$

Note that $$ \lim_{t \to \infty} {\bf q}^{(t)} = {\bf q}^{(0)} {\bf P P P}\cdots = {\bf q}^{(0)}{\bf P}^\infty . $$

  • If there is a nonzero probability of transitioning from any state ${\bf x}$ to any other state ${\bf x}'$ for some power $t$:
    • Then the Perron-Frobenius theorem (Perron, 1907; Frobenius, 1908) guarantees that the largest eigenvalue is real and equal to 1.
      • Because the matrix is stochastic, all other eigenvalues must be in the range $(1,-1]$.
    • Note that the above condition is trivially guaranteed if $q({\bf x}' | {\bf x}) > 0$ for any ${\bf x}, {\bf x}' \in \mathbb{D}$.

If we add the condition that the Markov chain is aperiodic, all other eigenvalues are in $(1,-1)$. From the spectral decomposition of ${\bf P} = {\bf V} \text{diag}(\boldsymbol{\lambda}) {\bf V}^{-1}$, where $\boldsymbol{\lambda}$ are the eigenvalues of ${\bf P}$, we see that $$ {\bf P}^t = \Bigg({\bf V} \text{diag}(\boldsymbol{\lambda}) {\bf V}^{-1} \Bigg)^t = {\bf V} \text{diag}(\boldsymbol{\lambda}^t) {\bf V}^{-1}. $$

Thus, the process thus converges to an unique stationary distribution sometimes also called the equilibrium distribution. And this unique stationary distribution ${\bf q}$, has the following property: $$ {\bf q} = {\bf q} {\bf P} . $$

Algorithm 1: The convergence property of Markov chains gives the following algorithm to sample from $q({\bf x})$:

  1. $t \leftarrow 0$
  2. sample ${\bf x}_0 \sim q^{(0)}({\bf x})$ from any initial distribution $q^{(0)}$
  3. sample ${\bf x}_{t+1} \sim q({\bf x} | {\bf x}_t)$
  4. $t \leftarrow t + 1$ and GOTO 3:
  5. output ${\bf x}_{\infty}$

Then, ${\bf x}_\infty \sim q({\bf x})$.

Metropolis-Hastings Sampling¶

Metropolis-Hastings sampling will merge rejection sampling with the Markov chain sampling of Algorithm 1 and get rid of the problematic constant $M$.

Consider the Markov chain sampling Algorithm 1 again,

Algorithm 1 (revisiting):

  1. $t \leftarrow 0$
  2. sample ${\bf x}_0 \sim q^{(0)}({\bf x})$ from any initial distribution $q^{(0)}$
  3. sample ${\bf x}_{t+1} \sim q({\bf x} | {\bf x}_t)$
  4. $t \leftarrow t + 1$ and GOTO 3:
  5. output ${\bf x}_{\infty} \sim q({\bf x})$

Suppose $q({\bf x} | {\bf x}_t)$ is also intractable. In Metropolis-Hastings we will use rejection sampling to sample from the 1st order Markov chain $q({\bf x} | {\bf x}_t)$.

Rejection sampling (Refresh)¶

Acceptance-rejection sampling algorithm (for $M \geq \sup_x \frac{p(x)}{q(x)}$):

  1. sample $x \sim q(x)$ from the proposal distribution $q(x)$
  2. sample $u \sim \text{Uniform}(0,1)$ independently of $X$
  3. while $u > \frac{p(x)}{M q(x)}$, reject $x$ and GOTO 1:
  4. Accept (output) $x$ as a sample from $p(x)$.

Rejection sampling is very difficulty in high-dimensional domains (e.g., images) because of the large value of $M$.

Building the Markov chain $q({\bf x} | {\bf x}_t)$ also via rejection sampling¶

  • The Metropolis Hastings algorithm starts with a target objective density $$\text{target density := }p({\bf x}).$$
  • The proposal distribution is a conditional density ${\bf x}_{t+1} \sim q({\bf x}|{\bf x}_t)$ that has full support over $p({\bf x})$ (i.e., can generate all samples that $p$ can generate).
  • The general requirement for $q$ is that the ratio $$ \frac{p({\bf x}')}{q({\bf x}'|{\bf x})}$$ is known for any ${\bf x}'$ and ${\bf x}$ up to a constant that is independent of ${\bf x}$.

The Metropolis-Hastings algorithm is a type of acceptance-rejection sampling algorithm:

  1. $t \leftarrow 0$, ${\bf x}_0 \sim q^{(0)}$.
  2. sample ${\bf x} \sim q({\bf x}|{\bf x}_t)$ from the proposal distribution $q({\bf x}|{\bf x}_t)$
  3. sample $u \sim \text{Uniform}(0,1)$ independently of ${\bf x}$
  4. If $$u > \frac{p({\bf x}) q({\bf x}_t|{\bf x})}{p({\bf x}_t) q({\bf x}|{\bf x}_t) }:$$ Reject ${\bf x}$ and make ${\bf x}_{t+1} \leftarrow {\bf x}_t$.
  5. Otherwise: Accept ${\bf x}$ and make ${\bf x}_{t+1} \leftarrow {\bf x}$.
  6. $t \leftarrow t + 1$
  7. GOTO 2:
  8. Accept (output) ${\bf x}_\infty$ as a sample from $p({\bf x})$.

Practical Issues¶

  • Metropolis-Hastings is an easy way to construct a posterior sampler.
  • The trick is finding a proposal Markov chain distribution that keeps the rejection probability low.
    • Note that Gibbs sampling is generally more efficient than the Metropolis-Hastings algorithm as the Gibbs sampling "proposal" is always accepted.

Metropolis-Hastings for Bayesian Deep Learning¶

Consider a supervised learning task with data $\mathcal{D} = \{(x_i,y_i)\}_{i=1}^N$. Consider a deep neural network with paramters ${\bf W}$ with likelihood function $$ \text{likelihood} = p(\mathcal{D}|{\bf W}). $$

If we want a Bayesian view of deep learning, we will sample model paramters according to $$ p({\bf W} | \mathcal{D}) = \frac{p(\mathcal{D} | {\bf W}) p({\bf W})}{\int p(\mathcal{D} | {\bf W}) p({\bf W}) d{\bf W}} = \frac{p(\mathcal{D} | {\bf W}) p({\bf W})}{p(\mathcal{D})}. $$ The parameter prior $p({\bf W})$ is a standard Normal: $\text{Normal}({\bf 0},{\bf I})$. The challenge is sampling ${\bf W}$.

Approach 1: Rejection sampling (for a constant $M$).

  1. Sample ${\bf W}_0 \sim p({\bf W})$
  2. sample $u \sim \text{Uniform}(0,1)$ independently of ${\bf W}_0$.
  3. while $u > \frac{p({\bf W} | \mathcal{D})}{M p({\bf W})}$, reject ${\bf W}_0$ and GOTO 1:
  4. Accept (output) ${\bf W}_0$ as a sample from $p({\bf W} | \mathcal{D})$.

Note that $$ \frac{p({\bf W} | \mathcal{D})}{M p({\bf W})} = \frac{p(\mathcal{D} | {\bf W})}{M p(\mathcal{D})} = \frac{p(\mathcal{D} | {\bf W})}{M'}, $$ where $M' = M p(\mathcal{D})$. Note that $$M' \geq \max_{\bf W} p(\mathcal{D} | {\bf W} ).$$

$M'$ can be very large for large datasets $(N \gg 1)$, forcing us to reject most samples. This happens when the Normal prior is a poor proposal.

Using Metropolis-Hastings for better proposals¶

The Metropolis-Hastings algorithm is a type of acceptance-rejection sampling algorithm:

  1. $t \leftarrow 0$
  2. sample ${\bf W}' \sim q({\bf W}|{\bf W}_t)$ from the proposal distribution $q({\bf W}|{\bf W}_t)$
  3. sample $u \sim \text{Uniform}(0,1)$ independently of ${\bf W}_0$
  4. If $$u > \frac{p({\bf W}'| \mathcal{D}) q({\bf W}_t|{\bf W}')}{p({\bf W}_t | \mathcal{D}) q({\bf W}'|{\bf W}_t) }:$$ Reject ${\bf W}'$ and make ${\bf W}_{t+1} \leftarrow {\bf W}_t$.
  5. Otherwise: Accept ${\bf W}'$ and make ${\bf W}_{t+1} \leftarrow {\bf W}'$.
  6. $t \leftarrow t + 1$
  7. GOTO 2:
  8. Accept (output) ${\bf W}_\infty$ as a sample from $p({\bf W} | \mathcal{D})$.
  • Example of $q({\bf W}|{\bf W}_t)$: If set $q({\bf W}|{\bf W}_t)$ is to set ${\bf W}' = {\bf W}_t + \Sigma$, where $\Sigma$ is a low-variance zero mean Normal variable of the same dimension as ${\bf W}$.

Q1: Given the above ${\bf W}' = {\bf W}_t + \Sigma$, $\Sigma \sim \text{Normal}({\bf 0},\sigma {\bf I})$, $\sigma > 0$, how do we compute the ratio $$\frac{q({\bf W}_t|{\bf W}')}{q({\bf W}'|{\bf W}_t)} ?$$

A1: The ratio is one, as $\text{Normal}({\bf 0},\sigma {\bf I})$ is symmetric around the x-axis and $q({\bf W}'|{\bf W}_t) = p(\Sigma)$ and $q({\bf W}_t|{\bf W}') = p(-\Sigma)$.

  • The challenge of Metropolis-Hastings is still a high rejection rate (in general better than rejection sampling, but not good enough yet).

Hamiltonian Monte Carlo¶

There are three desirable properties we want to have:

  1. $q({\bf W}_t|{\bf W}') = q({\bf W}'|{\bf W}_t)$ so we don't have to compute this ratio; and
  2. We should use the gradient $\frac{\partial}{\partial {\bf W}} p(\mathcal{D}| {\bf W})\Big\vert_{{\bf W}_t}$ to make better proposals.
  3. We want to alleviate the Brownian motion-type behavior (e.g., randomly samping Normals) to reduce correlation between samples.

Metropolis-adjusted Langevin algorithm (MALA)¶

  • We will first use gradients to make a new proposal distribution: $$ {\bf W}' \sim q({\bf W} | {\bf W}_t) = \text{Normal}\bigg({\bf W}_t - \frac{\sigma^2}{2} \frac{\partial}{\partial {\bf W}} U({\bf W})\Big\vert_{{\bf W}_t}, \sigma^2 {\bf I}\bigg), $$ for $\sigma > 0$, where $$U({\bf W}) = - \log \bigg( p(\mathcal{D}| {\bf W}) p({\bf W}) \bigg),$$ with $p({\bf W}) = \exp(-\frac{1}{2}\Vert {\bf W} \Vert^2_2)/(2 \pi)^{d/2},$ where $d$ is the number parameters in ${\bf W}$ (i.e., $d$ is the dimension of $\text{vec}({\bf W})$.

The above proposal is equivalent to $$ {\bf W}' = {\bf W}_t - \frac{\sigma^2}{2} \frac{\partial}{\partial {\bf W}} U({\bf W})\Big\vert_{{\bf W}_t} + \sigma \Sigma, $$ where $\Sigma \sim \text{Normal}({\bf 0}, {\bf I})$.

  • We then accept the proposal with the usual Metropolis-Hastings acceptance procedure.
  • Note that as $\sigma \to 0$, the proposals become symmetric: $$\frac{q({\bf W}_t|{\bf W}')}{q({\bf W}'|{\bf W}_t)} = 1$$ and we will accept all proposals since ${\bf W}' \approx {\bf W}_t$.
    • This procedure is equivalent to a Lavengin diffusion, a set of stochastic differential equations with $p({\bf W} | \mathcal{D})$ as solution

Hamiltonian Monte Carlo (HMC)¶

  • Hamiltonian Monte Carlo extends MALA to allow multiple steps in the proposal.
  • Our goal is to make proposals ${\bf W}_{t+1} \sim q({\bf W}| {\bf W}_{t+1})$ that will always be accepted.
  • Let's call $U({\bf W})$ the potential energy of position ${\bf W}$.
  • Let's introduce an auxiliary variable $\phi$ of the same dimentionality of ${\bf W}$ where $$ \phi \sim \text{Normal}({\bf 0},R),$$ where $R$ is a covariance matrix and a hyperparameter of the algorithm.
  • Then, define $$K(\phi) = -\log(p(\phi)) = \frac{1}{2} \phi^T R^{-1} \phi + \frac{1}{2} \log(2 \pi^d |R|),$$ where $d$ is the dimension of $\text{vec}({\bf W})$.

Call $\phi$ the momentum and $K(\phi)$ the kinetic energy. where

  • The total energy of the system (Hamiltonian) $$H({\bf W},\phi) = U({\bf W}) + K(\phi).$$
  • This implies the following statistical symmetry (invariance): $$ p({\bf W},\phi|\mathcal{D}) = p({\bf W}|\mathcal{D}) p(\phi|\mathcal{D}) = p({\bf W}|\mathcal{D}) p(\phi) = \frac{1}{C} \exp(-U({\bf W})) \exp(-K(\phi)) = \frac{1}{C} \exp(-H({\bf W},\phi)), $$ where $C = P(\mathcal{D})$ is a normalizing constant.
  • Consider the following:
    • Marginally sample $$ \phi_t \sim \text{Normal}({\bf 0},R).$$
    • Marginally sample $$ {\bf W}_t \sim p({\bf W}|\mathcal{D}).$$
    • The $p({\bf W}_t,\phi_t|\mathcal{D})$ is the joint of some Hamiltonian $$H({\bf W}_t,\phi_t) = U({\bf W}_t) + K(\phi_t).$$
    • Any other values of ${\bf W}'$ and $\phi'$ with the same Hamiltonian $H({\bf W}_t,\phi_t) = H({\bf W}',\phi')$ will have the same joint probability $p({\bf W}_t,\phi_t|\mathcal{D}) = p({\bf W}',\phi'|\mathcal{D})$
    • Hence, if we treat ${\bf W}'$ and $\phi'$ as proposals for a "rejection sampling" algorithm:
      • But since $p({\bf W}',\phi'|\mathcal{D}) = p({\bf W}_t,\phi_t| \mathcal{D})$ that there is nothing to reject in our joint proposal ${\bf W}'$ and $\phi'$ since $\frac{p({\bf W}',\phi'|\mathcal{D})}{p({\bf W}_t,\phi_t| \mathcal{D})} = 1.$
  • Note that $p({\bf W}',\phi'|\mathcal{D})$ has $p({\bf W}'| \mathcal{D})$ as its marginal.
    • Hence, by sampling from $p({\bf W}',\phi'|\mathcal{D})$ and throwing away $\phi'$, we are sampling from the posterior $p({\bf W}| \mathcal{D})$.
  • Here, we will use the deterministic Hamiltonian dynamics above to make a deterministic update to both $\phi$ and ${\bf W}$.

Noether's theorem states that for any physical law with a conserved quantity (e.g., energy conservation), there must be symmetry, in this case the proposals $$\frac{q({\bf W}_t, \phi_t|{\bf W}',\phi')}{q({\bf W}',\phi'|{\bf W}_t,\phi_t)} = 1$$ are symmetic. And this observation is at the heart of Radford Neil's ingenuous Hamiltonian Monte Carlo method.

Hamiltonian Monte Carlo (HMC) Algorithm:

  1. $t \leftarrow 0$
  2. Sample $\phi_t \sim \text{Normal}({\bf 0},R)$.
  3. Simulate Hamiltonian dynamics to get proposal $({\bf W}',\phi')$ from $({\bf W_t},\phi_t)$.
  4. ${\bf W}_{t+1} \leftarrow {\bf W}'$. (assumes acceptance probability is one)
  5. $t \leftarrow t + 1$.
  6. GOTO 2:

Simulating the Hamiltonian Dynamics:

Dicretization of Hamiltonian dynamics into discrete steps introduces errors. The resulting Markov chain is not longer symmetric (reversible) if simulated naively.

Solution: Leapfrog dynamics. Repeat $T/\epsilon$ steps, where $\epsilon > 0$ is a discretization of the dynamics and $T > 0$ is an abitrary constant.

For $\tau = 0 \text{ to } T$ in $\epsilon$ increments: $$ \begin{align} {\bf W}_{t + \tau/T + \epsilon/2} &= {\bf W}_{t+ \tau/T} + (R^{-1} \phi_{t+\tau/T}) \frac{\epsilon}{2} \\ \phi_{\tau + \epsilon} &= \phi_{t+\tau/T} + \left(\frac{\partial}{\partial W} U({\bf W})\Big\vert_{{\bf W}_{t+\tau/T+ \epsilon/2}}\right) \epsilon \\ {\bf W}_{t + \tau/T + \epsilon} &= {\bf W}_{t+\tau/T + \epsilon/2} + (R^{-1} \phi_{t+\tau/T + \epsilon}) \frac{\epsilon}{2} \end{align}$$

You can verify that the above Markov chain is reversible, that is, $$p({\bf W}_{t+1},\phi_{t+1} | \mathcal{D})p({\bf W}_{t},\phi_{t} | {\bf W}_{t+1},\phi_{t+1}, \mathcal{D}) = p({\bf W}_{t},\phi_{t} | \mathcal{D})p({\bf W}_{t+1},\phi_{t+1} | {\bf W}_{t},\phi_{t}, \mathcal{D}).$$

However, the acceptance probability is no longer one, and we need to perform a true Metropolis-Hastings acceptance step.

Final HMC Algorithm. HMC Algorithm with imperfect Hamiltonian Simulation:

  1. $t \leftarrow 0$
  2. Sample $\phi_t \sim \text{Normal}({\bf 0},R)$.
  3. Simulate Hamiltonian dynamics to get proposal $(\mathbf{W}',\phi')$.
  4. sample $u \sim \text{Uniform}(0,1)$ independently of $X$
  5. if $u > \exp(H(\mathbf{W}',\phi')- H(\mathbf{W}_t,\phi_t)):$
    • Reject $({\bf W}',\phi')$ and make ${\bf W}_{t+1} \leftarrow {\bf W}_{t}$.
  6. else: accept and let $\mathbf{W}_{t+1} \leftarrow \mathbf{W}'$.
  7. $t \leftarrow t + 1$
  8. GOTO 2:

Stochastic Gradient MCMC (Welling & Teh, 2011)¶

Recall the negative log-likelihood for parameters with a prior $p(W)$, $W \in \mathbb{R}^d$, is: $$\frac{\partial}{\partial W} \mathcal{L}(\mathcal{D}^\text{train}; W) = - \frac{1}{N} \sum_{i=1}^N \frac{\partial}{\partial W} \log p(y_i | x_i; W) - \frac{1}{N} \frac{\partial}{\partial W} \log p(W),$$ where if $W$ is a multivariate standard normal, then $\frac{\partial}{\partial W} \log p(W) \propto \Vert W \Vert_2^2$.

For a learning rate $\eta$, the update equations are: $$ m^{(t)} = (1 - \eta \gamma) m^{(t-1)} - \eta n \frac{\partial}{\partial W} \mathcal{L}(\mathcal{D}^\text{train}; W) + \sqrt{2 \gamma \eta T} M^{1/2} R^{(t)}, $$ where $$ W^{(t)} = W^{(t-1)} + \eta M^{-1} m^{(t)}, $$ where

  • $\eta$ is the learning rate

  • $R^{(t)} \sim N_d(0, I_d)$ is a sample from a standard normal vector.

  • $\gamma$ is a friction parameter

  • $T$ is a temperature parameter

    • $T$ scales the effect of the noise.
    • $T \to 0$ turns SG-MCMC into SGD
    • $T \to \infty$ makes SG-MCMC ignore the likelihood
  • The mass matrix $M$ is a preconditioner, and if we use no preconditioner then M = I, such that all M-related terms vanish from the equations.

  • There should be an accept/reject Metropolis-Hastings corrections in SG-MCMC.

    • However, if $\eta \to 0$, one can show that the acceptance probability tends to one.
SG-MCMC in Literature¶

Best results achieved with cold posteriors $T < 1$:

  • (Li et al., 2016) $T = 1/\sqrt{N}$.

  • (Leimkuhler et al., 2019) $T < 10^{-3}$.

  • (Heek & Kalchbrenner, 2020) $T = 1/5$.

  • (Zhang et al., 2020) $T = 1/\sqrt{50000}$.

  • Zhang et al. (2020) proposes to use to perform cycles of iterations $t = 1, 2, \ldots$ with a high-to-low step size schedule.

    • Another way to understand the idea is to consider the start of the cycle as having high temperatures ($T$ large) and end the cycle having low temperatures ($T$ small).
    • Still, sampling largely uses cold posteriors $T < 1$.
  • (Wenzel et al., 2020) does a deep-drive into the necessity of cold posteriors for Bayesian deep learning.

References¶

  • (Neal, 2011) Neal, Radford M. "MCMC using Hamiltonian dynamics." Handbook of Markov Chain Monte Carlo 2, no. 11 (2011): 2.

  • (Hinton, 2006) G. Hinton, To Recognize Shapes, First Learn to Generate Images, Tech Report 2006 http://www.cs.toronto.edu/~fritz/absps/montrealTR.pdf

  • Wenzel, Florian, Kevin Roth, Bastiaan S. Veeling, Jakub Świątkowski, Linh Tran, Stephan Mandt, Jasper Snoek, Tim Salimans, Rodolphe Jenatton, and Sebastian Nowozin. "How good is the bayes posterior in deep neural networks really?." ICML 2020.

  • Li, C., Chen, C., Carlson, D., and Carin, L. Preconditioned stochastic gradient Langevin dynamics for deep neural networks. In Thirtieth AAAI Conference on Artificial Intelligence, 2016.

  • Heek, J. and Kalchbrenner, N. Bayesian inference for large scale image classification. In International Conference on Learning Representations (ICLR 2020), 2020

  • Zhang, R., Li, C., Zhang, J., Chen, C., and Wilson, A. G. Cyclical stochastic gradient MCMC for Bayesian deep learning. In International Conference on Learning Representations (ICLR 2020), 2020

  • Leimkuhler, B., Matthews, C., and Vlaar, T. Partitioned integrators for thermodynamic parameterization of neural networks. arXiv preprint arXiv:1908.11843, 2019.

  • Florian Wenzel, Kevin Roth, Bastiaan S. Veeling, Jakub Świątkowski, Linh Tran, Stephan Mandt, Jasper Snoek, Tim Salimans, Rodolphe Jenatton, Sebastian Nowozin, How Good is the Bayes Posterior in Deep Neural Networks Really?, ICML 2020, https://arxiv.org/abs/2002.02405

In [ ]: