A comprehensive PyTorch implementation of Denoising Diffusion Probabilistic Models (DDPM) with detailed mathematical foundations and educational content.
Model Description
This repository contains a complete implementation of Diffusion Models (DDPM) trained on 2D synthetic datasets. The model learns to generate new data points by mastering the art of noise removal through a reverse diffusion process. This implementation serves as both a working model and an educational resource for understanding the mathematics and implementation of diffusion models.
Architecture Details
Model Type: Denoising Diffusion Probabilistic Model (DDPM)
Framework: PyTorch
Input: 2D point coordinates
Diffusion Steps: 1000 timesteps
Hidden Dimensions: 256 units with SiLU activations
Time Embedding: 64-dimensional rich representations
Total Parameters: ~130K
Model Size: 1.8MB
Key Components
Noise Predictor Network: Neural network that predicts noise ε_θ(x_t, t)
Forward Diffusion Process: Gradually adds Gaussian noise over T steps
Reverse Diffusion Process: Iteratively removes noise to generate samples
Time Embedding Module: Converts timesteps to rich feature representations
Training Details
Dataset: Synthetic 2D point clusters
Diffusion Steps: 1000
Beta Schedule: Linear (0.0001 to 0.02)
Optimizer: AdamW with cosine annealing
Learning Rate: 0.001
Training Epochs: 2000
Batch Processing: Dynamic batching for efficient training
Final Training Loss: Converged to stable low values
Training Time: ~30 minutes on GPU
Memory Usage: <500MB GPU memory
Convergence: Stable training without mode collapse
Capabilities
✅ High-quality 2D point generation
✅ Smooth interpolation in data space
✅ Stable training without adversarial dynamics
✅ Mathematically grounded approach
✅ Excellent sample diversity
Usage
Quick Start
python
1import torch
2import torch.nn as nn
3import matplotlib.pyplot as plt
45# Load the model components (full implementation in notebook)6classNoisePredictor(nn.Module):7def__init__(self, data_dim=2, hidden_dim=256, time_embed_dim=64):8super(NoisePredictor, self).__init__()9# ... (complete implementation in notebook)1011defforward(self, x, t):12# ... (complete implementation in notebook)13return noise_prediction
1415classDiffusionModel:16def__init__(self, T=1000, beta_start=0.0001, beta_end=0.02):17# ... (complete implementation in notebook)1819defsample(self, n_samples=100):20# Generate new samples from pure noise21# ... (complete implementation in notebook)22return generated_samples
2324# Load trained model25model = DiffusionModel()26# Load weights: model.model.load_state_dict(torch.load('diffusion_model_complete.pth'))2728# Generate new samples29samples = model.sample(n_samples=100)30plt.scatter(samples[:,0], samples[:,1])31plt.title("Generated 2D Points")32plt.show()
Advanced Usage
python
1# Visualize the diffusion process2model.visualize_diffusion_process()34# Monitor training progress5model.plot_training_curves()67# Sample with different parameters8high_quality_samples = model.sample(n_samples=500, guidance_scale=1.0)
Visualizations Available
Diffusion Process: Step-by-step noise addition and removal
Training Curves: Loss evolution and learning dynamics
Generated Samples: Comparison with original data distribution