This repository provides a visual comparison between two generative modeling approaches: Diffusion Models and Flow Matching. The implementation focuses on a simple 2D toy dataset to help understand and visualize the fundamental differences between these methods.
Diffusion models work by gradually adding noise to data and then learning to reverse this process. They:
- Start with pure noise
- Gradually denoise the data through multiple steps
- Use a neural network to predict and remove noise at each step
- Follow a predefined noise schedule
Flow matching directly learns a continuous transformation between noise and data distributions. They:
- Learn velocity fields that transform noise to data
- Use a single neural network to predict velocities
- Transform data in a single continuous flow
- Don't require a noise schedule
In this implementation, we use a linear interpolation path with added noise:
x_t = (1 - t) * z + t * x_0 + noise * torch.sqrt(t * (1 - t))where:
zis the noise distributionx_0is the target datatis the time parameter (0 to 1)noiseis small Gaussian noise scaled bysigma=0.1- The noise term
torch.sqrt(t * (1 - t))ensures smooth transitions
This path choice provides a simple yet effective way to transform between distributions, with the noise term helping to stabilize training and prevent mode collapse.
Create a conda environment and install dependencies:
conda create -n difffm python=3.10
conda activate difffm
pip install torch numpy matplotlib tqdm ipythonRun the main script to see the comparison:
python diff_vs_flowmatch.py run-diffusion-demo --n-epochs 200 --batch-size 128 --n-samples 1000python diff_vs_flowmatch.py run-flow-matching-demo --n-epochs 200 --batch-size 128 --n-samples 1000python diff_vs_flowmatch.py run-comparison-demo --n-epochs 200 --batch-size 128 --n-samples 1000This will:
- Train both models on a toy dataset (3 Gaussian clusters)
- Generate samples from both models
- Create various visualizations to compare the approaches
The code generates several types of visualizations:
-
Sample Comparison
- Real vs Generated samples for both methods
- Shows final quality of generated samples
-
Trajectory Visualization
- Shows how individual points move during generation
- Helps understand the path from noise to data
-
Intermediate Steps
- Displays snapshots of the generation process
- Shows how the distribution evolves over time
-
Vector Fields
- Visualizes the learned vector fields at different timesteps
- Shows how each model guides points toward the data distribution
-
Animations
- Dynamic visualization of the generation process
- Creates GIFs showing the full transformation
The repository contains several key components:
SimpleMLP: Neural network architecture shared by both modelsDiffusionModel: Implementation of the diffusion processFlowMatching: Implementation of the flow matching approach- Various visualization functions for analysis and comparison
-
Training Objective
- Diffusion: Predicts noise at each step
- Flow Matching: Predicts velocity vectors
-
Generation Process
- Diffusion: Discrete steps from noise to data
- Flow Matching: Continuous flow from noise to data
-
Sampling
- Diffusion: Uses multiple denoising steps
- Flow Matching: Uses ODE solvers (Euler or Heun's method)
The visualizations help understand how each method approaches the generation task:
- Diffusion models gradually denoise the data through many small steps
- Flow matching creates a smooth transformation from noise to data
- Both methods can successfully learn the target distribution
- Each method has its own characteristic generation trajectory
