TensorFlow Probability Nightly
TensorFlow Probability (TFP) is a library for probabilistic modeling and statistical inference built on TensorFlow. This `tfp-nightly` package provides the latest pre-release versions of TFP, offering cutting-edge features and bug fixes. It is typically updated daily and is rigorously tested against nightly builds of TensorFlow and JAX. The current version is 0.26.0.dev20260415.
Common errors
-
ModuleNotFoundError: No module named 'tensorflow_probability.python.internal.backend.keras'
cause TensorFlow 2.16+ uses Keras 3, but TFP expects Keras 2. The Keras 2 backend module cannot be found at the expected path.fixInstall `tf-keras` (or `tf-keras-nightly`) using `pip install tf-keras`. You may also need to explicitly configure TFP to use this Keras version if issues persist. -
ValueError: Python 3.8 is no longer supported by TensorFlow Probability.
cause Attempting to install or run `tfp-nightly` on an unsupported Python 3.8 environment.fixUpgrade your Python environment to 3.9, 3.10, or 3.11. -
AttributeError: module 'tensorflow.python.framework.ops' has no attribute 'uid'
cause This error typically indicates an incompatibility between the installed `tfp-nightly` and `tensorflow-nightly` versions, where one expects an API that the other no longer provides or has changed.fixEnsure both `tensorflow-nightly` and `tfp-nightly` are updated to their very latest versions: `pip install --upgrade tensorflow-nightly tfp-nightly`. -
TypeError: 'int' object is not iterable
cause This can occur due to subtle API changes in nightly builds, especially concerning how shapes, dtypes, or specific arguments are handled in distributions or bijectors. It might also relate to NumPy deprecation warnings and changes in array casting.fixCheck the latest `tfp-nightly` release notes or GitHub issues. Ensure all input tensors have explicit shapes and correct dtypes. For instance, ensure single-element tensors are not implicitly cast to Python integers when iterating is expected.
Warnings
- breaking TensorFlow Probability currently relies on Keras 2 for its Keras backend integration. With TensorFlow 2.16+ (including `tensorflow-nightly`), `tf.keras` now points to Keras 3. This leads to incompatibility issues.
- breaking Support for Python 3.8 has been officially removed from TensorFlow Probability.
- gotcha `tfp-nightly` is built and tested against `tensorflow-nightly` and specific JAX versions. Using `tfp-nightly` with stable TensorFlow releases or significantly older/newer `jax` versions can lead to runtime errors or unexpected behavior due to API mismatches.
- gotcha As a nightly build, `tfp-nightly` may introduce API changes, experimental features, or deprecations without strict backward compatibility guarantees. Code written for stable `tensorflow-probability` might break with `tfp-nightly`.
Install
-
pip install tfp-nightly
Imports
- tfp
import tensorflow_probability as tfp
- tfd
import tensorflow_probability.distributions as tfd
- tfb
import tensorflow_probability.bijectors as tfb
- tf
import tensorflow as tf
Quickstart
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
tfb = tfp.bijectors
# Create a Normal distribution
mean = tf.constant(0.0, dtype=tf.float32)
stddev = tf.constant(1.0, dtype=tf.float32)
normal_dist = tfd.Normal(loc=mean, scale=stddev)
# Sample from the distribution
samples = normal_dist.sample(10)
print("Samples from Normal distribution:", samples)
# Calculate log probability
log_prob = normal_dist.log_prob(0.5)
print("Log probability of 0.5:", log_prob)
# Demonstrate a bijector (e.g., Exp) to create a LogNormal distribution
exp_bijector = tfb.Exp()
log_normal_dist = exp_bijector(normal_dist)
# Sample from the transformed distribution
log_normal_samples = log_normal_dist.sample(5)
print("Samples from LogNormal distribution (via Exp bijector):", log_normal_samples)