Skip to content

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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

133 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Probabilistic Deep Learning

Probabilistic Deep Learning with TensorFlow Banner

Unlock the power of uncertainty quantification, density estimation, and generative modeling with TensorFlow Probability.

TensorFlow TensorFlow Probability Platform

Stars GitHub Issues Last Commit Repo Size

Author Maintained License Contributions Welcome

This repository is a comprehensive collection of TensorFlow Probability implementations for probabilistic deep learning. The primary goal is educational: to bridge the gap between traditional deterministic models and real-world uncertainty quantification.

This repository provides hands-on implementations of probabilistic deep learning using TensorFlow Probability (TFP), enabling you to build models that not only make predictions but also quantify how confident they are about those predictions.

Documentation: TFP API Docs

🎯 Overview

tfp-map

What Makes This Repository Special?

Traditional machine learning models provide point estimates without quantifying uncertainty. In critical applications like medical diagnosis, autonomous vehicles, or financial modeling, knowing how confident your model is can be the difference between success and catastrophic failure.

  • Enables models to express confidence levels using probabilistic layers and Bayesian neural networks.

  • Supports sampling, log-likelihood evaluation, and manipulation of complex distributions (univariate & multivariate).

  • Powers VAEs and normalizing flows for density estimation, representation learning, and synthetic data generation.

This repository demonstrates how TensorFlow Probability transforms your standard neural networks into probabilistic powerhouses that:

  • Quantify uncertainty in predictions
  • Model complex distributions beyond simple Gaussian assumptions
  • Perform Bayesian inference at scale
  • Generate realistic synthetic data through advanced generative models

Why Probabilistic Deep Learning Matters

Description

Real-world data is messy, incomplete, and uncertain. Probabilistic deep learning addresses these challenges by:

  • Handling Data Scarcity: Bayesian approaches work well with limited data.
  • Robust Decision Making: Uncertainty estimates guide better decisions.
  • Interpretable AI: Understanding model confidence builds trust
  • Anomaly Detection: Identifying outliers and unusual patterns.
  • Risk Assessment: Quantifying potential failure modes.

βš™οΈTechnical Strengths

  • Bayesian neural networks: Adds priors to weights and calibrates predictive uncertainty for out-of-distribution robustness.

  • Normalizing flows: Uses invertible transforms for expressive density estimation and efficient sampling.

  • Variational inference: Optimizes ELBO with reparameterization for controllable generation and learning.

πŸš€Trade-offs & Performance

  • Higher memory and training time than deterministic models.
  • Gains in interpretability, calibrated risk, and anomaly detection often outweigh the cost

πŸ”§ Prerequisites

Mathematical Background

  • Linear Algebra: Matrix operations, eigenvalues, SVD
  • Calculus: Derivatives, gradients, optimization
  • Statistics: Probability theory, Bayes' theorem, distributions
  • Information Theory: KL divergence, entropy, mutual information

Programming Skills

  • Python 3.8+ with object-oriented programming
  • TensorFlow/Keras fundamentals
  • NumPy/SciPy for numerical computing
  • Matplotlib/Seaborn for visualization

Recommended Reading


πŸš€ Quick Start

  1. Clone the repository:

    git clone https://github.com/mohd-faizy/Probabilistic-Deep-Learning-with-TensorFlow.git
    cd Probabilistic-Deep-Learning-with-TensorFlow
  2. Create virtual environment (using uv – ⚑ faster alternative):

    # Install uv if not already installed
    pip install uv
    
    # Create and activate virtual environment
    uv venv
    
    # Activate the env
    source .venv/bin/activate   # Linux/macOS
    .venv\Scripts\activate      # Windows
  3. Install dependencies:

    uv add -r requirements.txt
  4. Verify installation:

    import tensorflow as tf
    import tensorflow_probability as tfp
    
    print(f"TensorFlow: {tf.__version__}")
    print(f"TensorFlow Probability: {tfp.__version__}")

⚑ Quick Example

import tensorflow as tf
import tensorflow_probability as tfp

tfd = tfp.distributions

# Create a probabilistic model
def create_bayesian_model():
    model = tf.keras.Sequential([
        tfp.layers.DenseVariational(
            units=64,
            make_prior_fn=lambda: tfd.Normal(0., 1.),
            make_posterior_fn=tfp.layers.default_mean_field_normal_fn(),
            kl_weight=1/50000
        ),
        tf.keras.layers.Dense(10, activation='softmax')
    ])
    return model

# Train with uncertainty quantification
model = create_bayesian_model()
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')

🎲 Core Probability Distributions

Understanding core probability distributions is fundamental for designing probabilistic models. In TensorFlow Probability, distributions are treated as first-class computational objects. They are grouped here by their mathematical type: Discrete, Continuous, and Multivariate.

πŸ“‹ Distributions Quick Reference

Distribution Type Support Parameters TFP Class
Bernoulli 🟒 Discrete ${0, 1}$ $p \in [0, 1]$ or $\text{logits} \in \mathbb{R}$ tfd.Bernoulli
Binomial 🟒 Discrete ${0, 1, \dots, n}$ $n \in \mathbb{N}^+$, $p \in [0, 1]$ tfd.Binomial
Poisson 🟒 Discrete ${0, 1, 2, \dots}$ $\lambda > 0$ (rate) tfd.Poisson
Gaussian (Normal) πŸ”΅ Continuous $\mathbb{R}$ $\mu \in \mathbb{R}$, $\sigma > 0$ tfd.Normal
Exponential πŸ”΅ Continuous $[0, \infty)$ $\lambda > 0$ (rate) tfd.Exponential
Beta πŸ”΅ Continuous $(0, 1)$ $\alpha, \beta > 0$ (concentration) tfd.Beta
Multivariate Gaussian 🌐 Multivariate $\mathbb{R}^k$ $\boldsymbol{\mu} \in \mathbb{R}^k$, covariance $\boldsymbol{\Sigma} \in \mathbb{R}^{k \times k}$ tfd.MultivariateNormalTriL

πŸ“Š Discrete Distributions

Discrete distributions model count data, binary trials, or categorical events where outcomes are distinct, countable values.

🟒 Bernoulli Distribution (tfd.Bernoulli)

Bernoulli Distribution

Mathematical Formulation: $$P(X = x) = p^x (1-p)^{1-x}, \quad x \in {0, 1}$$

  • Support: $x \in {0, 1}$
  • Parameters: Probability of success $p \in [0, 1]$ (or log-odds $\text{logits} \in \mathbb{R}$)
  • Typical DL Use Cases: Binary classification outputs, Variational Autoencoder (VAE) decoder output for binary data (e.g. MNIST pixels), and stochastic dropout masks.

TFP Implementation:

import tensorflow_probability as tfp
tfd = tfp.distributions

# Define via probability of success
bernoulli = tfd.Bernoulli(probs=0.7)

# Define via logits (log-odds, preferred for neural networks)
bernoulli_logits = tfd.Bernoulli(logits=0.85)



🟒 Binomial Distribution (tfd.Binomial)

Binomial Distribution

Mathematical Formulation: $$P(X = k) = \binom{n}{k} p^k (1-p)^{n-k}, \quad k \in {0, 1, \dots, n}$$

  • Support: $k \in {0, 1, \dots, n}$
  • Parameters: Number of trials $n \in \mathbb{N}^+$, success probability $p \in [0, 1]$
  • Typical DL Use Cases: A/B testing models, click-through-rate (CTR) modeling, quality control and defect counts.

TFP Implementation:

import tensorflow_probability as tfp
tfd = tfp.distributions

# 15 independent trials with success probability 0.4
binomial = tfd.Binomial(total_count=15., probs=0.4)



🟒 Poisson Distribution (tfd.Poisson)

Poisson Distribution

Mathematical Formulation: $$P(X = k) = \frac{\lambda^k e^{-\lambda}}{k!}, \quad k \in {0, 1, 2, \dots}$$

  • Support: $k \in {0, 1, 2, \dots}$
  • Parameters: Average event rate $\lambda > 0$
  • Typical DL Use Cases: Count regression models (Poisson regression), web traffic/API request volume forecasting, and anomaly detection in system logs.

TFP Implementation:

import tensorflow_probability as tfp
tfd = tfp.distributions

# Average rate of 5.0 events per interval
poisson = tfd.Poisson(rate=5.0)



πŸ“ˆ Continuous Distributions

Continuous distributions model real-valued parameters, waiting times, or data points that can take any value within a range.

πŸ”΅ Gaussian (Normal) Distribution (tfd.Normal)

Gaussian Distribution

Mathematical Formulation: $$f(x) = \frac{1}{\sigma\sqrt{2\pi}} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right), \quad x \in \mathbb{R}$$

  • Support: $x \in \mathbb{R}$
  • Parameters: Mean $\mu \in \mathbb{R}$, standard deviation $\sigma > 0$
  • Typical DL Use Cases: Weight priors/posteriors in Bayesian Neural Networks (BNNs), VAE continuous latent spaces (reparameterization trick), and continuous regression with uncertainty output.

TFP Implementation:

import tensorflow_probability as tfp
tfd = tfp.distributions

# Standard Normal (mean 0, std dev 1)
normal = tfd.Normal(loc=0.0, scale=1.0)



πŸ”΅ Exponential Distribution (tfd.Exponential)

Exponential Distribution

Mathematical Formulation: $$f(x) = \lambda e^{-\lambda x}, \quad x \geq 0$$

  • Support: $x \in [0, \infty)$
  • Parameters: Decay/arrival rate $\lambda > 0$
  • Typical DL Use Cases: Survival analysis and time-to-event estimation, wait-time and queueing theory in networks, positive-valued prior assumptions.

TFP Implementation:

import tensorflow_probability as tfp
tfd = tfp.distributions

# Exponential decay rate of 1.0
exponential = tfd.Exponential(rate=1.0)



πŸ”΅ Beta Distribution (tfd.Beta)

Beta Distribution

Mathematical Formulation: $$f(x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{\mathrm{B}(\alpha, \beta)}, \quad x \in (0, 1)$$

  • Support: $x \in (0, 1)$
  • Parameters: Shape/concentration parameters $\alpha, \beta > 0$
  • Typical DL Use Cases: Prior distribution for Binomial/Bernoulli probabilities, modeling bounded target proportions, and Bayesian inference of rates / success probabilities.

TFP Implementation:

import tensorflow_probability as tfp
tfd = tfp.distributions

# Beta prior distribution
beta = tfd.Beta(concentration1=2.0, concentration0=5.0)



🌐 Multivariate Distributions

Multivariate distributions model higher-dimensional vectors of random variables, accounting for the correlations between different dimensions.

🌐 Multivariate Gaussian Distribution (tfd.MultivariateNormalTriL)

Multivariate Gaussian Distribution

Mathematical Formulation: $$f(\mathbf{x}) = \frac{e^{-\frac{1}{2}(\mathbf{x}-\boldsymbol{\mu})^T\boldsymbol{\Sigma}^{-1}(\mathbf{x}-\boldsymbol{\mu})}}{\sqrt{(2\pi)^k\vert\boldsymbol{\Sigma}\vert}}, \quad \mathbf{x} \in \mathbb{R}^k$$

  • Support: $\mathbf{x} \in \mathbb{R}^k$
  • Parameters: Mean vector $\boldsymbol{\mu} \in \mathbb{R}^k$, covariance matrix $\boldsymbol{\Sigma} \in \mathbb{R}^{k \times k}$ (positive-definite)
  • Typical DL Use Cases: Modeling correlated multidimensional features, generative modeling (Normalizing Flows and VAE priors), and Kalman filter state estimators.

TFP Implementation:

import tensorflow_probability as tfp
tfd = tfp.distributions

# Correlated bivariate normal via lower triangular Cholesky factor
mv_normal = tfd.MultivariateNormalTriL(
    loc=[0.0, 0.0],
    scale_tril=[[1.0, 0.0], 
                [0.6, 0.8]]
)



πŸ’‘ Core Distribution Methods in TFP

Every distribution object in TensorFlow Probability exposes a consistent API for seamless integration with TensorFlow graph execution:

  • sample(sample_shape): Draws Monte Carlo samples from the distribution.
  • log_prob(value): Computes the log probability density (or mass) function. Crucial for calculating custom loss functions (e.g. Negative Log-Likelihood).
  • prob(value): Computes the exact probability density/mass $P(X=x)$.
  • mean() / variance() / stddev(): Analytical moments of the distribution.
  • kl_divergence(other_dist): Analytical Kullback-Leibler divergence between two distributions of the same type.

πŸ§ͺ Hands-On Examples

Comprehensive Notebook Collection

# Topic Difficulty Key Concepts Notebook
00 Univariate Distributions 🟒 Beginner Normal, Exponential, Beta distributions Open Notebook
01 Multivariate Distributions 🟑 Intermediate MultivariateNormal, covariance structure Open Notebook
02 Independent Distributions 🟑 Intermediate tfd.Independent, batch dimensions Open Notebook
03 Sampling & Log Probabilities 🟑 Intermediate sample(), log_prob(), Monte Carlo Open Notebook
04 Trainable Distributions 🟑 Intermediate tf.Variable parameters, gradient flow Open Notebook
05 TFP Distributions Summary 🟒 Reference Distribution catalog, API reference Open Notebook
06 Independent Naive Classifier 🟑 Intermediate Feature independence, text classification Open Notebook
07 Naive Bayes with TFP 🟑 Intermediate Bayes' theorem, posterior computation Open Notebook
08 Multivariate Gaussian Full Covariance πŸ”΄ Advanced Full covariance, correlation modeling Open Notebook
09 Broadcasting Rules 🟑 Intermediate Shape manipulation, batch processing Open Notebook
10 Naive Bayes & Logistic Regression 🟑 Intermediate Generative vs discriminative models Open Notebook
11 Probabilistic Layers & Bayesian NNs πŸ”΄ Advanced DenseVariational, weight uncertainty, captured epistemic/aleatoric risk Open Notebook
12 Bijectors & Normalizing Flows πŸ”΄ Advanced tfp.bijectors, invertible transforms, RealNVP, coupling layers Open Notebook
13 Variational Autoencoders πŸ”΄ Advanced ELBO, reparameterization trick, celebrity face generation Open Notebook
14 Introduction to MCMC πŸ”΄ Advanced Sampling problems, Metropolis-Hastings from scratch Open Notebook
15 Hamiltonian Monte Carlo πŸ”΄ Advanced Hamiltonian dynamics, leapfrog integration, HMC in TFP Open Notebook
16 Bayesian Model Comparison πŸ”΄ Advanced Marginal likelihood, Bayes factors estimation via MCMC Open Notebook
17 Hierarchical Bayesian Models πŸ”΄ Advanced Joint distributions, pooling strategies, TFP joint distribution APIs Open Notebook
18 Gaussian Mixture Models 🟑 Intermediate MixtureSameFamily, 1D/2D mixtures, sampling & density evaluation Open Notebook
19 Expectation Maximization (EM) πŸ”΄ Advanced Latent variables, ELBO derivation, GMM EM from scratch Open Notebook
20 Bayesian Mixture Models πŸ”΄ Advanced Dirichlet-Multinomial mixtures, fully Bayesian GMMs with MCMC Open Notebook
21 Introduction to Gaussian Processes πŸ”΄ Advanced Distributions over functions, RBF/periodic kernels, GP prior sampling Open Notebook
22 GP Classification & Kernels πŸ”΄ Advanced Bernoulli likelihood, kernel engineering/arithmetic, latent GP classification Open Notebook
23 Sparse & Scalable GPs πŸ”΄ Expert Inducing variables, Variational Sparse GP (VSGP) optimization Open Notebook
24 Structural Time Series Models 🟑 Intermediate State-space models, local linear trend, seasonality components, tfp.sts Open Notebook
25 Bayesian Time Series Forecasting πŸ”΄ Advanced Multiple seasonalities, regression, spike-and-slab variable selection Open Notebook
26 Deep Probabilistic Time Series πŸ”΄ Advanced Probabilistic LSTMs, Negative Log-Likelihood (NLL) loss, seq-to-seq uncertainty Open Notebook
27 Capstone: Bayesian Medical Diagnosis πŸ”΄ Expert Clinical decision support, BNN for diagnosis, KL weight regularization Open Notebook
28 Capstone: Probabilistic Forecasting πŸ”΄ Expert Synthetic market data, BSTS, Value at Risk (VaR), risk quantification Open Notebook
29 Capstone: Probabilistic Generative Models πŸ”΄ Expert Normalizing flow dataset creation, VAE, latent space interpolation Open Notebook

πŸ“Š Performance Benchmarks

Training Time Comparison

Model Type Dataset Standard NN Bayesian NN VAE Normalizing Flow
MNIST Classification 60k samples 2 min 8 min 12 min 15 min
CIFAR-10 Classification 50k samples 15 min 45 min 60 min 90 min
CelebA Generation 200k samples N/A N/A 120 min 180 min

Benchmarks on NVIDIA RTX 3090 GPU

Memory Usage

Probabilistic models typically require 2-4x more memory than standard models due to:

  • Parameter uncertainty representation
  • Additional forward/backward passes
  • Sampling operations during training

🎯 TensorFlow Probability vs TensorFlow Core

Aspect TensorFlow Probability (TFP) TensorFlow Core (TF)
Primary Focus Probabilistic modeling, uncertainty quantification Deterministic neural networks, optimization
Model Output Distributions with uncertainty bounds Point estimates
Key Strengths Bayesian inference, generative modeling Fast training, established workflows
Learning Curve Steeper (requires probability theory) Gentler (standard ML concepts)
Memory Usage Higher (parameter distributions) Lower (point parameters)
Training Time Slower (sampling, variational inference) Faster (direct optimization)
Interpretability Higher (uncertainty quantification) Lower (black box predictions)
Best Use Cases Critical decisions, small data, research Large datasets, production systems

🀝 Contributing

We welcome contributions from the community! Here's how you can help:

Contribution Process

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“š Additional Resources

Reference Materials

Research Papers

Foundational Papers

Normalizing Flows & Bijectors

Bayesian Neural Networks

Variational Inference & MCMC

Gaussian Processes

Probabilistic Time Series Forecasting

TensorFlow Probability Specific

Applications & Case Studies


βš–οΈ License

This project is licensed under the MIT License - see the LICENSE file for details


πŸ”— Connect with me

Twitter LinkedIn Stack Exchange GitHub

Releases

No releases published

Packages

 
 
 

Contributors