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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 Probabilistic Deep Learning with TensorFlow

Author TensorFlow TensorFlow Probability Platform Maintained Last Commit GitHub Issues Stars License Contributions Welcome Repo Size

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.

✨Unlock the power of uncertainty quantification in machine learning.

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 these distributions is crucial for effective probabilistic modeling:


πŸ“Š Discrete Distributions

Binomial Distribution

Models the number of successes in (n) independent trials with probability (p).

$$ P(X = k) = \binom{n}{k} p^k (1-p)^{n-k} $$

Use Cases: A/B testing, quality control, medical trials


Poisson Distribution

Models the number of events occurring in a fixed interval.

$$ P(X = k) = \frac{\lambda^k e^{-\lambda}}{k!} $$

Use Cases: Customer arrivals, system failures, web traffic


πŸ“ˆ Continuous Distributions

Gaussian (Normal) Distribution

The cornerstone of probabilistic modeling with symmetric, bell-shaped curves.

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

Use Cases: Neural network weights, measurement errors, natural phenomena


Exponential Distribution

Models waiting times and survival analysis.

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

Use Cases: System reliability, queueing theory, survival analysis


🌐 Multivariate Distributions

Multivariate Gaussian

Essential for modeling correlated variables with full covariance structure.

$$ f(\mathbf{x}) = \frac{1}{\sqrt{(2\pi)^k|\boldsymbol{\Sigma}|}} \exp\left(-\frac{1}{2}(\mathbf{x}-\boldsymbol{\mu})^T\boldsymbol{\Sigma}^{-1}(\mathbf{x}-\boldsymbol{\mu})\right) $$

Use Cases: Dimensionality reduction, portfolio optimization, computer vision


πŸ§ͺ 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 Open Notebook
12 Bijectors & Normalizing Flows πŸ”΄ Advanced tfp.bijectors, invertible transforms Open Notebook
13 Variational Autoencoders πŸ”΄ Advanced ELBO, reparameterization trick Open Notebook
14 Probabilistic Generative Models πŸ”΄ Expert Complete pipeline, model evaluation 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 3080 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

🌟 Star History

Star History Chart


πŸ“š Additional Resources

Reference Materials

Research Papers

Foundational Papers

Normalizing Flows & Bijectors

Bayesian Neural Networks

Variational Inference & MCMC

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

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