Izmailov 2021 - Full-Batch HMC for Neural Networks

Summary

The paper’s sampler is plain HMC on the full-data log-posterior, parallelized over 512 TPUv3 devices, with no data augmentation, smooth (Swish) activations and filter-response normalization so that the likelihood is a legitimate function of the weights. Trajectory length is set by , step sizes around give high acceptance, and a single long chain is almost as good as several.

Overview

A BNN’s predictions are the Bayesian model average (BMA) (eq. 1), approximated as with . HMC (Neal et al. 2011) produces asymptotically exact samples given the unnormalized density and its gradient. The authors do not recommend HMC for practice; they use it as the most precise instrument available to study the BNN posterior.

Main Content

Why full-batch

Stochastic-gradient MCMC bias

SGMCMC (SGLD, SGHMC) is fundamentally biased because (1) it omits the Metropolis-Hastings correction and (2) subsampling noise perturbs the stationary distribution; Betancourt (2015) argues HMC is incompatible with data subsampling. Random data augmentation likewise makes the log-density stochastic, with no clean interpretation as a valid likelihood (Wenzel et al. 2020), so the main evaluations use no data augmentation.

Model and implementation choices

ChoiceValueReason
ArchitecturesResNet-20-FRN (CIFAR), CNN-LSTM (IMDB, from Wenzel et al.)follows Wenzel et al. 2020
NormalizationFilter response normalization instead of batch normbatch norm couples training examples, obscuring the likelihood
ActivationsSwish/SiLU, not ReLUsmooth posterior surface improves HMC acceptance without hurting accuracy
Hardware512 TPUv3, SPMD: data sharded, each device keeps a synchronized chain copy, full-batch gradients via collectives; JAX code releasedfull-batch gradient
DataCIFAR-10/100 (a 40,960-point subset so the data shards evenly), IMDB; UCI regression-
CostA CIFAR-10 HMC run equals over 60 million epochs of SGD trainingnot a practical recipe

Hyperparameters

Trajectory length rule (eq. 2)

where is the prior standard deviation of the weights. For a spherical Gaussian target the Hamiltonian is a set of independent harmonic oscillators with period , so is a quarter-turn that swaps position and momentum; HMC with small step size then yields exact samples. The authors argue the prior sets the posterior scale in large BNNs (checked empirically in Appendix C).

  • Trajectory length ablation (Fig. 1a): accuracy and log-likelihood improve with longer ; performs well on accuracy, log-likelihood and ECE for CIFAR-10 and IMDB. “Despite the expense, when applying HMC to BNNs it is actually helpful to use tens of thousands of gradient evaluations per iteration” (up to leapfrog steps; earlier work used fewer than 100).
  • Step size (ResNet-20-FRN, CIFAR-10, 50 iterations, MH correction ignored): gave mean accept probabilities of 72.2%, 46.3%, 22.2%, 12.5% and BMA log-likelihoods of . Smaller steps cost proportionally more gradients because leapfrog steps .
  • Number of chains (Fig. 1b): at fixed compute, 2 or 3 chains are only slightly better than 1, so HMC is “relatively unobstructed by energy barriers” (see Izmailov 2021 - Mixing Diagnostics and Posterior Geometry).
  • Main runs: CIFAR: 3 chains, step size , prior variance , 70,248 leapfrog steps per sample, first 50 samples discarded as burn-in; IMDB: step , 3 chains, 50 burn-in then 400 samples per chain (1,200 total). UCI: single chain, 10 burn-in and 90 samples.

Examples

Applying the trajectory rule. Prior with prior variance gives , so . With step that is about leapfrog steps, consistent with the reported 70,248.

import numpy as np
alpha = np.sqrt(1/5); tau_hat = np.pi*alpha/2; step = 1e-5
print(round(tau_hat,3), int(tau_hat/step))     # 0.702  70248

(The calculation reproduces the paper’s number of leapfrog steps; checked when writing this note.)

HMC on a small network in PyMC (sketch, untested).

with pm.Model() as bnn:
    W1 = pm.Normal("W1", 0, 1/np.sqrt(n_in),  shape=(n_in, H))
    W2 = pm.Normal("W2", 0, 1/np.sqrt(H),     shape=(H, 1))     # 1/sqrt(width) scaling
    mu = pt.dot(pt.tanh(pt.dot(X, W1)), W2).ravel()
    pm.Normal("y", mu, pm.HalfNormal("s", 1), observed=y)
    idata = pm.sample(target_accept=0.9)           # NUTS; diagnose in function space

Connections

See Also

Izmailov 2021 - Bayesian Neural Network Posteriors - Overview, Izmailov 2021 - Cheaper Approximations versus HMC.