TensorFlow Probability
TensorFlow Probability (TFP) is a Python library built on TensorFlow that facilitates probabilistic reasoning and statistical analysis. It seamlessly integrates probabilistic models with deep learning on modern hardware (TPUs, GPUs) by providing a wide selection of probability distributions, bijectors, probabilistic layers, variational inference, Markov chain Monte Carlo, and optimizers. Currently at version 0.25.0, TFP follows a regular release cadence, typically aligning with TensorFlow releases.
Warnings
- breaking TensorFlow Probability is NOT compatible with Keras 3. Starting with TensorFlow 2.16+, `pip install tensorflow` installs Keras 3 by default. Attempting to use TFP with Keras 3 will result in `AttributeError` upon import or usage of `tfp.layers`.
- gotcha TFP versions are tested and stable against specific TensorFlow versions. Using incompatible versions can lead to unexpected errors or instability.
- breaking Support for Python 3.8 was removed in TensorFlow Probability 0.22.0.
- gotcha When using Python 3.12, a TensorFlow + `wrapt` bug can cause errors in `tfp.layers` and `tfp.experimental.nn`.
- gotcha TensorFlow Probability on JAX (using `tensorflow_probability.substrates.jax`) has some functional limitations compared to the TensorFlow backend.
Install
-
pip install tensorflow-probability -
pip install tensorflow-probability[tf] -
pip install tensorflow-probability[tf-cpu] -
pip install tensorflow-probability[jax]
Imports
- tfp
import tensorflow_probability as tfp
- tfd
import tensorflow_probability.distributions as tfd
- tfb
import tensorflow_probability.bijectors as tfb
- tfp.glm
import tensorflow_probability.glm as tfg
Quickstart
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
# Generate synthetic data for logistic regression
features = tfd.Normal(loc=0., scale=1., name='features').sample(int(100e3))
labels = tfd.Bernoulli(logits=1.618 * features, name='labels').sample()
# Specify a Bernoulli GLM (Generalized Linear Model)
model = tfp.glm.Bernoulli()
# Fit the model using Maximum Likelihood Estimation
coeffs, linear_response, is_converged, num_iter = tfp.glm.fit(
model_matrix=features[:, tf.newaxis],
response=tf.cast(labels, dtype=tf.float32),
model=model
)
print(f"Estimated Coefficients: {coeffs.numpy()}")
print(f"Converged: {is_converged.numpy()}")