Install & Compatibility
Where this runs
tested against v0.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
645MB installed
● package 645MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
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
✗ import tensorflow_probability.general_linear_model as tfg
The Generalized Linear Model module is aliased as `glm`, not `general_linear_model`.
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.
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()}")
Debug
Known issues
breakingTensorFlow 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`.fixExplicitly 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.
affects: TensorFlow Probability 0.24.0+ with TensorFlow 2.16+ and Keras 3.
gotchaTFP versions are tested and stable against specific TensorFlow versions. Using incompatible versions can lead to unexpected errors or instability.fixAlways check the TFP release notes for the recommended compatible TensorFlow version. For example, TFP 0.25.0 is stable against TensorFlow 2.18.
affects: All versions.
breakingSupport for Python 3.8 was removed in TensorFlow Probability 0.22.0.fixUpgrade your Python environment to 3.9 or newer. TFP 0.24.0 and later support Python 3.12.
affects: TensorFlow Probability >= 0.22.0
gotchaWhen using Python 3.12, a TensorFlow + `wrapt` bug can cause errors in `tfp.layers` and `tfp.experimental.nn`.fixSet the environment variable `WRAPT_DISABLE_EXTENSIONS=true` to work around the bug.
affects: TensorFlow Probability >= 0.24.0 with Python 3.12
gotchaTensorFlow Probability on JAX (using `tensorflow_probability.substrates.jax`) has some functional limitations compared to the TensorFlow backend.fixBe 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.
affects: All versions of TFP on JAX.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'tensorflow_probability'
The tensorflow-probability library is not installed in the current Python environment or the environment is not activated.
fixRun `pip install tensorflow-probability` or `conda install -c conda-forge tensorflow-probability` to install the library.
ERROR: Could not find a version that satisfies the requirement tensorflow-probability
This error often indicates a conflict with the currently installed TensorFlow version, preventing pip from finding a compatible tensorflow-probability package.
fixInstall a `tensorflow` and `tensorflow-probability` version pair that is known to be compatible. Refer to the `tensorflow-probability` release notes for version compatibility, e.g., `pip install tensorflow==2.15.0 tensorflow-probability==0.24.0`.
NameError: name 'tfp' is not defined
The tensorflow_probability library was not imported using the standard alias 'tfp', or it was not imported at all.
fixAdd `import tensorflow_probability as tfp` at the beginning of your script to make the 'tfp' alias available.
ValueError: The event_shape of the input distribution must be fully defined.
This error typically occurs when creating complex distributions (e.g., `TransformedDistribution`) where the `event_shape` of a component distribution cannot be inferred or is not explicitly specified as a concrete shape at construction time.
fixEnsure all `event_shape` arguments for base distributions and bijectors are explicitly defined as concrete shapes (e.g., `[10]` or `tf.TensorShape([10])`) or can be fully inferred from their inputs before the graph is finalized.
Upgrade
Version history
0.25.0latest on PyPI · released Nov 8, 2024
Audit
Dependencies
tensorflowrequiredCore 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).
tf-kerasrequiredRequired for Keras 2 compatibility when using TensorFlow 2.16+ (which defaults to Keras 3), as TFP is not compatible with Keras 3.
jaxoptionalAlternative backend for accelerated numerical computing; enables TFP functionality without TensorFlow.