Skip to content

Probabilistic Deep Learning finds its application in autonomous vehicles and medical diagnoses. This is an increasingly important area of deep learning that aims to quantify the noise and uncertainty that is often present in real-world datasets.

License

Notifications You must be signed in to change notification settings

mohd-faizy/Probabilistic-Deep-Learning-with-TensorFlow

Repository files navigation

author made-with-Markdown Language Platform Maintained Last Commit GitHub issues Open Source Love svg2 Stars GitHub GitHub license contributions welcome Size

Probabilistic Deep Learning with TensorFlow

Documentation: tfp_api_docs

⁉️Why is probabilistic programming important for deep learning?

  • 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.

TensorFlow Probability is a library for probabilistic reasoning and statistical analysis.

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.


🎯TensorFlow Probability (TFP) vs TensorFlow Core (TF)

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.

🌟 Key Probability Distributions You Should Know

πŸ“Œ Why Are These Distributions Important in TensorFlow Probability (TFP)?

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.


What is it?

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.

Why It's Useful in TFP:

Binomial distribution helps model classification problems or situations involving yes/no outcomes. Example: Predicting whether a machine fails in n trials.

Formula

The probability of getting exactly k successes in n trials is:
$$ f(k; n, p) = \binom{n}{k} p^k (1 - p)^{n-k} $$


What is it?

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.

Why It's Useful in TFP:

Poisson distribution is used in time-series data, event modeling, or predicting rare occurrences. Example: Modeling customer arrivals or system failures.

Formula

$$ f(k; \lambda) = \frac{\lambda^k e^{-\lambda}}{k!} $$
Here, Ξ» (lambda) is the average number of events in a given interval.


What is it?

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 and b have the same probability.
  • Simple yet foundational for simulations and random sampling.

Why It's Useful in TFP:

Uniform distribution is critical for generating random variables, initializing weights in neural networks, and performing Monte Carlo simulations.

Formula

Probability density function (PDF):
$$ f(x) = \begin{cases} \frac{1}{b-a} & a \leq x \leq b \ 0 & \text{otherwise} \end{cases} $$


What is it?

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.

Why It's Useful in TFP:

Gaussian distributions are everywhere in machine learning, from modeling errors to designing probabilistic models. They are essential for understanding uncertainty in predictions.

Formula

$$ f(x) = \frac{1}{\sigma \sqrt{2\pi}} e^{-\frac{1}{2} \left(\frac{x-\mu}{\sigma}\right)^2} $$


What is it?

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.

Why It's Useful in TFP:

Exponential distribution helps in survival analysis and modeling waiting times in sequential processes. Example: Predicting downtime of a machine or arrival rates in traffic.

Formula

$$ f(x; \lambda) = \begin{cases} \lambda e^{-\lambda x} & x \geq 0 \\ 0 & x < 0 \end{cases} $$


πŸš€ Summary Table of Distributions

Distribution Key Use Case Formula Highlights Why Learn for TFP?
Binomial Success/Failure Outcomes $f(k; n, p) = \binom{n}{k} p^k (1-p)^{n-k}$ Classifications, Yes/No Predictions
Poisson Rare Events in Fixed Intervals $f(k; \lambda) = \frac{\lambda^k e^{-\lambda}}{k!}$ Modeling Events over Time/Space
Uniform Equal Likelihood of Outcomes $f(x) = \frac{1}{b-a}$ Random Sampling, Initialization
Gaussian (Normal) Symmetric Data Distribution $f(x) = \frac{1}{\sigma \sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}$ Probabilistic Models, Central Limit Theorem
Exponential Time Until Next Event $f(x; \lambda) = \lambda e^{-\lambda x}$ Survival Analysis, Sequential Event Modeling

Path

Notes

my_notes

1️⃣ βœ”οΈ The TensorFlow Probability library

2️⃣ βœ”οΈ Probabilistic layers and Bayesian neural networks

3️⃣ βœ”οΈ Bijectors and Normalising Flows

4️⃣ βœ”οΈ Variational autoencoders

5️⃣ βœ”οΈ Capstone Project

Star History

Star History Chart

Other Resources

$\color{skyblue}{\textbf{Connect with me:}}$


About

Probabilistic Deep Learning finds its application in autonomous vehicles and medical diagnoses. This is an increasingly important area of deep learning that aims to quantify the noise and uncertainty that is often present in real-world datasets.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published