CUQIpy-PyTorch
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.
Installation
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.
Quickstart
Example for sampling from the eight schools model:
$$ \begin{align*} \mu &\sim \mathcal{N}(0, 10^2)\ \tau &\sim \log\mathcal{N}(5, 1)\ \boldsymbol \theta' &\sim \mathcal{N}(\mathbf{0}, \mathbf{I}_m)\ \boldsymbol \theta &= \mu + \tau \boldsymbol \theta'\ \mathbf{y} &\sim \mathcal{N}(\boldsymbol \theta, \boldsymbol \sigma^2 \mathbf{I}_m) \end{align*} $$
where $\mathbf{y}\in\mathbb{R}^m$ and $\boldsymbol \sigma\in\mathbb{R}^m$ are observed data.
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.
Metadata
Release files for CUQIpy-PyTorch 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cuqipy_pytorch-0.5.0.tar.gz | 14.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cuqipy_pytorch-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.1 kB
Release files / cuqipy_pytorch-0.5.0.tar.gz
| Download URL | cuqipy_pytorch-0.5.0.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
badfa0fca21b509abc75e28b5c78e59382c1fa4c5fe1ac03940fe5e72cd6aa62
|
|
BLAKE2b-256 checksum How to use checksums |
021564d82767e117152ac39f6f72386078acfdbe8586b708ab46717c47c15e98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / cuqipy_pytorch-0.5.0-py3-none-any.whl
| Download URL | cuqipy_pytorch-0.5.0-py3-none-any.whl |
|---|---|
| Size | 15.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
267dd9b208aa5a874247cd57c0d241769aee7c581e6b885117816bcd90bd80e5
|
|
BLAKE2b-256 checksum How to use checksums |
acef5536cd11074d922708ba86c54f794840daf34cb4ecd847158d9e2f9fd287
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|