Package: probflow
ProbFlow is a Python package for building probabilistic Bayesian models withTensorFlow 2.0orPyTorch, 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.Git repository:http://github.com/brendanhasz/probflowDocumentation:http://probflow.readthedocs.ioBug reports:http://github.com/brendanhasz/probflow/issuesGetting StartedProbFlowallows you to quickly and less painfully build, fit, and evaluate custom Bayesian models (orready-madeones!) which run on top of eitherTensorFlow 2.0andTensorFlow ProbabilityorPyTorch.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 regressioncan be built by creating a ProbFlow Model.  This is just a class which inheritspf.Model(orpf.ContinuousModelorpf.CategoricalModeldepending 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:importprobflowaspfimporttensorflowastfclassLinearRegression(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):returnpf.Normal(x*self.weight()+self.bias(),self.std())model=LinearRegression()Then, the model can be fit using stochastic variational inference, inone line:# x and y are Numpy arrays or pandas DataFrame/Seriesmodel.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]Computeprobabilisticpredictions 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.217Inspect 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.903and diagnosewhereyour 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 multi-layer Bayesian neural network like this:Can be built and fit with ProbFlow in only a few lines:classDensityNetwork(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))returnpf.Normal(self.mean(z),tf.exp(self.std(z)))# Create the modelmodel=DensityNetwork([x.shape[1],256,128],[128,64,32,1])# Fit it!model.fit(x,y)For convenience, ProbFlow also includes severalpre-built modelsfor 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 likegeneralized
linear models,deep time-to-event models,neural matrix factorizationmodels, andGaussian mixture models.  You can even
mixprobabilistic and non-probabilistic models!  Take
a look at theexamplesand theuser guidefor more!InstallationIf you already have your desired backend installed (i.e. Tensorflow/TFP or
PyTorch), then you can just do:pipinstallprobflowOr, to install both ProbFlow and the CPU version of TensorFlow + TensorFlow
Probability,pipinstallprobflow[tensorflow]Or, to install ProbFlow and the GPU version of TensorFlow + TensorFlow
Probability,pipinstallprobflow[tensorflow_gpu]Or, to install ProbFlow and PyTorch,pipinstallprobflow[pytorch]SupportPost bug reports, feature requests, and tutorial requests inGitHub issues.ContributingPull requestsare 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.  ¯\_(ツ)_/¯