Awesome Open Source
Awesome Open Source


|Version Badge| |Build Badge| |Docs Badge| |Coverage Badge|

.. |Version Badge| image:: :target:

.. |Build Badge| image:: :target:

.. |Docs Badge| image:: :target:

.. |Coverage Badge| image:: :target:

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

.. image:: :width: 30 % :align: center

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:

.. code-block:: python

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:

.. code-block:: python

# x and y are Numpy arrays or pandas DataFrame/Series, y)

You can generate predictions for new data:

.. code-block:: pycon

# x_test is a Numpy array or pandas DataFrame
>>> model.predict(x_test)

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

.. code-block:: python

model.pred_dist_plot(x_test, ci=0.95)

.. image:: :width: 90 % :align: center

Evaluate your model's performance using metrics:

.. code-block:: pycon

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

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

.. code-block:: python


.. image:: :width: 90 % :align: center

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

.. code-block:: pycon

>>> model.pred_dist_coverage(ci=0.95)

and diagnose where your model is having problems capturing uncertainty:

.. code-block:: python


.. image:: :width: 90 % :align: center

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:

.. image:: :width: 99 % :align: center

Can be built and fit with ProbFlow in only a few lines:

.. code-block:: python

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!, 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:

.. code-block:: python

model = pf.LinearRegression(x.shape[1]), y)

And a multi-layer Bayesian neural net can be made easily using ProbFlow's ready-made DenseRegression model:

.. code-block:: python

model = pf.DenseRegression([x.shape[1], 128, 64, 1]), 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!


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

.. code-block:: bash

pip install probflow

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

.. code-block:: bash

pip install probflow[tensorflow]

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

.. code-block:: bash

pip install probflow[tensorflow_gpu]

Or, to install ProbFlow and PyTorch,

.. code-block:: bash

pip install probflow[pytorch]


Post bug reports, feature requests, and tutorial requests in GitHub issues <>_.


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. ¯\(ツ)

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
python (51,962
machine-learning (3,528
pytorch (2,279
tensorflow (2,131
data-science (866
statistics (328
bayesian-inference (88
bayesian (48
bayesian-statistics (35
bayesian-methods (27

Find Open Source By Browsing 7,000 Topics Across 59 Categories