{"id":4801,"library":"tensorflow-probability","title":"TensorFlow Probability","description":"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.","status":"active","version":"0.25.0","language":"en","source_language":"en","source_url":"http://github.com/tensorflow/probability","tags":["machine-learning","deep-learning","statistics","probability","tensorflow","bayesian-inference","probabilistic-programming","glm"],"install":[{"cmd":"pip install tensorflow-probability","lang":"bash","label":"Stable release"},{"cmd":"pip install tensorflow-probability[tf]","lang":"bash","label":"With TensorFlow Keras 2 compatibility"},{"cmd":"pip install tensorflow-probability[tf-cpu]","lang":"bash","label":"With TensorFlow CPU Keras 2 compatibility"},{"cmd":"pip install tensorflow-probability[jax]","lang":"bash","label":"With JAX substrate"}],"dependencies":[{"reason":"Core library for numerical operations and deep learning integration. TFP versions are stable against specific TensorFlow versions (e.g., TFP 0.25.0 is stable with TF 2.18).","package":"tensorflow","optional":false},{"reason":"Required for Keras 2 compatibility when using TensorFlow 2.16+ (which defaults to Keras 3), as TFP is not compatible with Keras 3.","package":"tf-keras","optional":false},{"reason":"Alternative backend for accelerated numerical computing; enables TFP functionality without TensorFlow.","package":"jax","optional":true}],"imports":[{"symbol":"tfp","correct":"import tensorflow_probability as tfp"},{"symbol":"tfd","correct":"import tensorflow_probability.distributions as tfd"},{"symbol":"tfb","correct":"import tensorflow_probability.bijectors as tfb"},{"note":"The Generalized Linear Model module is aliased as `glm`, not `general_linear_model`.","wrong":"import tensorflow_probability.general_linear_model as tfg","symbol":"tfp.glm","correct":"import tensorflow_probability.glm as tfg"}],"quickstart":{"code":"import tensorflow as tf\nimport tensorflow_probability as tfp\n\ntfd = tfp.distributions\n\n# Generate synthetic data for logistic regression\nfeatures = tfd.Normal(loc=0., scale=1., name='features').sample(int(100e3))\nlabels = tfd.Bernoulli(logits=1.618 * features, name='labels').sample()\n\n# Specify a Bernoulli GLM (Generalized Linear Model)\nmodel = tfp.glm.Bernoulli()\n\n# Fit the model using Maximum Likelihood Estimation\ncoeffs, linear_response, is_converged, num_iter = tfp.glm.fit(\n    model_matrix=features[:, tf.newaxis],\n    response=tf.cast(labels, dtype=tf.float32),\n    model=model\n)\n\nprint(f\"Estimated Coefficients: {coeffs.numpy()}\")\nprint(f\"Converged: {is_converged.numpy()}\")","lang":"python","description":"This quickstart demonstrates fitting a simple Bayesian logistic regression model using TensorFlow Probability's Generalized Linear Model (GLM) module. It generates synthetic data from a Bernoulli distribution, defines a Bernoulli GLM, and then fits the model to estimate coefficients."},"warnings":[{"fix":"Explicitly install Keras 2 by running `pip install tf-keras` or `pip install tensorflow-probability[tf]`. Alternatively, set the environment variable `TF_USE_LEGACY_KERAS=1` before importing TensorFlow and TFP to force TensorFlow to use Keras 2 if `tf-keras` is installed.","message":"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`.","severity":"breaking","affected_versions":"TensorFlow Probability 0.24.0+ with TensorFlow 2.16+ and Keras 3."},{"fix":"Always check the TFP release notes for the recommended compatible TensorFlow version. For example, TFP 0.25.0 is stable against TensorFlow 2.18.","message":"TFP versions are tested and stable against specific TensorFlow versions. Using incompatible versions can lead to unexpected errors or instability.","severity":"gotcha","affected_versions":"All versions."},{"fix":"Upgrade your Python environment to 3.9 or newer. TFP 0.24.0 and later support Python 3.12.","message":"Support for Python 3.8 was removed in TensorFlow Probability 0.22.0.","severity":"breaking","affected_versions":"TensorFlow Probability >= 0.22.0"},{"fix":"Set the environment variable `WRAPT_DISABLE_EXTENSIONS=true` to work around the bug.","message":"When using Python 3.12, a TensorFlow + `wrapt` bug can cause errors in `tfp.layers` and `tfp.experimental.nn`.","severity":"gotcha","affected_versions":"TensorFlow Probability >= 0.24.0 with Python 3.12"},{"fix":"Be aware that `tfp.layers` is not fully supported, `tfp.math.minimize` does not work due to `tf.Variable` dependence, and `tf.Variable` equivalents are not present. Pseudorandomness handling also differs.","message":"TensorFlow Probability on JAX (using `tensorflow_probability.substrates.jax`) has some functional limitations compared to the TensorFlow backend.","severity":"gotcha","affected_versions":"All versions of TFP on JAX."}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}