Skip to main content

A Python package for building Bayesian models with TensorFlow or PyTorch

Project description

Version Badge Build Badge Docs Badge Coverage Badge

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 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 less painfully build, fit, and evaluate custom Bayesian models (or ready-made 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

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

can be built by creating a ProbFlow Model. This is just a class which inherits pf.Model (or pf.ContinuousModel or pf.CategoricalModel depending on the target type). The __init__ method sets up the parameters, and the __call__ method performs a forward pass of the model, returning the predicted probability distribution of the target:

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)
https://raw.githubusercontent.com/brendanhasz/probflow/master/docs/img/pred_dist_light.svg?sanitize=true

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)
https://raw.githubusercontent.com/brendanhasz/probflow/master/docs/img/posteriors_light.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)
0.903

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/coverage_light.svg?sanitize=true

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 multi-layer Bayesian neural network like this:

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

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):
        z = tf.nn.relu(self.core(x))
        return pf.Normal(self.mean(z), tf.exp(self.std(z)))

# 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 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(x.shape[1])
model.fit(x, y)

And a multi-layer Bayesian neural net can be made easily using ProbFlow’s ready-made 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 time-to-event models, neural matrix factorization models, and Gaussian mixture models. You can even mix probabilistic and non-probabilistic models! Take a look at the examples and the user guide for more!

Installation

If you already have your desired backend installed (i.e. Tensorflow/TFP or PyTorch), then you can just do:

pip install probflow

Or, to install both ProbFlow and the CPU version of TensorFlow + TensorFlow Probability,

pip install probflow[tensorflow]

Or, to install ProbFlow and the GPU version of TensorFlow + TensorFlow Probability,

pip install probflow[tensorflow_gpu]

Or, to install ProbFlow and PyTorch,

pip install probflow[pytorch]

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.4.1.tar.gz (62.3 kB view details)

Uploaded Source

Built Distribution

probflow-2.4.1-py3-none-any.whl (93.3 kB view details)

Uploaded Python 3

File details

Details for the file probflow-2.4.1.tar.gz.

File metadata

  • Download URL: probflow-2.4.1.tar.gz
  • Upload date:
  • Size: 62.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for probflow-2.4.1.tar.gz
Algorithm Hash digest
SHA256 0f226cfddda55e7f19686fcae444f26d01dff4601356a295e5d1c4e5b59a944a
MD5 884b1a195047e7a75004a70c8cfa6f04
BLAKE2b-256 fd1059652ffc3219e365d0cf0746c16626e2c6dbdb48bfcdabbb8b09bd4afccf

See more details on using hashes here.

File details

Details for the file probflow-2.4.1-py3-none-any.whl.

File metadata

  • Download URL: probflow-2.4.1-py3-none-any.whl
  • Upload date:
  • Size: 93.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for probflow-2.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dfed69848a0b19d925d5d27b6fa20eb0de1fd5c4c49bf2fcb9eb0a63750153d4
MD5 47d4ab8f0411fd08737352949092aa78
BLAKE2b-256 ad9fef30e8c74eea112b62e910ffb91b0f6a3410c8977dc2f85dc48acf4554cf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page