NumPyro Documentation
repository·master·Indexed 25 days ago
https://github.com/pyro-ppl/numpyroA lightweight probabilistic programming library powered by JAX, providing a NumPy backend for Pyro. It supports high-performance autograd and JIT compilation to CPU, GPU, and TPU. NumPyro includes various inference algorithms such as MCMC (NUTS, MixedHMC, HMCECS, BarkerMH, HMCGibbs, SA), Stochastic Variational Inference (SVI) with multiple ELBO implementations, and Nested Sampling via jaxns. It features a distribution API compatible with PyTorch and supports TensorFlow Probability (TFP) distributions.
What's inside NumPyro
- NumPyro is a lightweight probabilistic programming library that provides a NumPy backend for Pyro. It leverages JAX for automatic differentiation and JIT compilation to GPU, CPU, and TPU. It is designed to be a flexible substrate for probabilistic modeling, supporting Pyro primitives, various inference algorithms, and a distribution API compatible with PyTorch.
Explore NumPyro model and inference examples
masterNumPyro provides various examples for different modeling and inference tasks. Key examples include:
- Bayesian Regression: Covers model writing, MCMC API, effect handlers, and custom inference utilities.
- Time Series Forecasting: Demonstrates converting for-loops to JAX's
lax.scanfor performance. - Discrete Latent Variables: Uses enumeration mechanisms for inference.
- Hierarchical Models: Implementation of the Baseball example using NUTS.
- Hidden Markov Models (HMM): Comparison with Stan implementations.
- Variational Autoencoders (VAE): Using Variational Inference with neural networks.
- Gaussian Processes: Using NUTS to sample posterior hyper-parameters.
- Horseshoe Regression: Implementing GLMs with Horseshoe priors.
- Statistical Rethinking: Translations of Richard McElreath's book to NumPyro.
Use non-centered reparameterization to fix MCMC pathologies
masterIf your MCMC chain shows high divergence counts or low effective sample size (
n_eff), you can use non-centered reparameterization. This is achieved by usingnumpyro.handlers.reparamwith a reparameterization configuration.For distributions with
locandscaleparameters (likeNormal,Cauchy, orStudentT), you can useLocScaleReparam(centered=0)to automatically handle the transformation.Choose an Automatic Guide for Variational Inference
masterNumPyro provides several
AutoGuideclasses to automatically generate variational distributions (guides) for your models. Use the following guide to select an appropriate one based on your needs:Basic Mean-Field Guides
Best for starting with variational inference. They automatically handle non-Euclidean latent spaces (e.g., positivity constraints) using bijective transformations.
AutoNormal: Basic mean-field guide.AutoDiagonalNormal: Basic mean-field guide.
Multivariate Normal Guides
These capture correlations in the posterior but may be difficult to fit in high-dimensional settings.
AutoMultivariateNormal: Full multivariate Normal distribution.AutoLowRankMultivariateNormal: Low-rank multivariate Normal distribution.
Normalizing Flow Guides
Provide highly flexible variational distributions parameterized by normalizing flows.
AutoBNAFNormal: Uses BNAF flows.AutoIAFNormal: Uses IAF (Inverse Autoregressive Flow) flows.
Advanced HMC-based Guides
Powerful algorithms that leverage HMC. Good for highly correlated posteriors but can be computationally expensive.
AutoDAIS: Standard DAIS algorithm.AutoSurrogateLikelihoodDAIS: DAIS with support for data subsampling.AutoSemiDAIS: Supports data subsampling by using a parametric guide for global latent variables while using DAIS-style approximations for local latent variables.
Other Specialized Guides
AutoDelta: Used for computing point estimates via MAP (Maximum A Posteriori) estimation.AutoLaplaceApproximation: Computes a Laplace approximation.AutoGuideList: Used to combine multiple automatic guides.
Migrate Pyro models to NumPyro
masterNumPyro supports most Pyro primitives (
sample,param,plate,module, and effect handlers). To use a Pyro model in NumPyro, consider the following changes:- Replace Torch operations: Convert
torchoperations tojax.numpyoperations. - Handle Randomness: Wrap
pyro.samplestatements outside of inference contexts in aseedhandler. - Parameter Retrieval: Since there is no global parameter store, use
SVI.get_params()to retrieve optimized values from SVI.numpyro.paramworks inside models via thesubstituteeffect handler. - Neural Networks: Rewrite PyTorch modules using
staxorflax. - Functional Style: JAX works best with functional code. If your model has side-effects that are not visible to the JAX tracer, rewrite it in a more functional style to leverage JIT compilation.
- Replace Torch operations: Convert
Handle random number generation in NumPyro
masterUnlike Pyro,
numpyro.samplerequires an explicit random number generator key because JAX does not have a global random state. If you callnumpyro.sampleoutside of an inference context, you have three options:Directly call the distribution and provide a PRNG key:
dist.Normal(0, 1).sample(key)Provide the
rng_keyargument tonumpyro.sample:numpyro.sample('x', dist.Normal(0, 1), rng_key=key)Use a
seedhandler as a context manager or a higher-order function to thread the key automatically.
Using
seedas a context manager:with handlers.seed(rng_seed=0): x = numpyro.sample("x", dist.Beta(1, 1)) y = numpyro.sample("y", dist.Bernoulli(x))Using
seedas a higher-order function:def fn(): x = numpyro.sample("x", dist.Beta(1, 1)) y = numpyro.sample("y", dist.Bernoulli(x)) return y print(handlers.seed(fn, rng_seed=0)())with handlers.seed(rng_seed=0): x = numpyro.sample("x", dist.Beta(1, 1)) y = numpyro.sample( "y", dist.Bernoulli(x) )Build NumPyro documentation
masterTo build the documentation from the top-level directory, use themake docscommand. To build the HTML pages specifically, usemake html.make docs # or make htmlInstall NumPyro dependencies
masterInstall the required dependencies for NumPyro using the provided requirements file.
pip install -r requirements.txtInstall NumPyro
masterYou can install NumPyro using
piporconda.CPU Installation
To install the latest CPU version of JAX and NumPyro:
pip install numpyroIf you encounter compatibility issues, force a known compatible CPU version:
pip install 'numpyro[cpu]'GPU Installation
To use NumPyro on a GPU, you must install CUDA first. Use the command corresponding to your CUDA version:
For CUDA 12.x.y:
pip install 'numpyro[cuda12]' -f https://storage.googleapis.com/jax_cuda_releases.htmlFor CUDA 13.x.y:
pip install 'numpyro[cuda13]' -f https://storage.googleapis.com/jax_cuda_releases.htmlCloud TPU Installation
- Set up the TPU backend following the Cloud TPU VM JAX Quickstart Guide.
- Install NumPyro via
pip install numpyro.
Other Installation Methods
Conda:
conda install -c conda-forge numpyroFrom Source:
git clone https://github.com/pyro-ppl/numpyro.git cd numpyro # install jax/jaxlib first for CUDA support pip install -e '.[dev]'pip install numpyroUse NumPyro optimization algorithms
masterNumPyro provides several optimizer classes that wrap JAX optimizers to work seamlessly with NumPyro inference algorithms (like
SVI). These optimizers can be passed directly to inference objects.Available optimizers include:
AdamClippedAdam: Adam with gradient clipping between[-clip_norm, clip_norm].AdagradMomentumRMSPropRMSPropMomentumSGDSM3Minimize: A wrapper forjax.scipy.optimize.minimize(currently only supports theBFGSmethod). Note thatMinimizeis intended for use with static guides (e.g., MLE, MAP, orAutoLaplaceApproximation) and may be difficult to converge in stochastic settings.
Use `condition` handler for forecasting models
masterWhen designing a model that needs to perform both inference on observed data and forecasting on unobserved future data, use the
numpyro.handlers.conditionhandler instead of theobs=argument insample().By using
condition(data={'y': y}), you can provide observed values for the training period. When the model is later used for forecasting (where the sequence length exceeds the length of the conditioned data), NumPyro will automatically sample the unobserved values from the specified distribution rather than raising an index-out-of-bounds error.Run MCMC inference with NUTS
masterTo perform Bayesian inference using the No-U-Turn Sampler (NUTS), follow these steps:
- Define your model as a Python callable using
numpyro.sampleprimitives. - Initialize the
NUTSkernel with your model. - Create an
MCMCinstance specifying the kernel,num_warmup, andnum_samples. - Execute the inference using
mcmc.run(), providing a JAXPRNGKeyand any necessary model arguments.
Note: JAX requires explicit PRNG keys for all stochastic operations. Use
jax.random.splitto manage keys.Methods available on the
MCMCobject:run(...): Runs warmup, adapts step size/mass matrix, and performs sampling.print_summary(): Prints diagnostic information (quantiles, effective sample size, Gelman-Rubin diagnostic).get_samples(): Retrieves the posterior distribution samples.warmup(...): Performs only the warmup phase.
from numpyro.infer import MCMC, NUTS from jax import random # Initialize kernel and MCMC kernel = NUTS(model) num_samples = 2000 mcmc = MCMC(kernel, num_warmup=1000, num_samples=num_samples) # Run inference rng_key = random.key(0) rng_key, rng_key_ = random.split(rng_key) mcmc.run( rng_key_, marriage=dset.MarriageScaled.values, divorce=dset.DivorceScaled.values ) # Retrieve results mcmc.print_summary() samples = mcmc.get_samples()- Define your model as a Python callable using