What is Pyro?
Pyro is a flexible, scalable deep probabilistic programming library built on PyTorch. Developed by Uber AI Labs, Pyro aims to provide a unified platform for both deep learning and probabilistic modeling, allowing researchers and practitioners to build complex models that combine neural networks with probabilistic reasoning.
How does Pyro work?
Pyro provides a language for expressing probabilistic models as well as a suite of inference algorithms for learning model parameters and making predictions. Pyro models are defined using stochastic functions, which can be composed to build complex, hierarchical models. Pyro’s inference engine supports several algorithms, including variational inference, Markov chain Monte Carlo (MCMC), and sequential Monte Carlo (SMC).
Example of using Pyro in Python
To use Pyro, you first need to install the package:
$ pip install pyro-ppl
Here’s a simple example of using Pyro to define a Bayesian linear regression model:
import torch import pyro import pyro.distributions as dist def model(x, y): # Define priors w = pyro.sample("w", dist.Normal(torch.zeros(2), torch.ones(2))) sigma = pyro.sample("sigma", dist.Uniform(0, 10)) # Define likelihood mean = torch.matmul(x, w) with pyro.plate("data", len(y)): pyro.sample("obs", dist.Normal(mean, sigma), obs=y)
Additional resources on Pyro
- Pyro documentation
- Deep Universal Probabilistic Programming with Pyro
- Probabilistic Programming and Bayesian Methods for Hackers