TensorFlow Probability

0.25.0 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

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()}")

view raw JSON →