Documentation: tfp_api_docs
- The use of statistics to overcome uncertainty is one of the pillars of a large segment of the machine learning. Probabilistic reasoning has long been considered one of the foundations of inference algorithms and is represented is all major machine learning frameworks and platforms.
- Usually the classifications that you have arise and the predictions that we make, don't fall into a single category, or they fall into a category with some confidence level. Incorporating those probabilities is incredibly important for machine learning projects in the real world. Usually there is no single answer. There's this wide spectrum of answers that fall into some common distribution pattern.
- TensorFlow probability gives you the capability to take probabilistic distributions and integrate them directly with your Keras layers. TensorFlow probability despite not being part of TensorFlow Core, is an incredibly important part of the model building process.
import tensorflow as tf
import tensorflow_probability as tfp # tfp is a seprate library itself
# Pretend to load synthetic data set.
features = tfp.distributions.Normal(loc=0., scale=1.).sample(int(100e3))
labels = tfp.distributions.Bernoulli(logits=1.618 * features).sample()
# Specify model.
model = tfp.glm.Bernoulli()
# Fit model given data.
coeffs, linear_response, is_converged, num_iter = tfp.glm.fit(
model_matrix=features[:, tf.newaxis],
response=tf.cast(labels, dtype=tf.float32),
model=model)
# ==> coeffs is approximately [1.618] (We're golden!)
TensorFlow Probability (TFP) is a Python library built on TensorFlow that makes it easy to combine probabilistic models and deep learning on modern hardware (TPU, GPU). It's for data scientists, statisticians, ML researchers, and practitioners who want to encode domain knowledge to understand data and make predictions. TFP includes:
- A wide selection of probability distributions and bijectors.
- Tools to build deep probabilistic models, including probabilistic layers and a
JointDistribution
abstraction. - Variational inference and Markov chain Monte Carlo.
- Optimizers such as Nelder-Mead, BFGS, and SGLD
The TensorFlow Probability library provides a powerful set of tools, for statistical modeling, and makes it easy to extend our use of TensorFlow to probabilistic deep learning models. The TFP library, is part of the wider TensorFlow ecosystem, which contains a number of libraries and extensions for advanced and specialized use cases.
Feature | TensorFlow Probability (TFP) | TensorFlow Core (TF) |
---|---|---|
Purpose | Designed for probabilistic modeling and statistical inference. | General-purpose framework for machine learning and deep learning. |
Focus | Handles uncertainty, distributions, and Bayesian reasoning. | Builds and trains deterministic models like neural networks. |
Key Components | Probability distributions, Bayesian layers, MCMC, variational inference. | Layers, optimizers, losses, and metrics for supervised/unsupervised learning. |
Distributions | Extensive support for probability distributions (e.g., Normal, Poisson). | Limited to simple random number generation. |
Modeling Style | Probabilistic models with uncertainty quantification. | Deterministic models for tasks like classification, regression, etc. |
Monte Carlo Support | Built-in tools for Monte Carlo methods and variational inference. | No dedicated Monte Carlo functionality. |
Integration | Seamlessly integrates with TensorFlow Core for hybrid models. | Core framework for defining and optimizing computation graphs. |
Use Cases | Bayesian neural networks, uncertainty estimation, statistical analysis. | Supervised/unsupervised learning, reinforcement learning, deep learning. |
Target Audience | Statisticians, researchers, and Bayesian modelers. | Machine learning practitioners and data scientists. |
Understanding probability distributions is the foundation of modeling uncertainty in machine learning. TensorFlow Probability (TFP) provides tools to work with these distributions, enabling you to build probabilistic models, perform Bayesian inference, and generate realistic simulations. These distributions help you solve real-world problems like predicting outcomes, modeling uncertainties, and analyzing rare events.
π· a) Binomial Distribution
A Binomial Distribution describes the probability of success or failure outcomes in a repeated experiment (e.g., coin flips, test results). Itβs perfect for scenarios with two possible outcomes.
π Key Features:
- Two Outcomes: Success (S) or Failure (F).
- Fixed Trials (
n
): The number of times the experiment is repeated. - Probability (
p
): The likelihood of one specific outcome.
Binomial distribution helps model classification problems or situations involving yes/no outcomes. Example: Predicting whether a machine fails in n
trials.
The probability of getting exactly k
successes in n
trials is:
$$
f(k; n, p) = \binom{n}{k} p^k (1 - p)^{n-k}
$$
π· b) Poisson Distribution
The Poisson Distribution predicts how often a rare event occurs over a specific time or space interval. For example, how many customers arrive at a store in an hour.
π Key Features:
- Models rare events in a large population.
- Events are independent and occur at a constant rate.
Poisson distribution is used in time-series data, event modeling, or predicting rare occurrences. Example: Modeling customer arrivals or system failures.
$$
f(k; \lambda) = \frac{\lambda^k e^{-\lambda}}{k!}
$$
Here, Ξ»
(lambda) is the average number of events in a given interval.
π· c) Uniform Distribution
A Uniform Distribution is where all outcomes are equally likely. Example: Rolling a fair dice or tossing a coin.
π Key Features:
- All values between
a
andb
have the same probability. - Simple yet foundational for simulations and random sampling.
Uniform distribution is critical for generating random variables, initializing weights in neural networks, and performing Monte Carlo simulations.
Probability density function (PDF):
$$
f(x) =
\begin{cases}
\frac{1}{b-a} & a \leq x \leq b \
0 & \text{otherwise}
\end{cases}
$$
π· d) Gaussian (Normal) Distribution
The Gaussian Distribution (or Normal Distribution) is the famous bell curve, where most values cluster around the mean, and the probability tapers off symmetrically on both sides.
π Key Features:
- Defined by mean (ΞΌ) and standard deviation (Ο).
- Forms the basis of the Central Limit Theorem.
Gaussian distributions are everywhere in machine learning, from modeling errors to designing probabilistic models. They are essential for understanding uncertainty in predictions.
π· e) Exponential Distribution
The Exponential Distribution models the time until an event occurs. Example: How long until a server crashes, or the time between customer arrivals.
π Key Features:
- Describes the waiting time for events.
- Often used in reliability engineering and queueing theory.
Exponential distribution helps in survival analysis and modeling waiting times in sequential processes. Example: Predicting downtime of a machine or arrival rates in traffic.
Distribution | Key Use Case | Formula Highlights | Why Learn for TFP? |
---|---|---|---|
Binomial | Success/Failure Outcomes | Classifications, Yes/No Predictions | |
Poisson | Rare Events in Fixed Intervals | Modeling Events over Time/Space | |
Uniform | Equal Likelihood of Outcomes | Random Sampling, Initialization | |
Gaussian (Normal) | Symmetric Data Distribution | Probabilistic Models, Central Limit Theorem | |
Exponential | Time Until Next Event | Survival Analysis, Sequential Event Modeling |
1οΈβ£ βοΈ The TensorFlow Probability library
2οΈβ£ βοΈ Probabilistic layers and Bayesian neural networks
3οΈβ£ βοΈ Bijectors and Normalising Flows
4οΈβ£ βοΈ Variational autoencoders
5οΈβ£ βοΈ Capstone Project