Skip to main content

A Python package for building Bayesian models with TensorFlow or PyTorch

Project description

Build Status Build Status Documentation Status

ProbFlow is a Python package for building probabilistic Bayesian models with TensorFlow 2.0 or PyTorch, performing variational inference with those models, and evaluating the models’ inferences. It provides both high-level modules for building Bayesian neural networks, as well as low-level parameters and distributions for constructing custom Bayesian models.

It’s very much still a work in progress.

Getting Started

ProbFlow allows you to quickly and painlessly build, fit, and evaluate custom Bayesian models (or ready-made ones!) which run on top of TensorFlow 2.0 and TensorFlow Probability or PyTorch.

With ProbFlow, the core building blocks of a Bayesian model are parameters, probability distributions, and modules (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

https://raw.githubusercontent.com/brendanhasz/probflow/master/docs/img/readme/regression_equation.svg?sanitize=true

can be built by creating a ProbFlow Model object:

import probflow as pf

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 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)

Compute probabilistic predictions for new data, with 95% confidence intervals:

model.pred_dist_plot(x_test, ci=0.95)
https://raw.githubusercontent.com/brendanhasz/probflow/master/docs/img/readme/pred_dist.svg?sanitize=true

Evaluate your model’s performance using metrics:

model.metric('mse', x_test, y_test)

Inspect the posterior distributions of your fit model’s parameters, with 95% confidence intervals:

model.posterior_plot(ci=0.95)
https://raw.githubusercontent.com/brendanhasz/probflow/master/docs/img/readme/posteriors.svg?sanitize=true

Investigate how well your model is capturing uncertainty by examining how accurate its predictive intervals are:

model.pred_dist_coverage(ci=0.95)

and diagnose where your model is having problems capturing uncertainty:

model.coverage_by(ci=0.95)
https://raw.githubusercontent.com/brendanhasz/probflow/master/docs/img/readme/coverage.svg?sanitize=true

ProbFlow also provides more complex layers, such as those required for building Bayesian neural networks. Also, ProbFlow lets you mix and match ProbFlow objects with TensorFlow (or PyTorch!) objects and operations. For example, a multi-layer Bayesian neural network can be built and fit using ProbFlow in only a few lines:

import tensorflow as tf

class DenseRegression(pf.ContinuousModel):

    def __init__(self, input_dims):
        self.net = pf.Sequential([
            pf.Dense(input_dims, 128),
            tf.nn.relu,
            pf.Dense(128, 64),
            tf.nn.relu,
            pf.Dense(64, 1),
        ])
        self.std = pf.ScaleParameter(name='std')

    def __call__(self, x):
        return pf.Normal(self.net(x), self.std())

model = DenseRegression()
model.fit(x, y)

For convenience, ProbFlow also includes several pre-built models for standard tasks (such as linear regressions, logistic regressions, and multi-layer dense neural networks). For example, the above linear regression example could have been done with much less work by using ProbFlow’s ready-made LinearRegression model:

model = pf.LinearRegression(7)
model.fit(x, y)

And the multi-layer Bayesian neural net could have been made more easily by using ProbFlow’s ready-made DenseRegression model:

model = pf.DenseRegression([7, 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, neural matrix factorization models, and Gaussian mixture models. Take a look at the examples section 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 from the GitHub source:

pip install git+http://github.com/brendanhasz/probflow.git

Version 1 vs 2

The latest version of ProbFlow (version 2) was built to work with eager execution in TensorFlow 2.x and PyTorch. Version 1 does not work with eager execution, and only works with TensorFlow 1.x (and not PyTorch). The v2 interface is significantly different from v1, based on a subclassing API instead of the more declarative API of v1. I won’t be supporting v1 moving forward, but if you want to install ProbFlow 1.0:

pip install git+http://github.com/brendanhasz/probflow.git@v1.0

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.

Source Distribution

probflow-2.0.0a0.tar.gz (39.9 kB view hashes)

Uploaded source

Built Distribution

probflow-2.0.0a0-py3-none-any.whl (44.6 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page