A clean, modular implementation of Denoising Diffusion Probabilistic Models (DDPM) applied to the MNIST dataset. This project implements the core mathematics and architectural requirements described in the seminal paper "Denoising Diffusion Probabilistic Models" by Ho et al.
This repository provides a complete pipeline for training a generative model that can synthesize handwritten digits from pure Gaussian noise. Unlike standard GANs, DDPMs learn to reverse a gradual multi-step corruption process.
- Modular Architecture: Separate components for the UNet, Gaussian Scheduler, and Data Pipeline.
- Optimized UNet: Features Sinusoidal Positional Timestep Embeddings, Group Normalization, and SiLU (Swish) activations for training stability.
- Kaggle Ready: Includes a consolidated script optimized for Kaggle's T4 GPUs.
- Mathematical Precision: Accurate implementation of the Langevin dynamics reverse step.
We gradually add Gaussian noise to a clean image
The model is trained to predict the noise
The model
The backbone is a symmetrical UNet designed for
- Encoder: Two downsampling stages using
MaxPool2dand residual-styleBlocks. - Bottleneck: Dual
Blocklayers to capture high-level latent representations. - Decoder:
ConvTranspose2dfor spatial recovery with skip connections to preserve fine-grained details. - Normalization:
GroupNorm(8 groups) is used throughout to stabilize training across various batch sizes. - Activation:
SiLU(Sigmoid Linear Unit) for smooth gradient flow.
git clone https://github.com/sharksurfauto-byte/ddpm_implementation_on_MNIST.git
cd ddpm_implementation_on_MNIST
pip install torch torchvision tqdmTo train the model on your local machine or a server:
python train.pyThe script will automatically download MNIST and save checkpoints every 5 epochs.
To generate new images using a trained checkpoint:
python sample.pyOutputs will be saved as ddpm_generated_digits.png.
For immediate training on Kaggle, use the kaggle_ddpm_mnist.py script. It is a standalone file that can be pasted into a single cell.
After 20 epochs with a learning rate of 2e-4, the model successfully transitions from static noise to recognizable digit structures, demonstrating the effective learning of the MNIST data manifold.
- Ho, J., Jain, A., & Abbeel, P. (2020). Denoising Diffusion Probabilistic Models.
- Vaswani, A., et al. (2017). Attention is All You Need (for Sinusoidal Embeddings).

