CUQIpy-PyTorch is a plugin for the CUQIpy software package.
It adds a PyTorch backend to CUQIpy, allowing the user to use the PyTorch API to define models, distributions etc.
It also links to the Pyro No U-Turn Hamiltonian Monte Carlo sampler (NUTS) for efficient sampling from the joint posterior.
For optimal performance consider installing PyTorch using conda, then install CUQIpy-PyTorch using pip:
pip install cuqipy-pytorch
If PyTorch, Pyro or CUQIpy are not installed, they will be installed automatically from the above command.
Example for sampling from the eight schools model:
where
import torch as xp
from cuqi.distribution import JointDistribution
from cuqipy_pytorch.distribution import Gaussian, Lognormal
from cuqipy_pytorch.sampler import NUTS
# Observations
y_obs = xp.tensor([28, 8, -3, 7, -1, 1, 18, 12], dtype=xp.float32)
σ_obs = xp.tensor([15, 10, 16, 11, 9, 11, 10, 18], dtype=xp.float32)
# Bayesian model
μ = Gaussian(0, 10**2)
τ = Lognormal(5, 1)
θp = Gaussian(xp.zeros(8), 1)
θ = lambda μ, τ, θp: μ+τ*θp
y = Gaussian(θ, cov=σ_obs**2)
# Posterior sampling
joint = JointDistribution(μ, τ, θp, y) # Define joint distribution
posterior = joint(y=y_obs) # Define posterior distribution
sampler = NUTS(posterior) # Define sampling strategy
samples = sampler.sample(N=500, Nb=500) # Sample from posterior
# Plot posterior samples
samples["θp"].plot_violin();
print(samples["μ"].mean()) # Average effect
print(samples["τ"].mean()) # Average variance
For more examples, see the demos folder.