A Python package for building Bayesian models with TensorFlow or PyTorch
Project description
ProbFlow is a Python package for building probabilistic Bayesian models with TensorFlow 2.0 or PyTorch, performing stochastic variational inference with those models, and evaluating the models’ inferences. It provides both highlevel modules for building Bayesian neural networks, as well as lowlevel parameters and distributions for constructing custom Bayesian models.
It’s very much still a work in progress.
 Git repository: http://github.com/brendanhasz/probflow
 Documentation: http://probflow.readthedocs.io
 Bug reports: http://github.com/brendanhasz/probflow/issues
Getting Started
ProbFlow allows you to quickly and less painfully build, fit, and evaluate custom Bayesian models (or readymade ones!) which run on top of either TensorFlow 2.0 and TensorFlow Probability or PyTorch.
With ProbFlow, the core building blocks of a Bayesian model are parameters and probability distributions (and, of course, the input data). Parameters define how the independent variables (the features) predict the probability distribution of the dependent variables (the target).
For example, a simple Bayesian linear regression
can be built by creating a ProbFlow Model:
import probflow as pf import tensorflow as tf class LinearRegression(pf.ContinuousModel): def __init__(self): self.weight = pf.Parameter(name='weight') self.bias = pf.Parameter(name='bias') self.std = pf.ScaleParameter(name='sigma') def __call__(self, x): return pf.Normal(x*self.weight()+self.bias(), self.std()) model = LinearRegression()
Then, the model can be fit using stochastic variational inference, in one line:
# x and y are Numpy arrays or pandas DataFrame/Series model.fit(x, y)
You can generate predictions for new data:
# x_test is a Numpy array or pandas DataFrame >>> model.predict(x_test) [0.983]
Compute probabilistic predictions for new data, with 95% confidence intervals:
model.pred_dist_plot(x_test, ci=0.95)
Evaluate your model’s performance using metrics:
>>> model.metric('mse', x_test, y_test) 0.217
Inspect the posterior distributions of your fit model’s parameters, with 95% confidence intervals:
model.posterior_plot(ci=0.95)
Investigate how well your model is capturing uncertainty by examining how accurate its predictive intervals are:
>>> model.pred_dist_coverage(ci=0.95) 0.903
and diagnose where your model is having problems capturing uncertainty:
model.coverage_by(ci=0.95)
ProbFlow also provides more complex modules, such as those required for building Bayesian neural networks. Also, you can mix ProbFlow with TensorFlow (or PyTorch!) code. For example, even a somewhat complex multilayer Bayesian neural network like this:
Can be built and fit with ProbFlow in only a few lines:
class DensityNetwork(pf.ContinuousModel): def __init__(self, units, head_units): self.core = pf.DenseNetwork(units) self.mean = pf.DenseNetwork(head_units) self.std = pf.DenseNetwork(head_units) def __call__(self, x): x = self.core(x) return pf.Normal(self.mean(x), tf.exp(self.std(x))) # Create the model model = DensityNetwork([x.shape[1], 256, 128], [128, 64, 32, 1]) # Fit it! model.fit(x, y)
For convenience, ProbFlow also includes several prebuilt models for standard tasks (such as linear regressions, logistic regressions, and multilayer dense neural networks). For example, the above linear regression example could have been done with much less work by using ProbFlow’s readymade LinearRegression model:
model = pf.LinearRegression(x.shape[1]) model.fit(x, y)
And a multilayer Bayesian neural net can be made easily using ProbFlow’s readymade DenseRegression model:
model = pf.DenseRegression([x.shape[1], 128, 64, 1]) model.fit(x, y)
Using parameters and distributions as simple building blocks, ProbFlow allows for the painless creation of more complicated Bayesian models like generalized linear models, deep timetoevent models, neural matrix factorization models, and Gaussian mixture models. Take a look at the examples and the user guide for more!
Installation
Before installing ProbFlow, you’ll first need to install either PyTorch, or TensorFlow 2.0 and TensorFlow Probability. See more details here.
Then, you can install ProbFlow itself:
pip install probflow
Support
Post bug reports, feature requests, and tutorial requests in GitHub issues.
Contributing
Pull requests are totally welcome! Any contribution would be appreciated, from things as minor as pointing out typos to things as major as writing new applications and distributions.
Why the name, ProbFlow?
Because it’s a package for probabilistic modeling, and it was built on TensorFlow. ¯\_(ツ)_/¯
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size  File type  Python version  Upload date  Hashes 

Filename, size probflow2.0.0a2py3noneany.whl (49.8 kB)  File type Wheel  Python version py3  Upload date  Hashes View 
Filename, size probflow2.0.0a2.tar.gz (44.7 kB)  File type Source  Python version None  Upload date  Hashes View 
Hashes for probflow2.0.0a2py3noneany.whl
Algorithm  Hash digest  

SHA256  5ee07fbdaf395c8a2fba6664f6549ba140b4a08d2ba35b95feca837910f81183 

MD5  9bbc63fb0af8bb0e19e5081d81518725 

BLAKE2256  246f5509d8f6c4a8f6bd70e9b1ae6530356f8b1d84baab582eb26efa973075ea 